GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/java/015_3Sum.java at master · yishangupenn/leetcode

Expand file tree

/

015_3Sum.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
47 lines (39 loc) · 1.48 KB
/

015_3Sum.java

Copy path

File metadata and controls

47 lines (39 loc) · 1.48 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//015. 3Sum
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
//create result list to store i,j,k
List<List<Integer>> result = new LinkedList<List<Integer>>();
//sorting nums
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
if (i > 0 && nums[i] == nums[i-1]) {
continue; //if nums have same numbers, just check one time.
}
while (left < right) {
int sum = nums[left] + nums[right] + nums[i];
if (sum == 0) {
//if sum == 0, store i,j,k
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++; //check anoter case
right--;
//if next number == now number
while (nums[left] == nums[left - 1] && left < right) {
left++;
}
while (nums[right] == nums[right + 1] && left < right) {
right--;
}
} else if (sum > 0) {
//if sum > 0, right--;
right--;
} else {
//if sum < 0, left++;
left++;
}
}
}
return result; //return result list
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.