Skip to content

[Ujoonnee] WEEK 03 solutions #1320

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

Closed
wants to merge 3 commits into from
Closed
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
28 changes: 28 additions & 0 deletions combination-sum/Ujoonnee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> output = new ArrayList<>();
Stack<Integer> nums = new Stack<>();
dfs(candidates, output, nums, target, 0, 0);

return output;
}

private void dfs(int[] candidates, List<List<Integer>> output, Stack<Integer> nums, int target, int start, int total) {
if (total > target) {
return;
}
if (total == target) {
output.add(new ArrayList<>(nums));
}
for (int i=start; i<candidates.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

start 변수로 이전 재귀에서 포함된 값에대한 조합은 건너뛰는게 되는군요!
불필요한 연산을 잘 줄이신것 같습니다👍👍

int num = candidates[i];
nums.push(num);
dfs(candidates, output, nums, target, i, total + num);
nums.pop();
}
}
}
19 changes: 19 additions & 0 deletions number-of-1-bits/Ujoonnee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int hammingWeight(int n) {
int answer = 0;
while (n > 0) {
answer += n & 1;
n >>= 1;
}

return answer;
}
}

/*
class Solution {
public int hammingWeight(int n) {
return Integer.toBinaryString(n).replace("0", "").length();
}
}
*/
51 changes: 51 additions & 0 deletions valid-palindrome/Ujoonnee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// time complexity : O(n)
// space complexity : O(1)

class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int i = 0;
int j = s.length() - 1;
while(i < j) {
char c1 = s.charAt(i);
if (!isAlphanumeric(c1)) {
i++;
continue;
}

char c2 = s.charAt(j);
if (!isAlphanumeric(c2)) {
j--;
continue;
}

if (c1 != c2) {
return false;
}
i++;
j--;
}

return true;
}

private boolean isAlphanumeric(char c) {
return (c >= '0' && c<='9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c<= 'z');
}
}

/*
class Solution {
public boolean isPalindrome(String s) {
Copy link
Contributor

Choose a reason for hiding this comment

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

주석처리 해주신 코드는 오답 기록용도로 쓰신걸까요?
아니면 좋은 예시를 담아주신건지 궁금합니다!

s = s.toLowerCase();
String[] split = s.replaceAll("[^A-Za-z0-9]", "").split("");
for (int i=0; i<split.length/2; i++) {
if (!split[i].equals(split[split.length-1-i])) {
return false;
}
}

return true;
}
}
*/