Skip to content

Commit 60c4a8b

Browse files
authored
Merge pull request #1327 from sora0319/main
[sora0319] Week 03 solutions
2 parents 8a74f65 + fd1f2f9 commit 60c4a8b

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

combination-sum/sora0319.java

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

decode-ways/sora0319.java

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

maximum-subarray/sora0319.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+
int n = nums.length;
4+
int[] dp = new int[n];
5+
6+
dp[0] = nums[0];
7+
int maxSum = dp[0];
8+
9+
for (int i = 1; i < n; i++) {
10+
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
11+
maxSum = Math.max(maxSum, dp[i]); // 최대값 갱신
12+
}
13+
14+
return maxSum;
15+
}
16+
17+
}
18+

number-of-1-bits/sora0319.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+
int cnt = 0;
4+
String binary = Integer.toBinaryString(n);
5+
6+
for(int i = 0; i < binary.length(); i++){
7+
if(binary.charAt(i) == '1') cnt++;
8+
}
9+
return cnt;
10+
}
11+
}
12+

valid-palindrome/sora0319.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
int start = 0;
4+
int end = s.length()-1;
5+
6+
boolean isPalindrome = true;
7+
s = s.toLowerCase();
8+
9+
while(start <= end){
10+
if((s.charAt(start) < 'a' || s.charAt(start) > 'z') && (s.charAt(start) < '0' || s.charAt(start) > '9')){
11+
start++;
12+
continue;
13+
}
14+
if((s.charAt(end) < 'a' || s.charAt(end) > 'z') && (s.charAt(end) < '0' || s.charAt(end) > '9')){
15+
end--;
16+
continue;
17+
}
18+
if(s.charAt(start) != s.charAt(end)) return false;
19+
start++;
20+
end--;
21+
22+
}
23+
24+
return isPalindrome;
25+
}
26+
}
27+

0 commit comments

Comments
 (0)