File tree 4 files changed +104
-0
lines changed
4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public List <List <Integer >> threeSum (int [] nums ) {
3
+ List <List <Integer >> result = new ArrayList <>();
4
+ Arrays .sort (nums );
5
+ if (nums .length == 3 && (nums [0 ] + nums [1 ] + nums [2 ]) == 0 ) {
6
+ result .add (new ArrayList <Integer >());
7
+ result .get (0 ).add (nums [0 ]);
8
+ result .get (0 ).add (nums [1 ]);
9
+ result .get (0 ).add (nums [2 ]);
10
+ return result ;
11
+ }
12
+ for (int i = 0 ; i < nums .length - 2 ; i ++) {
13
+ if (i == 0 || (i > 0 && nums [i ] != nums [i -1 ])) {
14
+ int left = i +1 ;
15
+ int right = nums .length - 1 ;
16
+
17
+ while (right > left ) {
18
+ int threeSum = nums [i ] + nums [left ] + nums [right ];
19
+ if (threeSum > 0 ) {
20
+ right --;
21
+ } else if (threeSum < 0 ) {
22
+ left ++;
23
+ } else {
24
+ ArrayList <Integer > newL = new ArrayList <Integer >();
25
+ newL .add (nums [i ]);
26
+ newL .add (nums [left ]);
27
+ newL .add (nums [right ]);
28
+ result .add (newL );
29
+ while (left < right && nums [left ] == nums [left + 1 ]) {
30
+ left ++;
31
+ }
32
+ while (nums [right ] == nums [right - 1 ] && left < right ) {
33
+ right --;
34
+ }
35
+ left ++;
36
+ right --;
37
+ }
38
+ }
39
+ }
40
+ }
41
+ return result ;
42
+ }
43
+
44
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxArea (int [] height ) {
3
+ int l = 0 ;
4
+ int r = height .length - 1 ;
5
+ int maxArea = 0 ;
6
+ while (l < r ) {
7
+ int area = ((r +1 ) - (l +1 )) * Math .min (height [l ],height [r ]);
8
+ if (area > maxArea ) {
9
+ maxArea = area ;
10
+ }
11
+ if (height [l ] < height [r ]) {
12
+ l = l + 1 ;
13
+ } else {
14
+ r = r - 1 ;
15
+ }
16
+ }
17
+ return maxArea ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ import java .util .Map ;
2
+ import java .util .ArrayList ;
3
+
4
+ class Solution {
5
+ public List <List <String >> groupAnagrams (String [] strs ) {
6
+ Map <String , List <String >> map = new HashMap <>();
7
+
8
+ for (String word : strs ) {
9
+ char [] chars = word .toCharArray ();
10
+ Arrays .sort (chars );
11
+ String sorted = new String (chars );
12
+
13
+ if (map .containsKey (sorted )) {
14
+ map .get (sorted ).add (word );
15
+ } else {
16
+ map .put (sorted , new ArrayList <>());
17
+ map .get (sorted ).add (word );
18
+ }
19
+
20
+ }
21
+
22
+ return new ArrayList <>(map .values ());
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] productExceptSelf (int [] nums ) {
3
+ int [] answers = new int [nums .length ];
4
+ int left = 1 ;
5
+ int right = 1 ;
6
+ for (int i = 0 ; i < nums .length ; i ++) {
7
+ answers [i ] = left ;
8
+ left *= nums [i ];
9
+ }
10
+ for (int i = nums .length - 1 ; i >= 0 ; i --) {
11
+ answers [i ] *= right ;
12
+ right *= nums [i ];
13
+ }
14
+
15
+ return answers ;
16
+ }
17
+ }
You can’t perform that action at this time.
0 commit comments