Skip to content

Commit 69d23d0

Browse files
committed
feat: update solutions to lc problem: No.0001
No.0001.Two Sum
1 parent 32e7381 commit 69d23d0

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

.prettierrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"bracketSpacing": true,
88
"arrowParens": "avoid",
99
"phpVersion": "8.1",
10-
"braceStyle": "1tbs"
10+
"braceStyle": "1tbs",
11+
"endOfLine": "lf"
1112
}

solution/0000-0099/0001.Two Sum/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,12 @@ class Solution {
244244
* @return Integer[]
245245
*/
246246
function twoSum($nums, $target) {
247-
$len = count($nums);
248-
for ($i = 0; $i < $len; $i++) {
249-
for ($j = 1 + $i; $j < $len; $j++) {
250-
if ($nums[$i] + $nums[$j] == $target) {
251-
return [$i, $j];
252-
}
247+
foreach ($nums as $key => $x) {
248+
$y = $target - $x;
249+
if (isset($hashtable[$y])) {
250+
return [$hashtable[$y], $key];
253251
}
252+
$hashtable[$x] = $key;
254253
}
255254
}
256255
}

solution/0000-0099/0001.Two Sum/README_EN.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,12 @@ class Solution {
233233
* @return Integer[]
234234
*/
235235
function twoSum($nums, $target) {
236-
$len = count($nums);
237-
for ($i = 0; $i < $len; $i++) {
238-
for ($j = 1 + $i; $j < $len; $j++) {
239-
if ($nums[$i] + $nums[$j] == $target) {
240-
return [$i, $j];
241-
}
236+
foreach ($nums as $key => $x) {
237+
$y = $target - $x;
238+
if (isset($hashtable[$y])) {
239+
return [$hashtable[$y], $key];
242240
}
241+
$hashtable[$x] = $key;
243242
}
244243
}
245244
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Solution {
2-
32
/**
43
* @param Integer[] $nums
54
* @param Integer $target
65
* @return Integer[]
76
*/
87
function twoSum($nums, $target) {
9-
$len = count($nums);
10-
for ($i = 0; $i < $len; $i++) {
11-
for ($j = 1 + $i; $j < $len; $j++) {
12-
if ($nums[$i] + $nums[$j] == $target) {
13-
return [$i, $j];
14-
}
8+
foreach ($nums as $key => $x) {
9+
$y = $target - $x;
10+
if (isset($hashtable[$y])) {
11+
return [$hashtable[$y], $key];
1512
}
13+
$hashtable[$x] = $key;
1614
}
1715
}
1816
}

0 commit comments

Comments
 (0)