Skip to content

Commit e9a4523

Browse files
authored
Merge pull request #1304 from jinhyungrhee/week3
[jinhyungrhee] Week 03 solutions
2 parents 5376245 + 9d49851 commit e9a4523

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

combination-sum/jinhyungrhee.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
class Solution {
3+
public List<List<Integer>> output;
4+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
5+
output = new ArrayList<>();
6+
Deque<Integer> stack = new ArrayDeque<>();
7+
dfs(candidates, target, stack, 0, 0);
8+
return output;
9+
10+
}
11+
12+
public void dfs(int[] candidates, int target, Deque<Integer> stack, int start, int total) {
13+
14+
if (total > target) return;
15+
16+
if (total == target) {
17+
output.add(new ArrayList<>(stack));
18+
return;
19+
}
20+
21+
for (int i = start; i < candidates.length; i++) {
22+
int num = candidates[i];
23+
stack.push(num);
24+
dfs(candidates, target, stack, i, total + num);
25+
stack.pop();
26+
}
27+
}
28+
}

decode-ways/jinhyungrhee.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class Solution {
3+
4+
public List<Integer> nums;
5+
public int numDecodings(String s) {
6+
nums = new ArrayList<>();
7+
for (int i = 0; i < s.length(); i++) {
8+
nums.add(s.charAt(i) - 48);
9+
}
10+
11+
int[] memo = new int[nums.size() + 1];
12+
Arrays.fill(memo, -1);
13+
return dfs(0, memo);
14+
}
15+
16+
public int dfs(int start, int[] memo) {
17+
18+
if (start == nums.size()) return 1;
19+
20+
if (memo[start] != -1) {
21+
return memo[start];
22+
}
23+
if (nums.get(start) == 0) {
24+
memo[start] = 0;
25+
}
26+
else if (start + 1 < nums.size() && nums.get(start) * 10 + nums.get(start + 1) < 27) {
27+
memo[start] = dfs(start + 1, memo) + dfs(start + 2, memo);
28+
} else {
29+
memo[start] = dfs(start + 1, memo);
30+
}
31+
return memo[start];
32+
}
33+
}

maximum-subarray/jinhyungrhee.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
4+
int[] dp = new int[nums.length];
5+
dp[0] = nums[0];
6+
for (int i = 1; i < nums.length; i++) {
7+
dp[i] = Math.max(dp[i-1]+nums[i], nums[i]);
8+
}
9+
10+
int maxVal = -987654321;
11+
for (int num : dp) {
12+
if (num > maxVal) {
13+
maxVal = num;
14+
}
15+
}
16+
return maxVal;
17+
}
18+
}

number-of-1-bits/jinhyungrhee.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int hammingWeight(int n) {
3+
4+
String bin = "";
5+
while(n != 1) {
6+
bin += (n % 2);
7+
n /= 2;
8+
}
9+
bin += "1";
10+
11+
int result = 0;
12+
for (int i = 0; i < bin.length(); i++) {
13+
if (bin.charAt(i) == '1') result++;
14+
}
15+
return result;
16+
}
17+
}

valid-palindrome/jinhyungrhee.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
4+
int len = s.length();
5+
6+
String target = "";
7+
for (int i = 0; i < len; i++) {
8+
if (Character.isLetterOrDigit(s.charAt(i))) {
9+
target += Character.toLowerCase(s.charAt(i));
10+
}
11+
}
12+
13+
String reversedTarget = "";
14+
for (int i = len - 1; i >= 0; i--) {
15+
if (Character.isLetterOrDigit(s.charAt(i))) {
16+
reversedTarget += Character.toLowerCase(s.charAt(i));
17+
}
18+
}
19+
20+
if (target.length() == 0) return true;
21+
for (int i = 0; i < target.length(); i++) {
22+
if (target.charAt(i) != reversedTarget.charAt(i)) return false;
23+
}
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)