forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ impl Solution { | |
max = area; | ||
} | ||
|
||
if rh< lh{ | ||
if rh < lh{ | ||
while r > 0 && height[r] <= rh{ | ||
r-=1; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
impl Solution { | ||
pub fn three_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> { | ||
nums.sort_unstable(); | ||
|
||
let mut res = vec![]; | ||
|
||
for i in 0..nums.len() - 1{ | ||
let n1 = nums[i]; | ||
|
||
if i > 0 && n1 == nums[i - 1]{ | ||
continue; | ||
} | ||
|
||
let (mut l, mut r) = (i + 1, nums.len() - 1); | ||
|
||
while l < r{ | ||
let n2 = nums[l]; | ||
let n3 = nums[r]; | ||
|
||
let sum = n1 + n2 + n3; | ||
|
||
if sum > 0{ | ||
r-=1; | ||
}else if sum < 0{ | ||
l+=1; | ||
}else{ | ||
res.push(vec![n1, n2, n3]); | ||
|
||
while l < r && n2 == nums[l + 1]{ | ||
l+=1; | ||
} | ||
l+=1; | ||
|
||
while l < r && n3 == nums[r - 1]{ | ||
r-=1; | ||
} | ||
r-=1; | ||
} | ||
} | ||
} | ||
|
||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
impl Solution { | ||
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> { | ||
let (mut l, mut r) = (0, numbers.len() - 1); | ||
|
||
while l < r{ | ||
let sum = numbers[l] + numbers[r]; | ||
|
||
if sum > target{ | ||
r-=1; | ||
}else if sum < target{ | ||
l+=1; | ||
}else{ | ||
return vec![(l + 1) as i32, (r + 1) as i32]; | ||
} | ||
} | ||
|
||
unreachable!() | ||
} | ||
} |