File tree Expand file tree Collapse file tree 4 files changed +82
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 4 files changed +82
-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+
4+ List <List <Integer >> answer = new ArrayList <>();
5+ Arrays .sort (nums );
6+ int len = nums .length ;
7+
8+ for (int i = 0 ; i < len - 2 ; i ++) {
9+ // 인접한 같은 수라면, 지나감
10+ if (i > 0 && nums [i ] == nums [i -1 ]) continue ;
11+
12+ int L = i + 1 , H = len - 1 ;
13+
14+ while (L < H ) {
15+ if (nums [L ] + nums [H ] + nums [i ] > 0 ) {
16+ H --;
17+ } else if (nums [L ] + nums [H ] + nums [i ] < 0 ) {
18+ L ++;
19+ } else {
20+ answer .add (Arrays .asList (nums [i ], nums [L ], nums [H ]));
21+ // 중복을 제거
22+ while (L < H && nums [L ] == nums [L +1 ]) L ++;
23+ while (L < H && nums [H ] == nums [H -1 ]) H --;
24+ L ++;
25+ H --;
26+ }
27+ }
28+ }
29+ return answer ;
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int climbStairs (int n ) {
3+ int [] dp = new int [n + 1 ];
4+
5+ if (n == 1 ) return 1 ;
6+ dp [1 ] = 1 ;
7+ dp [2 ] = 2 ;
8+
9+ for (int i = 3 ; i <= n ; i ++) {
10+ dp [i ] = dp [i - 1 ] + dp [i - 2 ];
11+ }
12+ return dp [n ];
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int [] productExceptSelf (int [] nums ) {
3+ int len = nums .length ;
4+ int [] res = new int [len ];
5+ int num = 1 ;
6+
7+ for (int i = 0 ; i < len ; i ++) {
8+ res [i ] = num ;
9+ num *= nums [i ];
10+ }
11+ num = 1 ;
12+ for (int i = len -1 ; i >=0 ; i --) {
13+ res [i ] *= num ;
14+ num *= nums [i ];
15+ }
16+
17+ for (int i = 0 ; i < len ; i ++) {
18+ System .out .print ( res [i ] + " " );
19+ }
20+
21+ return res ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isAnagram (String s , String t ) {
3+ if (s .length () != t .length ()) return false ;
4+
5+ Map <Character , Integer > mapS = new HashMap <>();
6+ Map <Character , Integer > mapT = new HashMap <>();
7+
8+ for (int i = 0 ; i < s .length (); i ++) {
9+ mapS .put (s .charAt (i ), mapS .getOrDefault (s .charAt (i ), 0 ) + 1 );
10+ mapT .put (t .charAt (i ), mapT .getOrDefault (t .charAt (i ), 0 ) + 1 );
11+ }
12+ return mapS .equals (mapT );
13+ }
14+ }
You can’t perform that action at this time.
0 commit comments