File tree Expand file tree Collapse file tree 5 files changed +282
-2
lines changed Expand file tree Collapse file tree 5 files changed +282
-2
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=1 lang=java
3
+ *
4
+ * [1] Two Sum
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public int [] twoSum (int [] nums , int target ) {
10
+ Map <Integer , Integer > map = new HashMap <>();
11
+
12
+ for (int i = 0 ; i < nums .length ; i ++) {
13
+ int sum = target - nums [i ];
14
+ if (map .containsKey (sum )) {
15
+ return new int []{i , map .get (sum )};
16
+ }
17
+ map .put (nums [i ], i );
18
+ }
19
+ return new int []{};
20
+ }
21
+ }
22
+ // @lc code=end
23
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=15 lang=java
3
+ *
4
+ * [15] 3Sum
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public List <List <Integer >> threeSum (int [] nums ) {
10
+ List <List <Integer >> result = new ArrayList <>();
11
+
12
+ if (nums .length == 0 ) {
13
+ return result ;
14
+ }
15
+ Arrays .sort (nums );
16
+ for (int i = 0 ; i < nums .length ; i ++) {
17
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
18
+ int left = i + 1 ;
19
+ int right = nums .length - 1 ;
20
+
21
+ while (left < right ) {
22
+ if (nums [i ] + nums [left ] + nums [right ] == 0 ) {
23
+ result .add (new ArrayList <>(Arrays .asList (nums [i ], nums [left ], nums [right ])));
24
+ while (left < right && nums [left ] == nums [left + 1 ]) left ++;
25
+ while (left < right && nums [right ] == nums [right - 1 ]) right --;
26
+ left ++;
27
+ right --;
28
+ } else if (nums [i ] + nums [left ] + nums [right ] < 0 ) {
29
+ while (left < right && nums [left ] == nums [left + 1 ]) left ++;
30
+ left ++;
31
+ } else {
32
+ while (left < right && nums [right ] == nums [right - 1 ]) right --;
33
+ right --;
34
+ }
35
+ }
36
+ }
37
+ return result ;
38
+ }
39
+ }
40
+ // @lc code=end
41
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=16 lang=java
3
+ *
4
+ * [16] 3Sum Closest
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public int threeSumClosest (int [] nums , int target ) {
10
+ Arrays .sort (nums );
11
+
12
+ int resultSum = 0 ;
13
+ int closest = Integer .MAX_VALUE ;
14
+
15
+ for (int i = 0 ; i < nums .length ; i ++) {
16
+ int low = i + 1 ;
17
+ int high = nums .length - 1 ;
18
+
19
+ if (i > 0 && nums [i ] == nums [i - 1 ]) continue ;
20
+
21
+ while (low < high ) {
22
+ int tempSum = nums [i ] + nums [low ] + nums [high ];
23
+ if (tempSum == target ) {
24
+ return target ;
25
+ } else if (tempSum < target ) {
26
+ low ++;
27
+ } else {
28
+ high --;
29
+ }
30
+ resultSum = closest < Math .abs (target - tempSum ) ? resultSum : tempSum ;
31
+ closest = Math .min (closest , Math .abs (target - tempSum ));
32
+ }
33
+ }
34
+ return resultSum ;
35
+ }
36
+ }
37
+ // @lc code=end
38
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode id=259 lang=java
3
+ *
4
+ * [259] 3Sum Smaller
5
+ */
6
+
7
+ // @lc code=start
8
+ class Solution {
9
+ public int threeSumSmaller (int [] nums , int target ) {
10
+ Arrays .sort (nums );
11
+ int count = 0 ;
12
+ for (int i = 0 ; i < nums .length ; i ++) {
13
+ int low = i + 1 ;
14
+ int high = nums .length - 1 ;
15
+
16
+ while (low < high ) {
17
+ int tempSum = nums [i ] + nums [low ] + nums [high ];
18
+ if (tempSum < target ) {
19
+ count += high - low ;
20
+ low ++;
21
+ } else {
22
+ high --;
23
+ }
24
+ }
25
+ }
26
+ return count ;
27
+ }
28
+ }
29
+ // @lc code=end
You can’t perform that action at this time.
0 commit comments