-
-
Notifications
You must be signed in to change notification settings - Fork 247
[ohgyulim] WEEK 03 solutions #1800
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
6 commits
Select commit
Hold shift + click to select a range
7cd50c8
valid-palindrome solution
ohgyulim 17666b7
valid-palindrome solution
ohgyulim 06ecedd
number-of-1-bits solution
ohgyulim e03134f
combination-sum solution
ohgyulim 81cb39f
decode-ways solution
ohgyulim 94d7e5e
maximum-subarray solution
ohgyulim 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,25 @@ | ||
import java.util.*; | ||
|
||
class Solution { | ||
List<List<Integer>> answer = new ArrayList<>(); | ||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
Arrays.sort(candidates); | ||
recur(new ArrayList<Integer>(), candidates, target, 0, 0); | ||
return answer; | ||
} | ||
|
||
public void recur(List<Integer> result, int[] candidates, int target, int sum, int index) { | ||
if (sum == target) { | ||
List<Integer> deepCopyRes = new ArrayList<>(result); | ||
answer.add(deepCopyRes); | ||
return; | ||
} | ||
for (int i = index; i < candidates.length; i++) { | ||
if (sum + candidates[i] <= target) { | ||
result.add(candidates[i]); | ||
recur(result, candidates, target, sum + candidates[i], i); | ||
result.remove(Integer.valueOf(candidates[i])); | ||
} | ||
} | ||
} | ||
} | ||
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 @@ | ||
class Solution { | ||
/* 시간 복잡도: O(N) | ||
* - for 루프: O(N) | ||
* 공간 복잡도: O(N), dp배열 | ||
*/ | ||
public int numDecodings(String s) { | ||
if (s.charAt(0) == '0') return 0; | ||
int[] dp = new int[s.length() + 1]; | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (int i = 2; i <= s.length(); i++) { | ||
int one = Integer.parseInt(s.substring(i - 1, i)); | ||
int two = Integer.parseInt(s.substring(i - 2, i)); | ||
|
||
if (one > 0) { | ||
dp[i] += dp[i - 1]; | ||
} | ||
if (two >= 10 && two <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
return dp[s.length()]; | ||
} | ||
} | ||
|
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,17 @@ | ||
class Solution { | ||
/* 시간 복잡도: O(N) | ||
* - for 루프: O(N) | ||
* 공간 복잡도: O(N), dp배열 | ||
*/ | ||
public int maxSubArray(int[] nums) { | ||
int[] dp = new int[nums.length]; | ||
dp[0] = nums[0]; | ||
|
||
int answer = nums[0]; | ||
for (int i = 1; i < nums.length; i++) { | ||
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); | ||
answer = Math.max(answer, dp[i]); | ||
} | ||
return answer; | ||
} | ||
} |
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,10 @@ | ||
class Solution { | ||
/* 시간 복잡도: O(1) | ||
* | ||
* 공간 복잡도: O(1) | ||
*/ | ||
public int hammingWeight(int n) { | ||
return Integer.bitCount(n); | ||
} | ||
Comment on lines
+6
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바는 이렇게 내장함수로 풀 수있는 방법이 되게 좋네요. 다만 꼭 내장함수 말고도 풀수있는 방법이 있다면 고민해보시는것도 좋을거 같아요. |
||
} | ||
|
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 @@ | ||
class Solution { | ||
/* 시간 복잡도: O(N) | ||
* - for 루프: O(N) | ||
* | ||
* 공간 복잡도: O(N), StringBuilder | ||
*/ | ||
public boolean isPalindrome(String s) { | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (char ch : s.toCharArray()) { | ||
if (Character.isLetterOrDigit(ch)) { | ||
sb.append(Character.toLowerCase(ch)); | ||
} | ||
} | ||
|
||
for (int i = 0; i < sb.length(); i++) { | ||
int left = i; | ||
int right = sb.length() - i - 1; | ||
|
||
if (left >= right) return true; | ||
if (sb.charAt(left) != sb.charAt(right)) return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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.
백트래킹으로 깔끔하게 잘 푸신거 같습니다.