Skip to content

Commit 0fcb972

Browse files
authored
Merge pull request #1307 from Tessa1217/main
[Tessa1217] Week 03 Solutions
2 parents 04ed987 + 452f931 commit 0fcb972

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

combination-sum/Tessa1217.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.List;
2+
import java.util.ArrayList;
3+
/**
4+
중복 되지 않은 요소들이 들어있는 candidates 배열이 주어지고 target 값이 주어진다.
5+
합이 target과 같은 중복되지 않은 조합을 모두 반환하시오.
6+
*/
7+
class Solution {
8+
9+
List<List<Integer>> answer = new ArrayList<>();
10+
11+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
12+
combination(0, candidates, target, 0, new ArrayList<>());
13+
return answer;
14+
}
15+
16+
// 시간복잡도 O(2^target)
17+
public void combination(int idx, int[] candidates, int target, int currentSum, List<Integer> comb) {
18+
// 누적 합이 넘으면
19+
if (currentSum > target) {
20+
return;
21+
}
22+
23+
// 누적 합이 타겟 값과 같으면
24+
if (currentSum == target) {
25+
answer.add(new ArrayList<>(comb));
26+
return;
27+
}
28+
29+
for (int i = idx; i < candidates.length; i++) {
30+
comb.add(candidates[i]);
31+
combination(i, candidates, target, currentSum + candidates[i], comb);
32+
comb.remove(comb.size() - 1);
33+
}
34+
}
35+
}
36+

decode-ways/Tessa1217.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
주어진 문자열을 복호화 할 수 있는 경우의 수를 반환하시오.
3+
문자열은 A-Z까지 숫자 1-26으로 치환
4+
예시: "AAJF" => (1, 1, 10, 6), (11, 10, 6)...
5+
*/
6+
class Solution {
7+
8+
// 시간복잡도: O(n), 공간복잡도: O(n)
9+
public int numDecodings(String s) {
10+
11+
int[] dp = new int[s.length() + 1];
12+
13+
// contain leading zero(s)
14+
if (s.charAt(0) == '0') {
15+
return 0;
16+
}
17+
18+
dp[0] = 1;
19+
dp[1] = 1;
20+
21+
for (int i = 2; i <= s.length(); i++) {
22+
23+
// 1자리수 검사
24+
int one = Integer.parseInt(Character.toString(s.charAt(i - 1)));
25+
26+
if (one != 0) {
27+
dp[i] += dp[i - 1];
28+
}
29+
30+
// 2자리수 검사
31+
int two = Integer.parseInt(Character.toString(s.charAt(i - 2))) * 10 + one;
32+
33+
if (two >= 10 && two <= 26) {
34+
dp[i] += dp[i - 2];
35+
}
36+
37+
}
38+
39+
return dp[s.length()];
40+
}
41+
42+
}
43+

maximum-subarray/Tessa1217.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
정수 배열이 주어질 때 부분 수열의 가장 큰 합을 구하시오.
3+
*/
4+
class Solution {
5+
6+
// 시간복잡도: O(n), 공간복잡도: O(1)
7+
public int maxSubArray(int[] nums) {
8+
9+
int sum = nums[0];
10+
11+
for (int i = 1; i < nums.length; i++) {
12+
// 수 이어서 더할지 아니면 현재 값으로 초기화할지 여부 판단
13+
nums[i] = Math.max(nums[i], nums[i] + nums[i - 1]);
14+
sum = Math.max(nums[i], sum);
15+
}
16+
17+
return sum;
18+
}
19+
20+
}
21+

number-of-1-bits/Tessa1217.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/** 주어진 숫자의 Hamming weight 구하기 */
2+
class Solution {
3+
4+
// 시간복잡도: O(1), 공간복잡도: O(1), 비트 연산자 사용
5+
public int hammingWeight(int n) {
6+
int count = 0;
7+
while (n != 0) {
8+
count += (n & 1);
9+
n >>>= 1;
10+
}
11+
return count;
12+
}
13+
14+
// 시간복잡도: O(1), 공간복잡도: O(1)
15+
// public int hammingWeight(int n) {
16+
17+
// int count = 0;
18+
// while (n != 0) {
19+
// if (n % 2 == 1) {
20+
// count++;
21+
// }
22+
// n /= 2;
23+
// }
24+
// return count;
25+
// }
26+
27+
// 시간복잡도: O(1), 공간복잡도: O(n)
28+
// public int hammingWeight(int n) {
29+
// return Integer.toBinaryString(n).replaceAll("0", "").length();
30+
// }
31+
}
32+

valid-palindrome/Tessa1217.java

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** 대문자 문자들을 소문자로 변환하고, 알파벳이 아닌 문자들을 제거했을 때 앞뒤가 똑같이 읽히는 구문을 palindrome이라고 한다.
2+
주어진 구문이 palindrome인지 여부를 확인하여 boolean 값을 반환하세요.
3+
*/
4+
class Solution {
5+
6+
// 투 포인터 활용 시간 복잡도: O(n), 공간복잡도: O(n)
7+
public boolean isPalindrome(String s) {
8+
9+
10+
int left = 0;
11+
int right = s.length() - 1;
12+
13+
while (left < right) {
14+
// 문자 또는 숫자 아닐 시
15+
while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {
16+
left++;
17+
}
18+
while (left < right && !Character.isLetterOrDigit(s.charAt(right))) {
19+
right--;
20+
}
21+
// 양 포인터 일치하지 않을 경우 리턴
22+
if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
23+
return false;
24+
}
25+
26+
left++;
27+
right--;
28+
29+
}
30+
return true;
31+
}
32+
33+
// char 배열 활용 풀이: 시간 복잡도: O(n), 공간복잡도: O(n)
34+
// public boolean isPalindrome(String s) {
35+
// // non-alphanumeric (숫자 포함 - test case "0P")
36+
// char[] convertArr = s.toLowerCase().replaceAll("[^a-z0-9]", "").toCharArray();
37+
// int maxIdx = convertArr.length - 1;
38+
// for (int i = 0; i <= maxIdx; i++) {
39+
// if (convertArr[i] != convertArr[maxIdx - i]) {
40+
// return false;
41+
// }
42+
// }
43+
// return true;
44+
// }
45+
46+
// StringBuffer reverse() 활용
47+
// public boolean isPalindrome(String s) {
48+
// // non-alphanumeric (숫자 포함 - test case "0P")
49+
// String convertedString = s.toLowerCase().replaceAll("[^a-z0-9]", "");
50+
// StringBuffer sb = new StringBuffer(convertedString);
51+
// return convertedString.equals(sb.reverse().toString());
52+
// }
53+
}
54+

0 commit comments

Comments
 (0)