Skip to content

Commit d2532d5

Browse files
authored
Merge pull request #2441 from juhui-jeong/main
[juhui-jeong] WEEK 03 Solutions
2 parents 13a37fd + c8d52f3 commit d2532d5

3 files changed

Lines changed: 100 additions & 0 deletions

File tree

combination-sum/juhui-jeong.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
// dfs & 백트래킹
4+
List<List<Integer>> resultList = new ArrayList<>();
5+
dfs(candidates, target, 0, new ArrayList<>(), resultList);
6+
return resultList;
7+
}
8+
private void dfs(int[] candidates, int target, int start, List<Integer> path, List<List<Integer>> resultList) {
9+
if (target == 0) {
10+
resultList.add(new ArrayList<>(path));
11+
return;
12+
}
13+
14+
if (target < 0) {
15+
return;
16+
}
17+
18+
for(int i = start; i < candidates.length; i++) {
19+
path.add(candidates[i]);
20+
dfs(candidates, target - candidates[i], i, path, resultList);
21+
path.remove(path.size() -1);
22+
}
23+
}
24+
}

number-of-1-bits/juhui-jeong.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
// 첫번째 풀이,
3+
// int -> string -> bit -> length
4+
// 형변환을 하지 않고 하는 방법 필요
5+
class Solution {
6+
public int hammingWeight(int n) {
7+
String intConvertBit = String.format("%32s", Integer.toBinaryString(n)).replaceAll(" ", "0");
8+
9+
return intConvertBit.replaceAll("0", "").length();
10+
}
11+
}
12+
13+
// 32비트로 굳이 변환할 필요가 없는데..?
14+
// 음수는 안들어오니, 비트 변환 후 바로 체크하여 반환
15+
class Solution {
16+
public int hammingWeight(int n) {
17+
return Integer.toBinaryString(n).replaceAll("0", "").length();
18+
}
19+
}
20+
21+
// 두번째 풀이,
22+
// length말고 counting으로?
23+
class Solution {
24+
public int hammingWeight(int n) {
25+
String s = Integer.toBinaryString(n);
26+
27+
int count = 0;
28+
for (char c: s.toCharArray()) {
29+
if (c == '1') count++;
30+
}
31+
32+
return count;
33+
}
34+
}
35+
36+
// 세번째 풀이
37+
// int형 그대로 사용(달레님 강의 참고하여 풀이)
38+
// 10진수를 0이 될 때까지 계속 2로 나누고 나머지를 모두 연결한다.
39+
class Solution {
40+
public int hammingWeight(int n) {
41+
int cnt = 0;
42+
while (n > 0) {
43+
cnt += n % 2;
44+
n = n /2;
45+
}
46+
return cnt;
47+
}
48+
}
49+
*/
50+
51+
// 네번째 풀이
52+
// 비트 조작을 통한 풀이
53+
// 마지막 비트가 1인지 확인
54+
// 비트를 오른쪽으로 한 칸 이동시키고, 왼쪽 빈자리를 0으로 채움
55+
class Solution {
56+
public int hammingWeight(int n) {
57+
int cnt = 0;
58+
while (n > 0) {
59+
cnt += (n & 1);
60+
n >>>= 1;
61+
}
62+
return cnt;
63+
}
64+
}

valid-palindrome/juhui-jeong.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
s = s.replaceAll("[^a-zA-Z0-9]", "").trim().toLowerCase();
4+
if (s.equals("")) return true;
5+
6+
String[] strArr = s.split("");
7+
for (int i = 0; i < strArr.length/2; i++) {
8+
if (!strArr[i].equals(strArr[strArr.length-1-i])) return false;
9+
}
10+
return true;
11+
}
12+
}

0 commit comments

Comments
 (0)