From 15d4682468233070cc1ac3c6b0ae85c10089e008 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Tue, 15 Apr 2025 00:04:00 +0900 Subject: [PATCH 1/5] valid palindrome solution --- valid-palindrome/Yn3-3xh.java | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 valid-palindrome/Yn3-3xh.java diff --git a/valid-palindrome/Yn3-3xh.java b/valid-palindrome/Yn3-3xh.java new file mode 100644 index 000000000..7eb8624b6 --- /dev/null +++ b/valid-palindrome/Yn3-3xh.java @@ -0,0 +1,73 @@ +/** + [문제풀이] + - 대문자 -> 소문자 변환 + - 소문자가 아닌 문자면 넘어가고, 소문자만 체크 + - 숫자는 넘어갈 수 없다. + - 소문자 input, reverse input 비교 + + - 풀이1 + time: O(N), space: O(1) + class Solution { + public boolean isPalindrome(String s) { + if (s.trim().length() == 0) { + return true; + } + + s = s.toLowerCase(); + int left = 0; + int right = s.length() - 1; + while (left < right) { + while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { + left++; + } + while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { + right--; + } + + if (s.charAt(left) != s.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } + } + + - 풀이2 + time: O(N), space: O(1) + + [회고] + 중간에 공백을 replace하면 안좋을 것 같은데.. + -> 알파벳인지를 비교해서 인덱스를 바꾸자! + + for문만 고집하지 말고, while문도 사용해보자! + (처음에 for문으로 풀다가 스파게티가 되어버렸ㄷ..) + + !!! String.toLowerCase() 로 소문자로 변환하는 것이 좋은 방법일까?? !!! + */ + +class Solution { + public boolean isPalindrome(String s) { + s = s.toLowerCase(); + int left = 0; + int right = s.length() - 1; + while (left < right) { + if (!Character.isLetterOrDigit(s.charAt(left))) { + left++; + continue; + } + if (!Character.isLetterOrDigit(s.charAt(right))) { + right--; + continue; + } + + if (s.charAt(left) != s.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } +} From c826585f01ddb93157b0a73635402a61a0f85a63 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Tue, 15 Apr 2025 22:41:32 +0900 Subject: [PATCH 2/5] number of 1 bits solution --- number-of-1-bits/Yn3-3xh.java | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 number-of-1-bits/Yn3-3xh.java diff --git a/number-of-1-bits/Yn3-3xh.java b/number-of-1-bits/Yn3-3xh.java new file mode 100644 index 000000000..9c42adec1 --- /dev/null +++ b/number-of-1-bits/Yn3-3xh.java @@ -0,0 +1,39 @@ +/** + [문제풀이] + - 주어진 양의 정수를 2진수로 변환하여, 1의 개수 구하기 + + - 풀이 1 + time: O(log N), space: O(1) + class Solution { + public int hammingWeight(int n) { + int count = 0; + while (n > 0) { + if (n % 2 != 0) { + count++; + } + n /= 2; + } + return count; + } + } + - 풀이 2 + time: O(log N), space: O(1) + + [회고] + `n >> 1`비트 연산자는 `n / 2` 와 같다. + `n >> 1`로 풀면 비교도 비트로! + + 이진 표현에서 1의 개수를 세어주는 Integer.bitCount() 메서드도 있었다. + */ +class Solution { + public int hammingWeight(int n) { + int count = 0; + while (n > 0) { + if ((n & 1) == 1) { + count++; + } + n >>= 1; + } + return count; + } +} From f3f75a108b16db77a075d9f556bae37bb0772b6f Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Thu, 17 Apr 2025 19:59:15 +0900 Subject: [PATCH 3/5] combination sum solution --- combination-sum/Yn3-3xh.java | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 combination-sum/Yn3-3xh.java diff --git a/combination-sum/Yn3-3xh.java b/combination-sum/Yn3-3xh.java new file mode 100644 index 000000000..9d7ea3c44 --- /dev/null +++ b/combination-sum/Yn3-3xh.java @@ -0,0 +1,37 @@ +/** +[문제풀이] +- target 숫자를 주어진 배열로 나눠보면서 0이 되는 것들을 고르면 되지 않을까? +- dfs로 풀어보자. +time: O(2^N), space: O(N); + +[회고] +!!! dfs에서 기존 리스트가 이후에 변경될 수 있으므로 새 객체를 생성해서 넣어줘야 한다. !!! + +DP로도 풀어보려고 했지만, 솔루션을 봐도 내가 풀 수 없을 것 같았다.. + */ +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> results = new ArrayList<>(); + dfs(candidates, target, 0, results, new ArrayList<>(), 0); + return results; + } + + private void dfs(int[] candidates, int target, int index, List> results, List result, int sum) { + if (sum == target) { + results.add(new ArrayList<>(result)); + return; + } + + if (sum > target || index >= candidates.length) { + return; + } + + int num = candidates[index]; + result.add(num); + dfs(candidates, target, index, results, result, sum + num); + + result.remove(result.size() - 1); + dfs(candidates, target, index + 1, results, result, sum); + } +} + From 28137fde71b02bd04eb5e694056a84511e9129e5 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Fri, 18 Apr 2025 20:09:57 +0900 Subject: [PATCH 4/5] decode ways solution --- decode-ways/Yn3-3xh.java | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 decode-ways/Yn3-3xh.java diff --git a/decode-ways/Yn3-3xh.java b/decode-ways/Yn3-3xh.java new file mode 100644 index 000000000..9308d7e52 --- /dev/null +++ b/decode-ways/Yn3-3xh.java @@ -0,0 +1,58 @@ +/** +[문제풀이] +- 만들 수 있는 가지수를 모두 구하기 +- A ~ Z -> 1 ~ 26 +- DP로 풀어보자. +time: O(N), space: O(N) + 1212를 예시로, + 첫번째 수를 가져올 때 dp[1] = 1 + 만들어지는 수: 1 + + 한자리 수를 가져올 때 dp[2] = dp[1] = 1 + 만들어지는 수: 1, 2 + 두자리 수를 가져올 때 dp[2] = d[2] + dp[0] = 1 + 1 = 2 + 만들어지는 수: 12 + >> 1, 2 | 12 + + 한자리 수를 가져올 때 dp[3] = dp[2] = 2 + 만들어지는 수: 1, 2, 1 | 12, 1 + 두자리 수를 가져올 때 dp[3] = dp[3] + dp[1] = 2 + 1 = 3 + 만들어지는 수: 1, 21 + >> 1, 2, 1 | 12, 1 | 1, 21 + + 한자리 수를 가져올 때 dp[4] = dp[3] = 3 + 만들어지는 수: 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2 + 두자리 수를 가져올 때 dp[4] = dp[4] + dp[2] = 3 + 2 = 5 + 만들어지는 수: 1, 2, 12 | 12, 2 + >> 1, 2, 1, 2 | 12, 1, 2 | 1, 21, 2 | 1, 2, 12 | 12, 2 + +[회고] +dfs로 풀려다가 잘 안풀어져서 DP로 노선을 틀었는데, 아직 DP 구현이 모자른 것 같다.. + + */ +class Solution { + public int numDecodings(String s) { + if (s.charAt(0) == '0') { + return 0; + } + + int len = s.length(); + int[] dp = new int[len + 1]; + dp[0] = 1; + dp[1] = 1; + + for (int i = 2; i <= len; i++) { + int one = Character.getNumericValue(s.charAt(i - 1)); + int two = Integer.parseInt(s.substring(i - 2, i)); + + if (1 <= one && one <= 9) { + dp[i] = dp[i - 1]; + } + if (10 <= two && two <= 26) { + dp[i] += dp[i - 2]; + } + } + return dp[len]; + } +} + From a650368eba9093109f7f09bda0ab7d995091c401 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Fri, 18 Apr 2025 21:01:12 +0900 Subject: [PATCH 5/5] maximum subarray solution --- maximum-subarray/Yn3-3xh.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 maximum-subarray/Yn3-3xh.java diff --git a/maximum-subarray/Yn3-3xh.java b/maximum-subarray/Yn3-3xh.java new file mode 100644 index 000000000..fcd2db869 --- /dev/null +++ b/maximum-subarray/Yn3-3xh.java @@ -0,0 +1,23 @@ +/** +[문제풀이] +- 주어진 배열에서 연속된 수 배열의 합이 큰 수 구하기 + - (1) 현재수와 현재까지의 합 중 큰 수 구하기 : 현재 인덱스부터 시작되거나, 현재 인덱스까지 더해질 것 + - (2) 최댓값과 (1)번 수 중 큰 수 구하기 +time: O(N), space: O(1) + +[회고] +솔루션까지는 근접하는데, 결국 해결은 자꾸 안된다.. +어떻게 접근해야 솔루션까지 도달 할 수 있을까.. + */ +class Solution { + public int maxSubArray(int[] nums) { + int max = nums[0]; + int sum = nums[0]; + for (int i = 1; i < nums.length; i++) { + sum = Math.max(nums[i], sum + nums[i]); + max = Math.max(max, sum); + } + return max; + } +} +