Skip to content

Commit b670368

Browse files
authored
chore: delete no need compare (#352)
1 parent 6be35f4 commit b670368

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

problems/15.3-sum.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,14 @@ var threeSum = function(nums) {
9393
if (nums[i] > 0) break;
9494
// skip duplicated result without set
9595
if (i > 0 && nums[i] === nums[i - 1]) continue;
96-
let left = i;
96+
let left = i + 1;
9797
let right = nums.length - 1;
9898

9999
// for each index i
100100
// we want to find the triplet [i, left, right] which sum to 0
101101
while (left < right) {
102-
// skip i === left or i === right, in that case, the index i will be used twice
103-
if (left === i) {
104-
left++;
105-
} else if (right === i) {
106-
right--;
107-
} else if (nums[left] + nums[right] + nums[i] === 0) {
102+
// since left < right, and left > i, no need to compare i === left and i === right.
103+
if (nums[left] + nums[right] + nums[i] === 0) {
108104
list.push([nums[left], nums[right], nums[i]]);
109105
// skip duplicated result without set
110106
while(nums[left] === nums[left + 1]) {

0 commit comments

Comments
 (0)