Skip to content

[sora0319] Week 03 solutions #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions combination-sum/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.*;
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> output = new ArrayList<>();
List<Integer> sum = new ArrayList<>();
checkTarget(candidates, target, 0, output, sum);
return output;
}

private void checkTarget(int[] candidates, int target, int start, List<List<Integer>> output, List<Integer> sum) {
if (target == 0) {
output.add(new ArrayList<>(sum));
return;
}

for (int i = start; i < candidates.length; i++) {
int current = candidates[i];
if (current > target) continue;

sum.add(current);
checkTarget(candidates, target - current, i, output, sum);
sum.remove(sum.size() - 1);
}
}
}

24 changes: 24 additions & 0 deletions decode-ways/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
public int numDecodings(String s) {
if (s == null || s.length() == 0 || s.charAt(0) == '0') return 0;

int n = s.length();
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= n; i++) {
if (s.charAt(i - 1) != '0') {
dp[i] += dp[i - 1];
}

int twoNum = Integer.parseInt(s.substring(i - 2, i));
if (twoNum >= 10 && twoNum <= 26) {
dp[i] += dp[i - 2];
}
}

return dp[n];
}
}

18 changes: 18 additions & 0 deletions maximum-subarray/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int maxSubArray(int[] nums) {
int n = nums.length;
int[] dp = new int[n];

dp[0] = nums[0];
int maxSum = dp[0];

for (int i = 1; i < n; i++) {
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
maxSum = Math.max(maxSum, dp[i]); // 최대값 갱신
}

return maxSum;
}

}

12 changes: 12 additions & 0 deletions number-of-1-bits/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public int hammingWeight(int n) {
int cnt = 0;
String binary = Integer.toBinaryString(n);

for(int i = 0; i < binary.length(); i++){
if(binary.charAt(i) == '1') cnt++;
}
return cnt;
}
}

27 changes: 27 additions & 0 deletions valid-palindrome/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public boolean isPalindrome(String s) {
int start = 0;
int end = s.length()-1;

boolean isPalindrome = true;
s = s.toLowerCase();

while(start <= end){
if((s.charAt(start) < 'a' || s.charAt(start) > 'z') && (s.charAt(start) < '0' || s.charAt(start) > '9')){
start++;
continue;
}
if((s.charAt(end) < 'a' || s.charAt(end) > 'z') && (s.charAt(end) < '0' || s.charAt(end) > '9')){
end--;
continue;
}
if(s.charAt(start) != s.charAt(end)) return false;
start++;
end--;

}

return isPalindrome;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. 여기서 isPalindrome이 바뀌는경우가 없는것같아서요.
그냥 return true로 했어도 될것같습니다.

}
}