Skip to content
Merged
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
26 changes: 26 additions & 0 deletions combination-sum/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
Arrays.sort(candidates); //불필요한 탐색을 줄이기 위해 sorting
dfs(0, candidates, target, new ArrayList<>(), answer);
return answer;

}

public void dfs(int start, int[] candidates, int target, List<Integer> curList, List<List<Integer>> answer) {
if (target == 0) {
answer.add(new ArrayList<>(curList));
return;
}
for(int i = start; i < candidates.length; i++) {
if (candidates[i] > target) {
break;
}

curList.add(candidates[i]);
dfs(i, candidates, target - candidates[i], curList, answer);
curList.remove(curList.size()-1); //다음 경우의 수를 위해 backtracking

}
}
}
45 changes: 45 additions & 0 deletions decode-ways/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
public int numDecodings(String s) {
/**
1.문제: decode 할 수 있는 모든 경우의 수 return
2.조건:
- 여러 개의 경우의 수 있을 수 있다.
- decode 불가능한 경우가 있을 수 있다.
- 모두 불가능한 경우 return 0
3.풀이
- dp (해석할 수 있는 방법 수를 누적해서 더함)
e.g 226
0자리 : dp[0] = 0
1자리(2) : 2 -> dp]1] = 1
2자리(22): 2(B), 22(v) -> dp[2] = 2
3자리(226): (2, 2, 6), (22, 6), (2, 26) -> dp[3] = 3

time complexity: O(N)
space complexity: O(N)

*/
if (s.charAt(0) == '0') {
return 0;
}
int n = s.length();
int[] dp = new int[n+1];
dp[0] = 1; //아무것도 없는 상태
dp[1] = 1;

for(int i = 2; i<=n; i++) {
//1자리수
if (s.charAt(i-1) != '0') {
dp[i] += dp[i-1];
}
//2자리수
if(i > 1) {
int number = Integer.parseInt(s.substring(i-2, i));
//2자리 수는 10 ~ 26사이만 가능
if (number >= 10 && number <= 26) {
dp[i] += dp[i-2];
}
}
}
return dp[n];
}
}
23 changes: 23 additions & 0 deletions maximum-subarray/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public int maxSubArray(int[] nums) {
/**
1. 문제: 가장 큰 합을 가지는 subarray 의 sum 을 반환
2. 조건: 원소값은 음수 ~ 양수, 배열 최대 길이 = 10^5, 최소 길이 = 1
- time complexity: O(N)
- space complexity: O(1)
*/

if (nums.length == 1) {
return nums[0];
}

int maxSum = nums[0]; //전체 최대
int curSum = nums[0]; //현재 합

for(int i = 1; i<nums.length; i++) {
curSum = Math.max(nums[i], curSum + nums[i]);
maxSum = Math.max(maxSum, curSum);
}
return maxSum;
}
}
23 changes: 23 additions & 0 deletions number-of-1-bits/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public int hammingWeight(int n) {
/**
1.문제: 이진수 변환 후 1의 개수 반환
2. 2로 나누면서 나머지가 1일 경우 count+=1
*/
int count = 0;
// while(n > 0) {
// if (n % 2 != 0) {
// count += 1;
// }
// n = n / 2;
// }

while(n > 0) {
if ((n & 1) == 1) {
count += 1;
}
n >>= 1;
}
return count;
}
}
40 changes: 40 additions & 0 deletions valid-palindrome/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public boolean isPalindrome(String s) {
/**
1. 문제: 팰린드롬인지 판단하는 문제.
2. 조건: 알파벳 소문자로 변환, 문자 또는 숫자가 아닌건 모두 제거 (공백, 쉼표 등))
- left, right index 이중 포인터로 풀이
time complexity : O(n)
space complexity : O(1)
*/
boolean answer = true;
//문자열 추가하면 space complexity : O(n)
//s = s.toLowerCase();
//s = s.replaceAll("[^0-9a-z]", "");
int left = 0 ;
int right = s.length() - 1;

while(left < right) {
char l = s.charAt(left);
char r = s.charAt(right);

//왼쪽이 알파벳 or 숫자가 아니면 skip
if (!Character.isLetterOrDigit(l)) {
left += 1;
continue;
}
//오른쪽이 알파벳 or 숫자가 아니면 skip
if (!Character.isLetterOrDigit(r)) {
right -= 1;
continue;
}
if (Character.toLowerCase(l) != Character.toLowerCase(r) ) {
return false;
}
left += 1;
right -= 1;
}

return answer;
}
}
Loading