Skip to content

Commit e85d579

Browse files
authored
Merge branch 'doocs:main' into main
2 parents 03fcdff + 53605fd commit e85d579

File tree

46 files changed

+1203
-904
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1203
-904
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ use std::collections::HashMap;
145145

146146
impl Solution {
147147
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
148-
let mut map = HashMap::new();
149-
for (i, item) in nums.iter().enumerate() {
150-
if map.contains_key(item) {
151-
return vec![i as i32, map[item]];
152-
} else {
153-
let x = target - nums[i];
154-
map.insert(x, i as i32);
148+
let mut m = HashMap::new();
149+
for (i, &x) in nums.iter().enumerate() {
150+
let y = target - x;
151+
if let Some(&j) = m.get(&y) {
152+
return vec![j as i32, i as i32];
155153
}
154+
m.insert(x, i as i32);
156155
}
157156
unreachable!()
158157
}
@@ -204,12 +203,13 @@ class Solution {
204203
* @return Integer[]
205204
*/
206205
function twoSum($nums, $target) {
207-
foreach ($nums as $key => $x) {
206+
$m = [];
207+
foreach ($nums as $i => $x) {
208208
$y = $target - $x;
209-
if (isset($hashtable[$y])) {
210-
return [$hashtable[$y], $key];
209+
if (isset($m[$y])) {
210+
return [$m[$y], $i];
211211
}
212-
$hashtable[$x] = $key;
212+
$m[$x] = $i;
213213
}
214214
}
215215
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,13 @@ use std::collections::HashMap;
140140

141141
impl Solution {
142142
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
143-
let mut map = HashMap::new();
144-
for (i, item) in nums.iter().enumerate() {
145-
if map.contains_key(item) {
146-
return vec![i as i32, map[item]];
147-
} else {
148-
let x = target - nums[i];
149-
map.insert(x, i as i32);
143+
let mut m = HashMap::new();
144+
for (i, &x) in nums.iter().enumerate() {
145+
let y = target - x;
146+
if let Some(&j) = m.get(&y) {
147+
return vec![j as i32, i as i32];
150148
}
149+
m.insert(x, i as i32);
151150
}
152151
unreachable!()
153152
}
@@ -199,12 +198,13 @@ class Solution {
199198
* @return Integer[]
200199
*/
201200
function twoSum($nums, $target) {
202-
foreach ($nums as $key => $x) {
201+
$m = [];
202+
foreach ($nums as $i => $x) {
203203
$y = $target - $x;
204-
if (isset($hashtable[$y])) {
205-
return [$hashtable[$y], $key];
204+
if (isset($m[$y])) {
205+
return [$m[$y], $i];
206206
}
207-
$hashtable[$x] = $key;
207+
$m[$x] = $i;
208208
}
209209
}
210210
}

solution/0000-0099/0001.Two Sum/Solution.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ class Solution {
55
* @return Integer[]
66
*/
77
function twoSum($nums, $target) {
8-
foreach ($nums as $key => $x) {
8+
$m = [];
9+
foreach ($nums as $i => $x) {
910
$y = $target - $x;
10-
if (isset($hashtable[$y])) {
11-
return [$hashtable[$y], $key];
11+
if (isset($m[$y])) {
12+
return [$m[$y], $i];
1213
}
13-
$hashtable[$x] = $key;
14+
$m[$x] = $i;
1415
}
1516
}
16-
}
17+
}

solution/0000-0099/0001.Two Sum/Solution.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ use std::collections::HashMap;
22

33
impl Solution {
44
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
5-
let mut map = HashMap::new();
6-
for (i, item) in nums.iter().enumerate() {
7-
if map.contains_key(item) {
8-
return vec![i as i32, map[item]];
9-
} else {
10-
let x = target - nums[i];
11-
map.insert(x, i as i32);
5+
let mut m = HashMap::new();
6+
for (i, &x) in nums.iter().enumerate() {
7+
let y = target - x;
8+
if let Some(&j) = m.get(&y) {
9+
return vec![j as i32, i as i32];
1210
}
11+
m.insert(x, i as i32);
1312
}
1413
unreachable!()
1514
}

solution/0000-0099/0011.Container With Most Water/README.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ impl Solution {
151151
pub fn max_area(height: Vec<i32>) -> i32 {
152152
let mut i = 0;
153153
let mut j = height.len() - 1;
154-
let mut res = 0;
154+
let mut ans = 0;
155155
while i < j {
156-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
156+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
157157
if height[i] <= height[j] {
158158
i += 1;
159159
} else {
160160
j -= 1;
161161
}
162162
}
163-
res
163+
ans
164164
}
165165
}
166166
```
@@ -209,28 +209,23 @@ public class Solution {
209209
```php
210210
class Solution {
211211
/**
212-
* @param int[] $height
213-
* @return int
212+
* @param Integer[] $height
213+
* @return Integer
214214
*/
215-
216215
function maxArea($height) {
217-
$left = 0;
218-
$right = count($height) - 1;
219-
$maxArea = 0;
220-
221-
while ($left < $right) {
222-
$area = min($height[$left], $height[$right]) * ($right - $left);
223-
224-
$maxArea = max($maxArea, $area);
225-
226-
if ($height[$left] < $height[$right]) {
227-
$left++;
216+
$i = 0;
217+
$j = count($height) - 1;
218+
$ans = 0;
219+
while ($i < $j) {
220+
$t = min($height[$i], $height[$j]) * ($j - $i);
221+
$ans = max($ans, $t);
222+
if ($height[$i] < $height[$j]) {
223+
++$i;
228224
} else {
229-
$right--;
225+
--$j;
230226
}
231227
}
232-
233-
return $maxArea;
228+
return $ans;
234229
}
235230
}
236231
```

solution/0000-0099/0011.Container With Most Water/README_EN.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ impl Solution {
146146
pub fn max_area(height: Vec<i32>) -> i32 {
147147
let mut i = 0;
148148
let mut j = height.len() - 1;
149-
let mut res = 0;
149+
let mut ans = 0;
150150
while i < j {
151-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
151+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
152152
if height[i] <= height[j] {
153153
i += 1;
154154
} else {
155155
j -= 1;
156156
}
157157
}
158-
res
158+
ans
159159
}
160160
}
161161
```
@@ -204,28 +204,23 @@ public class Solution {
204204
```php
205205
class Solution {
206206
/**
207-
* @param int[] $height
208-
* @return int
207+
* @param Integer[] $height
208+
* @return Integer
209209
*/
210-
211210
function maxArea($height) {
212-
$left = 0;
213-
$right = count($height) - 1;
214-
$maxArea = 0;
215-
216-
while ($left < $right) {
217-
$area = min($height[$left], $height[$right]) * ($right - $left);
218-
219-
$maxArea = max($maxArea, $area);
220-
221-
if ($height[$left] < $height[$right]) {
222-
$left++;
211+
$i = 0;
212+
$j = count($height) - 1;
213+
$ans = 0;
214+
while ($i < $j) {
215+
$t = min($height[$i], $height[$j]) * ($j - $i);
216+
$ans = max($ans, $t);
217+
if ($height[$i] < $height[$j]) {
218+
++$i;
223219
} else {
224-
$right--;
220+
--$j;
225221
}
226222
}
227-
228-
return $maxArea;
223+
return $ans;
229224
}
230225
}
231226
```
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
class Solution {
22
/**
3-
* @param int[] $height
4-
* @return int
3+
* @param Integer[] $height
4+
* @return Integer
55
*/
6-
76
function maxArea($height) {
8-
$left = 0;
9-
$right = count($height) - 1;
10-
$maxArea = 0;
11-
12-
while ($left < $right) {
13-
$area = min($height[$left], $height[$right]) * ($right - $left);
14-
15-
$maxArea = max($maxArea, $area);
16-
17-
if ($height[$left] < $height[$right]) {
18-
$left++;
7+
$i = 0;
8+
$j = count($height) - 1;
9+
$ans = 0;
10+
while ($i < $j) {
11+
$t = min($height[$i], $height[$j]) * ($j - $i);
12+
$ans = max($ans, $t);
13+
if ($height[$i] < $height[$j]) {
14+
++$i;
1915
} else {
20-
$right--;
16+
--$j;
2117
}
2218
}
23-
24-
return $maxArea;
19+
return $ans;
2520
}
26-
}
21+
}

solution/0000-0099/0011.Container With Most Water/Solution.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ impl Solution {
22
pub fn max_area(height: Vec<i32>) -> i32 {
33
let mut i = 0;
44
let mut j = height.len() - 1;
5-
let mut res = 0;
5+
let mut ans = 0;
66
while i < j {
7-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
7+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
88
if height[i] <= height[j] {
99
i += 1;
1010
} else {
1111
j -= 1;
1212
}
1313
}
14-
res
14+
ans
1515
}
1616
}

solution/0000-0099/0015.3Sum/README.md

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -386,49 +386,37 @@ end
386386
```php
387387
class Solution {
388388
/**
389-
* @param int[] $nums
390-
* @return int[][];
389+
* @param Integer[] $nums
390+
* @return Integer[][]
391391
*/
392-
393392
function threeSum($nums) {
394-
$result = [];
395-
$n = count($nums);
396-
397393
sort($nums);
398-
for ($i = 0; $i < $n - 2; $i++) {
399-
if ($i > 0 && $nums[$i] === $nums[$i - 1]) {
394+
$ans = [];
395+
$n = count($nums);
396+
for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) {
397+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
400398
continue;
401399
}
402-
403-
$left = $i + 1;
404-
$right = $n - 1;
405-
406-
while ($left < $right) {
407-
$sum = $nums[$i] + $nums[$left] + $nums[$right];
408-
409-
if ($sum === 0) {
410-
$triplet = [$nums[$i], $nums[$left], $nums[$right]];
411-
$result[] = $triplet;
412-
413-
while ($left < $right && $nums[$left] === $nums[$left + 1]) {
414-
$left++;
400+
$j = $i + 1;
401+
$k = $n - 1;
402+
while ($j < $k) {
403+
$x = $nums[$i] + $nums[$j] + $nums[$k];
404+
if ($x < 0) {
405+
++$j;
406+
} elseif ($x > 0) {
407+
--$k;
408+
} else {
409+
$ans[] = [$nums[$i], $nums[$j++], $nums[$k--]];
410+
while ($j < $k && $nums[$j] == $nums[$j - 1]) {
411+
++$j;
415412
}
416-
417-
while ($left < $right && $nums[$right] === $nums[$right - 1]) {
418-
$right--;
413+
while ($j < $k && $nums[$k] == $nums[$k + 1]) {
414+
--$k;
419415
}
420-
421-
$left++;
422-
$right--;
423-
} elseif ($sum < 0) {
424-
$left++;
425-
} else {
426-
$right--;
427416
}
428417
}
429418
}
430-
431-
return $result;
419+
return $ans;
432420
}
433421
}
434422
```

0 commit comments

Comments
 (0)