-
-
Notifications
You must be signed in to change notification settings - Fork 361
[essaysir] WEEK 03 solutions #2717
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import java.util.*; | ||
|
|
||
| class Solution { | ||
| public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
| // 합이 target 이 되는 경우의 수를 모두 고르시오 | ||
| // target 이 150 이하 | ||
| List<List<Integer>> result = new ArrayList<>(); | ||
| List<Integer> current = new ArrayList<>(); | ||
|
|
||
| backTracking(candidates, target, 0 , result, current); | ||
| return result; | ||
| } | ||
|
|
||
| public void backTracking(int[] candidates, int remain, int start, List<List<Integer>> result, List<Integer> current) { | ||
| if ( remain == 0 ){ | ||
| result.add(new ArrayList<>(current)); | ||
| return; | ||
| } | ||
|
|
||
| if ( remain < 0 ){ | ||
| return; | ||
| } | ||
|
|
||
| for (int i = start; i < candidates.length; i++) { | ||
| current.add(candidates[i]); | ||
|
|
||
| backTracking(candidates, remain - candidates[i], i , result, current); | ||
|
|
||
| current.remove(current.size() - 1); | ||
|
Contributor
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. removeLast() 사용하셔도 될 것 같습니다 |
||
| } | ||
| } | ||
| } | ||
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: Java의 내장 메서드 Integer.bitCount를 이용해 비트를 셉니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution { | ||
| public int hammingWeight(int n) { | ||
| // 이진수로 바꾸고, 1 이 몇개 있는 지 파악한다. | ||
| return Integer.bitCount(n); | ||
|
Contributor
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. bitCount로 바로 풀리긴 하는데 알고리즘 풀이 목적으로는 직접 구현해보시는게 좋을 것 같아요. 추가로 bitCount에 대한 내부 구현도 안 보셨다면 이해해보시면 좋을 것 같습니다. |
||
|
|
||
| // 추가 풀이. | ||
| // String ans = Integer.toBinaryString(n).replace("0",""); | ||
| // return ans.length(); | ||
| } | ||
| } | ||
|
|
||
|
Contributor
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 필터링과 정규화 후 양 끝에서 대칭 비교로 팰린드롬 여부를 판단합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution { | ||
| public boolean isPalindrome(String s) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (char c : s.toCharArray()) { | ||
| if (Character.isLetterOrDigit(c)) { | ||
| sb.append(Character.toLowerCase(c)); | ||
| } | ||
| } | ||
|
Comment on lines
+3
to
+8
Contributor
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. StringBuilder를 쓰셨군요. 이 방식은 공간 복잡도 O(n) 을 필요로 하는데요. 별도의 공간을 사용하지 않고 투 포인터로도 풀어보셔도 좋을 거 같습니다. |
||
|
|
||
| String result = sb.toString(); | ||
| char[] x = result.toCharArray(); | ||
|
Comment on lines
+10
to
+11
Contributor
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. result 변수는 인라인 해도 될 거 같습니다. |
||
| for (int i = 0; i < x.length / 2; i++) { | ||
| if (x[i] != x[x.length - i - 1]) { | ||
| return false; | ||
| } | ||
| } | ||
|
Contributor
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. 변수명 x는 가독성이 안 좋아서 의미 있는 이름을 사용하시는게 좋을 거 같습니다. |
||
| return true; | ||
| } | ||
| } | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 백트래킹으로 각 후보를 재귀적으로 선택하거나 선택하지 않으며, 중복 없이 시작 인덱스(start)로 조합을 구성합니다.
개선 제안: 현재 구현이 적절해 보입니다.