Skip to content

Commit

Permalink
chore: delete no need compare (azl397985856#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinconan authored May 6, 2020
1 parent 311ef9d commit d71baf1
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions problems/15.3-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,14 @@ var threeSum = function(nums) {
if (nums[i] > 0) break;
// skip duplicated result without set
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i;
let left = i + 1;
let right = nums.length - 1;

// for each index i
// we want to find the triplet [i, left, right] which sum to 0
while (left < right) {
// skip i === left or i === right, in that case, the index i will be used twice
if (left === i) {
left++;
} else if (right === i) {
right--;
} else if (nums[left] + nums[right] + nums[i] === 0) {
// since left < right, and left > i, no need to compare i === left and i === right.
if (nums[left] + nums[right] + nums[i] === 0) {
list.push([nums[left], nums[right], nums[i]]);
// skip duplicated result without set
while(nums[left] === nums[left + 1]) {
Expand Down

0 comments on commit d71baf1

Please sign in to comment.