Skip to content

Commit 3c4a8f5

Browse files
committed
2 parents 6757360 + 081a46f commit 3c4a8f5

File tree

12 files changed

+243
-52
lines changed

12 files changed

+243
-52
lines changed

Easy/1.Two-sum/solution.py

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class Solution
4+
{
5+
public function checkStraightLine(array $coordinates)
6+
{
7+
$n = count($coordinates);
8+
9+
if ($n <= 2) {
10+
return true;
11+
}
12+
13+
// Calculate vectors and check cross product for collinearity
14+
$vector1 = [$coordinates[1][0] - $coordinates[0][0], $coordinates[1][1] - $coordinates[0][1]];
15+
16+
for ($i = 2; $i < $n; $i++) {
17+
$vector2 = [$coordinates[$i][0] - $coordinates[0][0], $coordinates[$i][1] - $coordinates[0][1]];
18+
19+
// Calculate cross product
20+
$crossProduct = ($vector1[0] * $vector2[1] ) - ($vector1[1] * $vector2[0]);
21+
22+
// If cross product is non-zero, points are not collinear
23+
if ($crossProduct !== 0) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
}
31+
32+
$solution = new Solution();
33+
34+
$coordinates = [[0, 0], [0, 1], [0, -1]];
35+
36+
$result = $solution->checkStraightLine($coordinates);
37+
38+
echo $result;
39+
40+
?>

Easy/14.Longest common prefix/sl.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
<?php
22

3-
class Solution{
4-
5-
6-
/**
7-
* @param String[] $strs
8-
* @return String
9-
*/
10-
function longestCommonPrefix($strs)
3+
class Solution
4+
{
5+
public function longestCommonPrefix($strs)
116
{
127
$first = array_shift($strs);
138
$check_match = str_split($first);
149
$length = count($check_match);
1510

16-
// print_r($length);
1711
$prefix = '';
1812
for ($i = 0; $i <= $length; $i++) {
13+
1914
foreach ($strs as $str) {
2015
if (!isset($str[$i])) {
2116
break 2;
@@ -32,7 +27,9 @@ function longestCommonPrefix($strs)
3227

3328
}
3429

35-
3630
$solution = new Solution();
3731

38-
$solution->longestCommonPrefix(["flower", "flow", "flight"]);
32+
$solution->longestCommonPrefix(["flower", "flow", "flight"]);
33+
34+
35+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class Solution
4+
{
5+
public function isAnagram($s, $t)
6+
{
7+
if (strlen($s) != strlen($t)) {
8+
return false;
9+
}
10+
11+
$array1 = count_chars(strtolower($s));
12+
$array2 = count_chars(strtolower($t));
13+
14+
//match check for invalid characters
15+
if (!empty(array_diff_assoc($array2, $array1))) {
16+
return false;
17+
}
18+
if (!empty(array_diff_assoc($array1, $array2))) {
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
}
26+
27+
?>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
class Solution
4+
{
5+
public function strStr($haystack, $needle)
6+
{
7+
8+
$position = strpos($haystack, $needle);
9+
10+
if ($position !== false) {
11+
return $position;
12+
} else {
13+
return -1;
14+
}
15+
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class Solution
4+
{
5+
public function findTheDifference($s, $t)
6+
{
7+
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
class ListNode
3+
{
4+
public $val = 0;
5+
public $next = null;
6+
7+
public function __construct($val = 0, $next = null)
8+
{
9+
$this->val = $val;
10+
$this->next = $next;
11+
}
12+
}
13+
14+
class Solution
15+
{
16+
public function mergeTwoLists($list1, $list2)
17+
{
18+
$dummy = new ListNode();
19+
$current = $dummy;
20+
21+
while (!empty($list1) && !empty($list2)) {
22+
if ($list1->val < $list2->val) {
23+
$current->next = $list1;
24+
$list1 = $list1->next;
25+
} else {
26+
$current->next = $list2;
27+
$list2 = $list2->next;
28+
}
29+
$current = $current->next;
30+
}
31+
32+
if (!empty($list1)) {
33+
$current->next = $list1;
34+
} elseif (!empty($list2)) {
35+
$current->next = $list2;
36+
}
37+
38+
return $dummy->next;
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
class Solution
3+
{
4+
5+
public function multiply(string $num1, string $num2)
6+
{
7+
$m = strlen($num1);
8+
$n = strlen($num2);
9+
$result = array_fill(0, $m + $n, 0);
10+
11+
for ($i = $m - 1; $i >= 0; $i--) {
12+
for ($j = $n - 1; $j >= 0; $j--) {
13+
$multiply = intval($num1[$i]) * intval($num2[$j]);
14+
$sum = $multiply + $result[$i + $j + 1];
15+
16+
$result[$i + $j] += intdiv($sum, 10);
17+
$result[$i + $j + 1] = $sum % 10;
18+
}
19+
}
20+
21+
$resultString = implode('', $result);
22+
23+
$resultString = ltrim($resultString, '0');
24+
25+
26+
return $resultString === '' ? '0' : $resultString;
27+
}
28+
29+
}

Medium/50.Pow(x, n)/solution.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var myPow = function (x, n) {
2+
if (n === 0) return 1;
3+
if (n > 0) return pow(x, n);
4+
if (n < 0) return 1 / pow(x, -n);
5+
};
6+
7+
var pow = function (x, n) {
8+
if (n === 1) return x;
9+
var num = pow(x, Math.floor(n / 2));
10+
if (n % 2 === 0) {
11+
return num * num;
12+
} else {
13+
return x * num * num;
14+
}
15+
};
16+

0 commit comments

Comments
 (0)