-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
음.. 여기서 isPalindrome이 바뀌는경우가 없는것같아서요.
그냥 return true로 했어도 될것같습니다.