diff --git a/3sum/std-freejia.java b/3sum/std-freejia.java new file mode 100644 index 000000000..450fe57c8 --- /dev/null +++ b/3sum/std-freejia.java @@ -0,0 +1,31 @@ +class Solution { + public List> threeSum(int[] nums) { + + List> answer = new ArrayList<>(); + Arrays.sort(nums); + int len = nums.length; + + for (int i = 0; i < len - 2; i++) { + // 인접한 같은 수라면, 지나감 + if (i > 0 && nums[i] == nums[i-1]) continue; + + int L = i + 1, H = len - 1; + + while(L < H) { + if (nums[L] + nums[H] + nums[i] > 0) { + H--; + } else if (nums[L] + nums[H] + nums[i] < 0) { + L++; + } else { + answer.add(Arrays.asList(nums[i], nums[L], nums[H])); + // 중복을 제거 + while (L < H && nums[L] == nums[L+1]) L++; + while (L < H && nums[H] == nums[H-1]) H--; + L++; + H--; + } + } + } + return answer; + } +} diff --git a/climbing-stairs/std-freejia.java b/climbing-stairs/std-freejia.java new file mode 100644 index 000000000..e15f361ac --- /dev/null +++ b/climbing-stairs/std-freejia.java @@ -0,0 +1,14 @@ +class Solution { + public int climbStairs(int n) { + int[] dp = new int [n + 1]; + + if (n == 1) return 1; + dp[1] = 1; + dp[2] = 2; + + for (int i = 3; i <= n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n]; + } +} diff --git a/product-of-array-except-self/std-freejia.java b/product-of-array-except-self/std-freejia.java new file mode 100644 index 000000000..806a35005 --- /dev/null +++ b/product-of-array-except-self/std-freejia.java @@ -0,0 +1,23 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + int len = nums.length; + int[] res = new int[len]; + int num = 1; + + for (int i = 0; i < len; i++) { + res[i] = num; + num *= nums[i]; + } + num = 1; + for (int i = len -1; i>=0; i--) { + res[i] *= num; + num *= nums[i]; + } + + for (int i = 0; i < len; i++) { + System.out.print( res[i] + " "); + } + + return res; +} +} diff --git a/valid-anagram/std-freejia.java b/valid-anagram/std-freejia.java new file mode 100644 index 000000000..8ca887b3e --- /dev/null +++ b/valid-anagram/std-freejia.java @@ -0,0 +1,14 @@ +class Solution { + public boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + + Map mapS = new HashMap<>(); + Map mapT = new HashMap<>(); + + for (int i = 0; i < s.length(); i++) { + mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i), 0) + 1); + mapT.put(t.charAt(i), mapT.getOrDefault(t.charAt(i), 0) + 1); + } + return mapS.equals(mapT); + } +}