-
-
Notifications
You must be signed in to change notification settings - Fork 339
[juhui-jeong] WEEK 03 Solutions #2441
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
Open
juhui-jeong
wants to merge
2
commits into
DaleStudy:main
Choose a base branch
from
juhui-jeong:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−0
Open
Changes from all commits
Commits
Show all changes
2 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,24 @@ | ||
| class Solution { | ||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| // dfs & 백트래킹 | ||
| List<List<Integer>> resultList = new ArrayList<>(); | ||
| dfs(candidates, target, 0, new ArrayList<>(), resultList); | ||
| return resultList; | ||
| } | ||
| private void dfs(int[] candidates, int target, int start, List<Integer> path, List<List<Integer>> resultList) { | ||
| if (target == 0) { | ||
| resultList.add(new ArrayList<>(path)); | ||
| return; | ||
| } | ||
|
|
||
| if (target < 0) { | ||
| return; | ||
| } | ||
|
|
||
| for(int i = start; i < candidates.length; i++) { | ||
| path.add(candidates[i]); | ||
| dfs(candidates, target - candidates[i], i, path, resultList); | ||
| path.remove(path.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,64 @@ | ||
| /* | ||
| // 첫번째 풀이, | ||
| // int -> string -> bit -> length | ||
| // 형변환을 하지 않고 하는 방법 필요 | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| String intConvertBit = String.format("%32s", Integer.toBinaryString(n)).replaceAll(" ", "0"); | ||
| return intConvertBit.replaceAll("0", "").length(); | ||
| } | ||
| } | ||
| // 32비트로 굳이 변환할 필요가 없는데..? | ||
| // 음수는 안들어오니, 비트 변환 후 바로 체크하여 반환 | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| return Integer.toBinaryString(n).replaceAll("0", "").length(); | ||
| } | ||
| } | ||
| // 두번째 풀이, | ||
| // length말고 counting으로? | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| String s = Integer.toBinaryString(n); | ||
| int count = 0; | ||
| for (char c: s.toCharArray()) { | ||
| if (c == '1') count++; | ||
| } | ||
| return count; | ||
| } | ||
| } | ||
| // 세번째 풀이 | ||
| // int형 그대로 사용(달레님 강의 참고하여 풀이) | ||
| // 10진수를 0이 될 때까지 계속 2로 나누고 나머지를 모두 연결한다. | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| int cnt = 0; | ||
| while (n > 0) { | ||
| cnt += n % 2; | ||
| n = n /2; | ||
| } | ||
| return cnt; | ||
| } | ||
| } | ||
| */ | ||
|
|
||
| // 네번째 풀이 | ||
| // 비트 조작을 통한 풀이 | ||
| // 마지막 비트가 1인지 확인 | ||
| // 비트를 오른쪽으로 한 칸 이동시키고, 왼쪽 빈자리를 0으로 채움 | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| int cnt = 0; | ||
| while (n > 0) { | ||
| cnt += (n & 1); | ||
| n >>>= 1; | ||
| } | ||
| 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,12 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| s = s.replaceAll("[^a-zA-Z0-9]", "").trim().toLowerCase(); | ||
| if (s.equals("")) return true; | ||
|
|
||
| String[] strArr = s.split(""); | ||
| for (int i = 0; i < strArr.length/2; i++) { | ||
| if (!strArr[i].equals(strArr[strArr.length-1-i])) return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
string 을 split 해서 배열로 사용하는 방법이있군요!