diff --git a/combination-sum/hoonjichoi1.java b/combination-sum/hoonjichoi1.java new file mode 100644 index 0000000000..00cd9d313b --- /dev/null +++ b/combination-sum/hoonjichoi1.java @@ -0,0 +1,35 @@ +/* +Time Complexity : O(c^t) +Space Complexity : O(t) + */ + +class Solution { + + public List> combinationSum(int[] candidates, int target) { + List> output = new ArrayList<>(); + Stack nums = new Stack<>(); + dfs (candidates, output, target, nums, 0, 0); + return output; + } + + private void dfs(int[] candidates, List> output, int target, Stack nums, int start, int total) { + // base case : pass + if (target == total) { + output.add(new ArrayList<>(nums)); + return; + } + // base case : fail + if (target < total) { + return; + } + + for (int i = start ; i < candidates.length ; i++) { + int num = candidates[i]; + nums.push(num); + dfs(candidates, output, target, nums, i, total + num); + nums.pop(); + } + + + } +} diff --git a/number-of-1-bits/hoonjichoi1.java b/number-of-1-bits/hoonjichoi1.java new file mode 100644 index 0000000000..7bc11fec2f --- /dev/null +++ b/number-of-1-bits/hoonjichoi1.java @@ -0,0 +1,19 @@ +class Solution { + public int hammingWeight(int n) { + + if (n == 1) { + return 1; + } + + int curr = n, result = 1; + while (curr > 1) { + if (curr % 2 == 1) { + result++; + } + curr = curr / 2; + } + + return result; + } +} + diff --git a/valid-palindrome/hoonjichoi1.java b/valid-palindrome/hoonjichoi1.java new file mode 100644 index 0000000000..be60940a0e --- /dev/null +++ b/valid-palindrome/hoonjichoi1.java @@ -0,0 +1,23 @@ +class Solution { + public boolean isPalindrome(String s) { + + // removing all non-alphanumeric characters and convert them to lower case + String conveted = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); + + // early return of the empty string case + if (conveted.length() == 0) { + return true; + } + + // check the symmetry + int left = 0, right = conveted.length() - 1; + while (left <= right) { + if (conveted.charAt(left) != conveted.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + } +}