File tree Expand file tree Collapse file tree 1 file changed +3
-7
lines changed Expand file tree Collapse file tree 1 file changed +3
-7
lines changed Original file line number Diff line number Diff line change @@ -93,18 +93,14 @@ var threeSum = function(nums) {
93
93
if (nums[i] > 0 ) break ;
94
94
// skip duplicated result without set
95
95
if (i > 0 && nums[i] === nums[i - 1 ]) continue ;
96
- let left = i;
96
+ let left = i + 1 ;
97
97
let right = nums .length - 1 ;
98
98
99
99
// for each index i
100
100
// we want to find the triplet [i, left, right] which sum to 0
101
101
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 ) {
108
104
list .push ([nums[left], nums[right], nums[i]]);
109
105
// skip duplicated result without set
110
106
while (nums[left] === nums[left + 1 ]) {
You can’t perform that action at this time.
0 commit comments