Skip to content

Commit cd08846

Browse files
authored
[hoonjichoi1] WEEK 03 solutions (#2716)
* valid-palindrom solution * number-of-1-bits solution * combination-sum solution * decode-ways solution
1 parent 80e2e17 commit cd08846

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

combination-sum/hoonjichoi1.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Time Complexity : O(c^t)
3+
Space Complexity : O(t)
4+
*/
5+
6+
class Solution {
7+
8+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
9+
List<List<Integer>> output = new ArrayList<>();
10+
Stack<Integer> nums = new Stack<>();
11+
dfs (candidates, output, target, nums, 0, 0);
12+
return output;
13+
}
14+
15+
private void dfs(int[] candidates, List<List<Integer>> output, int target, Stack<Integer> nums, int start, int total) {
16+
// base case : pass
17+
if (target == total) {
18+
output.add(new ArrayList<>(nums));
19+
return;
20+
}
21+
// base case : fail
22+
if (target < total) {
23+
return;
24+
}
25+
26+
for (int i = start ; i < candidates.length ; i++) {
27+
int num = candidates[i];
28+
nums.push(num);
29+
dfs(candidates, output, target, nums, i, total + num);
30+
nums.pop();
31+
}
32+
33+
34+
}
35+
}

decode-ways/hoonjichoi1.java

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

number-of-1-bits/hoonjichoi1.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
4+
if (n == 1) {
5+
return 1;
6+
}
7+
8+
int curr = n, result = 1;
9+
while (curr > 1) {
10+
if (curr % 2 == 1) {
11+
result++;
12+
}
13+
curr = curr / 2;
14+
}
15+
16+
return result;
17+
}
18+
}
19+

valid-palindrome/hoonjichoi1.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
4+
// removing all non-alphanumeric characters and convert them to lower case
5+
String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
6+
7+
// early return of the empty string case
8+
if (conveted.length() == 0) {
9+
return true;
10+
}
11+
12+
// check the symmetry
13+
int left = 0, right = conveted.length() - 1;
14+
while (left <= right) {
15+
if (conveted.charAt(left) != conveted.charAt(right)) {
16+
return false;
17+
}
18+
left++;
19+
right--;
20+
}
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)