File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ List <List <Integer >> result = new ArrayList <>();
3
+ int [] candidates ;
4
+
5
+ public List <List <Integer >> combinationSum (int [] candidates , int target ) {
6
+ this .candidates =candidates ;
7
+ backtrack (new ArrayList <>(), target , 0 );
8
+ return result ;
9
+ }
10
+ void backtrack (List <Integer > comb , int remain , int start ) {
11
+ if (remain ==0 ) {
12
+ result .add (new ArrayList <>(comb ));
13
+ return ;
14
+ }
15
+ if (remain <0 ) return ;
16
+ for (int i =start ; i <candidates .length ; i ++) {
17
+ comb .add (candidates [i ]);
18
+ backtrack (comb , remain -candidates [i ], i );
19
+ comb .remove (comb .size () - 1 );
20
+ }
21
+ }
22
+ }
23
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numDecodings (String s ) {
3
+ if (s .charAt (0 )=='0' ) return 0 ;
4
+
5
+ int n = s .length ();
6
+ int [] dp = new int [n +1 ];
7
+
8
+ dp [0 ]=1 ;
9
+ dp [1 ]=1 ;
10
+
11
+ for (int i =2 ; i <=n ; i ++) {
12
+ if (s .charAt (i -1 )!='0' ) dp [i ]+=dp [i -1 ];
13
+
14
+ int group = Integer .parseInt (s .substring (i -2 , i ));
15
+ if (group >=10 && group <=26 ) {
16
+ dp [i ]+=dp [i -2 ];
17
+ }
18
+ }
19
+ return dp [n ];
20
+ }
21
+ }
22
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxSubArray (int [] nums ) {
3
+ int curr = nums [0 ];
4
+ int maxSum = nums [0 ];
5
+
6
+ for (int i =1 ; i <nums .length ; i ++) {
7
+ curr = Math .max (nums [i ], curr +nums [i ]);
8
+ maxSum = Math .max (maxSum , curr );
9
+ }
10
+ return maxSum ;
11
+ }
12
+ }
13
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int hammingWeight (int n ) {
3
+ /*
4
+ * 시간복잡도 O(log n)
5
+ */
6
+ int cnt = 0 ;
7
+ while (n !=0 ) {
8
+ if (n %2 ==1 ) cnt ++;
9
+ n = n / 2 ;
10
+ }
11
+ return cnt ;
12
+ }
13
+ }
14
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /*
3
+ * 초반 풀이 시 while문 내에서 left<right 미체크로 오류 / 조건 추가
4
+ * 중복 while문 사용하지 않는 방법이 있는지 고민
5
+ *
6
+ * 시간복잡도 : O(n)
7
+ */
8
+ public boolean isPalindrome (String s ) {
9
+ int left = 0 ;
10
+ int right = s .length ()-1 ;
11
+
12
+ while (left < right ) {
13
+ while (left <right && !Character .isLetterOrDigit (s .charAt (left ))) {
14
+ left ++;
15
+ }
16
+ while (left <right && !Character .isLetterOrDigit (s .charAt (right ))) {
17
+ right --;
18
+ }
19
+
20
+ if (Character .toLowerCase (s .charAt (left )) != Character .toLowerCase (s .charAt (right ))) {
21
+ return false ;
22
+ }
23
+
24
+ left ++;
25
+ right --;
26
+ }
27
+ return true ;
28
+ }
29
+ }
30
+
You can’t perform that action at this time.
0 commit comments