Skip to content

Commit 86d5e72

Browse files
authored
[Yiseull] WEEK 03 Solutions (#2720)
* feat: 125. Valid Palindrome solution (cherry picked from commit d881c4d) * feat: 191. Number of 1 Bits solution * feat: 39. Combination Sum solution * feat: 191. Number of 1 Bits solution2 * feat: 91. Decode Ways solution * feat: 53. Maximum Subarray solution
1 parent cd08846 commit 86d5e72

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

combination-sum/Yiseull.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
3+
private List<List<Integer>> answer = new ArrayList<>();
4+
5+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
6+
Arrays.sort(candidates);
7+
dfs(candidates, target, 0, new ArrayList<>());
8+
return answer;
9+
}
10+
11+
private void dfs(int[] candidates, int target, int start, List<Integer> combination) {
12+
if (target == 0) {
13+
answer.add(new ArrayList<>(combination));
14+
return;
15+
}
16+
17+
for (int i = start; i < candidates.length; i++) {
18+
if (candidates[i] > target) {
19+
break;
20+
}
21+
22+
combination.add(candidates[i]);
23+
dfs(candidates, target - candidates[i], i, combination);
24+
combination.removeLast();
25+
}
26+
}
27+
}

decode-ways/Yiseull.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
3+
public int numDecodings(String s) {
4+
if (s.charAt(0) == '0') {
5+
return 0;
6+
}
7+
8+
int n = s.length();
9+
int[] dp = new int[n + 1];
10+
dp[0] = 1;
11+
dp[1] = 1;
12+
13+
for (int i = 2; i <= n; i++) {
14+
if (s.charAt(i - 1) != '0') {
15+
dp[i] += dp[i - 1];
16+
}
17+
18+
int two = Integer.parseInt(s.substring(i - 2, i));
19+
if (two >= 10 && two <= 26) {
20+
dp[i] += dp[i - 2];
21+
}
22+
}
23+
24+
return dp[n];
25+
}
26+
}

maximum-subarray/Yiseull.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
int answer = -10001, tmp = 0;
4+
5+
for (int i = 0; i < nums.length; i++) {
6+
tmp += nums[i];
7+
answer = Math.max(answer, tmp);
8+
tmp = Math.max(tmp, 0);
9+
}
10+
11+
return answer;
12+
}
13+
}

number-of-1-bits/Yiseull.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
// return Integer.bitCount(n);
4+
5+
int answer = 0;
6+
while (n != 0) {
7+
n &= n-1;
8+
answer++;
9+
}
10+
return answer;
11+
}
12+
}

valid-palindrome/Yiseull.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
/**
4+
첫 번째 풀이: 시간복잡도 O(n), 공간복잡도 O(n)
5+
String letters = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
6+
String reverse = new StringBuilder(letters).reverse().toString();
7+
return letters.equals(reverse);
8+
*/
9+
10+
// 두 번째 풀이: 시간복잡도 O(n), 공간복잡도 O(1)
11+
int left = 0, right = s.length() - 1;
12+
while (left < right) {
13+
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
14+
left++;
15+
}
16+
17+
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
18+
right--;
19+
}
20+
21+
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
22+
return false;
23+
}
24+
25+
left++;
26+
right--;
27+
}
28+
29+
return true;
30+
}
31+
}

0 commit comments

Comments
 (0)