Skip to content

Commit a9a2e59

Browse files
authored
Merge pull request #1800 from ohgyulim/main
[ohgyulim] WEEK 03 solutions
2 parents 2ca3a2f + 94d7e5e commit a9a2e59

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

combination-sum/ohgyulim.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
List<List<Integer>> answer = new ArrayList<>();
5+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
6+
Arrays.sort(candidates);
7+
recur(new ArrayList<Integer>(), candidates, target, 0, 0);
8+
return answer;
9+
}
10+
11+
public void recur(List<Integer> result, int[] candidates, int target, int sum, int index) {
12+
if (sum == target) {
13+
List<Integer> deepCopyRes = new ArrayList<>(result);
14+
answer.add(deepCopyRes);
15+
return;
16+
}
17+
for (int i = index; i < candidates.length; i++) {
18+
if (sum + candidates[i] <= target) {
19+
result.add(candidates[i]);
20+
recur(result, candidates, target, sum + candidates[i], i);
21+
result.remove(Integer.valueOf(candidates[i]));
22+
}
23+
}
24+
}
25+
}

decode-ways/ohgyulim.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - for 루프: O(N)
4+
* 공간 복잡도: O(N), dp배열
5+
*/
6+
public int numDecodings(String s) {
7+
if (s.charAt(0) == '0') return 0;
8+
int[] dp = new int[s.length() + 1];
9+
dp[0] = 1;
10+
dp[1] = 1;
11+
12+
for (int i = 2; i <= s.length(); i++) {
13+
int one = Integer.parseInt(s.substring(i - 1, i));
14+
int two = Integer.parseInt(s.substring(i - 2, i));
15+
16+
if (one > 0) {
17+
dp[i] += dp[i - 1];
18+
}
19+
if (two >= 10 && two <= 26) {
20+
dp[i] += dp[i - 2];
21+
}
22+
}
23+
return dp[s.length()];
24+
}
25+
}
26+

maximum-subarray/ohgyulim.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - for 루프: O(N)
4+
* 공간 복잡도: O(N), dp배열
5+
*/
6+
public int maxSubArray(int[] nums) {
7+
int[] dp = new int[nums.length];
8+
dp[0] = nums[0];
9+
10+
int answer = nums[0];
11+
for (int i = 1; i < nums.length; i++) {
12+
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
13+
answer = Math.max(answer, dp[i]);
14+
}
15+
return answer;
16+
}
17+
}

number-of-1-bits/ohgyulim.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
/* 시간 복잡도: O(1)
3+
*
4+
* 공간 복잡도: O(1)
5+
*/
6+
public int hammingWeight(int n) {
7+
return Integer.bitCount(n);
8+
}
9+
}
10+

valid-palindrome/ohgyulim.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
/* 시간 복잡도: O(N)
3+
* - for 루프: O(N)
4+
*
5+
* 공간 복잡도: O(N), StringBuilder
6+
*/
7+
public boolean isPalindrome(String s) {
8+
StringBuilder sb = new StringBuilder();
9+
10+
for (char ch : s.toCharArray()) {
11+
if (Character.isLetterOrDigit(ch)) {
12+
sb.append(Character.toLowerCase(ch));
13+
}
14+
}
15+
16+
for (int i = 0; i < sb.length(); i++) {
17+
int left = i;
18+
int right = sb.length() - i - 1;
19+
20+
if (left >= right) return true;
21+
if (sb.charAt(left) != sb.charAt(right)) return false;
22+
}
23+
24+
return true;
25+
}
26+
}

0 commit comments

Comments
 (0)