From e686121fe601b68816976f2b3b4bb352b31fcf21 Mon Sep 17 00:00:00 2001 From: Subin Kim <70996958+green-study@users.noreply.github.com> Date: Mon, 17 Oct 2022 14:50:33 +0900 Subject: [PATCH 01/12] Added 015_3Sum.java (#67) * Added 015_3Sum.java Contributed by @green-study --- java/015_3Sum.java | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 java/015_3Sum.java diff --git a/java/015_3Sum.java b/java/015_3Sum.java new file mode 100644 index 0000000..cc0a069 --- /dev/null +++ b/java/015_3Sum.java @@ -0,0 +1,47 @@ +//015. 3Sum +class Solution { + public List> threeSum(int[] nums) { + //create result list to store i,j,k + List> result = new LinkedList>(); + + //sorting nums + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 2; i++) { + + int left = i + 1; + int right = nums.length - 1; + + if (i > 0 && nums[i] == nums[i-1]) { + continue; //if nums have same numbers, just check one time. + } + + while (left < right) { + int sum = nums[left] + nums[right] + nums[i]; + + if (sum == 0) { + //if sum == 0, store i,j,k + result.add(Arrays.asList(nums[i], nums[left], nums[right])); + left++; //check anoter case + right--; + //if next number == now number + while (nums[left] == nums[left - 1] && left < right) { + left++; + } + while (nums[right] == nums[right + 1] && left < right) { + right--; + } + } else if (sum > 0) { + //if sum > 0, right--; + right--; + } else { + //if sum < 0, left++; + left++; + } + } + } + + return result; //return result list + } +} + From fefe70c3ce3fee059a5421fcb27333e431fea8d9 Mon Sep 17 00:00:00 2001 From: LONGNEW <40235475+LONGNEW@users.noreply.github.com> Date: Sun, 30 Oct 2022 12:24:08 +0900 Subject: [PATCH 02/12] Add 523_Continuous_Subarray_Sum.py (#68) * Add 523_Continuous_Subarray_Sum.py Contributed by @LONGNEW --- README.md | 1 + python/523_Continuous_Subarray_Sum.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 python/523_Continuous_Subarray_Sum.py diff --git a/README.md b/README.md index df026f7..c34ae3b 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/482_License_Key_Formatting.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/482_License_Key_Formatting.java) | String processing, lower and len % K, O(n) and O(n) | | 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/485_Max_Consecutive_Ones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/485_Max_Consecutive_Ones.java) | Add one when encounter 1, set to 0 when encounter 0, O(n) and O(1) | | 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/509_Fibonacci_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/509_Fibonacci_Number.java) | 1. Recursive, O(n)
2. DP with memo, O(n). Note that N<=30, which means that we can keep a memo from 0 to 30. | +| 523 | [Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/523_Continuous_Subarray_Sum.py) | O(n) solution using dict() | | 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.
2. Stack 3. Reverse Morris In-order Traversal | | 541 | [Reverse String II](https://leetcode.com/problems/reverse-string-ii/solution/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/541_Reverse_String_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/541_Reverse_String_II.java) | Handle each 2k until reaching end, On(n) and O(n) | | 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer | diff --git a/python/523_Continuous_Subarray_Sum.py b/python/523_Continuous_Subarray_Sum.py new file mode 100644 index 0000000..d37ceeb --- /dev/null +++ b/python/523_Continuous_Subarray_Sum.py @@ -0,0 +1,20 @@ +class Solution: + def checkSubarraySum(self, nums: List[int], k: int) -> bool: + # remeainders[0] = 0 is for when x == 0 + remainders = dict() + remainders[0] = 0 + pre_sum = 0 + + for idx, item in enumerate(nums): + pre_sum += item + remaind = pre_sum % k + + # remainder doesnt exist then it has to be init + # if it exists, then check the prev one has the same remainder + if remaind not in remainders: + remainders[remaind] = idx + 1 + elif remainders[remaind] < idx: + return True + + return False + From 67a842233a5d2d8b13330eb9128c62414d88669f Mon Sep 17 00:00:00 2001 From: Sitesh Pattanaik <12951458+spattk@users.noreply.github.com> Date: Sat, 5 Nov 2022 03:22:39 -0700 Subject: [PATCH 03/12] Added 787 in Java (#69) * Add 787 java. Contributed by @spattk --- java/787_Cheapest_Flight_Within_K_Stops.java | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 java/787_Cheapest_Flight_Within_K_Stops.java diff --git a/java/787_Cheapest_Flight_Within_K_Stops.java b/java/787_Cheapest_Flight_Within_K_Stops.java new file mode 100644 index 0000000..9355cf9 --- /dev/null +++ b/java/787_Cheapest_Flight_Within_K_Stops.java @@ -0,0 +1,41 @@ +class Solution { + //using bellman ford + + public void computePrice(int[][]flights, int[] prices, int [] temp){ + for(int[] flight: flights){ + int u = flight[0]; + int v = flight[1]; + int price = flight[2]; + + if(prices[u] != Integer.MAX_VALUE){ + if(prices[u] + price < temp[v]){ + temp[v] = prices[u] + price; + } + } + } + } + + public void copyTempToPrice(int[] prices, int[] temp){ + for(int i=0; i Date: Sat, 5 Nov 2022 19:23:17 +0900 Subject: [PATCH 04/12] Added 026_Remove_Duplicates_from_Sorted_Array.java (#70) * Added 026_Remove_Duplicates_from_Sorted_Array.java Contributed by @green-study --- .../026_Remove_Duplicates_from_Sorted_Array.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 java/026_Remove_Duplicates_from_Sorted_Array.java diff --git a/java/026_Remove_Duplicates_from_Sorted_Array.java b/java/026_Remove_Duplicates_from_Sorted_Array.java new file mode 100644 index 0000000..13f03b1 --- /dev/null +++ b/java/026_Remove_Duplicates_from_Sorted_Array.java @@ -0,0 +1,16 @@ +//026. Remove Duplicates from Sorted Array +class Solution { + public int removeDuplicates(int[] nums) { + int index = 1; + + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] != nums[i + 1]) { + nums[index] = nums[i + 1]; + index++; + } + } + + return index; + } +} + From b3bd2dded4966d9fecde33fcff5ce4ceba8b672b Mon Sep 17 00:00:00 2001 From: BHwi <43560497+BHwi@users.noreply.github.com> Date: Sat, 12 Nov 2022 00:08:04 +0900 Subject: [PATCH 05/12] Added 049_Group_Anagrams.java, 129_Contains_Duplicate_II.java (#72) * Added java solution for 049_Group_Anagrams.java * Added java solution for 129_Contains_Duplicate_II.java Contributed by @BHwi --- java/049_Group_Anagrams.java | 42 +++++++++++++++++++++++++++ java/219_Contains_Duplicate_II.java | 44 +++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 java/049_Group_Anagrams.java create mode 100644 java/219_Contains_Duplicate_II.java diff --git a/java/049_Group_Anagrams.java b/java/049_Group_Anagrams.java new file mode 100644 index 0000000..a787625 --- /dev/null +++ b/java/049_Group_Anagrams.java @@ -0,0 +1,42 @@ +class Solution { + public List> groupAnagrams(String[] strs) { + int[][] alphabets = new int[strs.length]['z' - 'a' + 1]; + + for(int i = 0; i < strs.length; i++) { + String str = strs[i]; + + for(int j = 0; j < str.length(); j++) + alphabets[i][str.charAt(j) - 'a']++; + } + + boolean[] visited = new boolean[strs.length]; + + List> answer = new ArrayList<>(); + + for(int i = 0; i < strs.length; i++) { + if(visited[i]) continue; + + List list = new ArrayList<>(); + + for(int j = i; j < strs.length; j++) { + if(visited[j]) continue; + if(isAnagram(alphabets[i], alphabets[j])) { + list.add(strs[j]); + visited[j] = true; + } + } + + answer.add(list); + } + + return answer; + } + + public boolean isAnagram(int[] arr1, int[] arr2) { + for(int i = 0; i < arr1.length; i++) { + if(arr1[i] != arr2[i]) + return false; + } + return true; + } +} diff --git a/java/219_Contains_Duplicate_II.java b/java/219_Contains_Duplicate_II.java new file mode 100644 index 0000000..f88a1a9 --- /dev/null +++ b/java/219_Contains_Duplicate_II.java @@ -0,0 +1,44 @@ +import java.util.*; + +class Solution { + /* + * I have to save indice for each index. + * So I use the HashMap> + */ + + public boolean containsNearbyDuplicate(int[] nums, int k) { + HashMap> map = new HashMap<>(); + + for(int i = 0; i < nums.length; i++) { + if(!map.containsKey(nums[i])) { + map.put(nums[i], new ArrayList<>()); + } + map.get(nums[i]).add(i); + } + + // use Iterator to find appropriate two indice. + // Each list guarantee ascending. + // So list.get(i) and list.get(i + 1) is minimum. + Iterator keys = map.keySet().iterator(); + boolean answer = false; + + while(keys.hasNext()) { + int key = keys.next(); + List list = map.get(key); + + if(list.size() < 2) continue; + + for(int i = 1; i < list.size(); i++) { + int a = list.get(i - 1); + int b = list.get(i); + + if(b - a <= k) { + answer = true; + break; + } + } + if(answer) break; + } + return answer; + } +} From 7951e9e57697ccbd7d968b48d13fe2a4b77d2c85 Mon Sep 17 00:00:00 2001 From: Subin Kim <70996958+green-study@users.noreply.github.com> Date: Sun, 27 Nov 2022 11:56:54 +0900 Subject: [PATCH 06/12] Added 039_Combination_Sum.java (#75) * Added 039_Combination_Sum.java Contributed by @green-study --- java/039_Combination_Sum.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 java/039_Combination_Sum.java diff --git a/java/039_Combination_Sum.java b/java/039_Combination_Sum.java new file mode 100644 index 0000000..7a54dd3 --- /dev/null +++ b/java/039_Combination_Sum.java @@ -0,0 +1,29 @@ +//039_Combination_Sum +class Solution { + List> answer = new ArrayList>(); + + public List> combinationSum(int[] candidates, int target) { + int clen =candidates.length; + for (int i = 0; i < clen; i++) { + List tlist = new ArrayList(); + tlist.add(candidates[i]); + backtracking(candidates, i, 1, (target - candidates[i]), tlist); + } + return answer; + } + private void backtracking(int[] candidates, int index, int tsize, int target, List temp) { + if (target == 0) { + answer.add(new ArrayList(temp)); + return; + } + + for (int i = index, len = candidates.length; i < len; i++) { + if (candidates[i] <= target) { + temp.add(candidates[i]); + backtracking(candidates, i, (tsize + 1), (target - candidates[i]), temp); + temp.remove(tsize); + } + } + } +} + From 9c0ddc4ef9522c90ef020cb0608f86acded02f12 Mon Sep 17 00:00:00 2001 From: LONGNEW <40235475+LONGNEW@users.noreply.github.com> Date: Mon, 28 Nov 2022 20:47:14 +0900 Subject: [PATCH 07/12] Update Q1, Q7 answer (#76) * Update 001_Two_Sum.py * Update 007_Reverse_Integer.py Contributed by @LONGNEW --- python/001_Two_Sum.py | 7 +---- python/007_Reverse_Integer.py | 50 ++++++++++------------------------- 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/python/001_Two_Sum.py b/python/001_Two_Sum.py index e4bd58a..52399a9 100644 --- a/python/001_Two_Sum.py +++ b/python/001_Two_Sum.py @@ -58,9 +58,4 @@ def twoSum(self, nums, target): begin += 1 else: end -= 1 - - -if __name__ == '__main__': - # begin - s = Solution() - print s.twoSum([3, 2, 4], 6) + diff --git a/python/007_Reverse_Integer.py b/python/007_Reverse_Integer.py index a125d47..9f3f20f 100644 --- a/python/007_Reverse_Integer.py +++ b/python/007_Reverse_Integer.py @@ -1,39 +1,17 @@ class Solution: - # @return an integer + def reverse(self, x): + # https://leetcode.com/problems/reverse-integer/ + flag = True if x < 0 else False + if flag: + x = -x + x = str(x)[::-1] - # def reverse(self, x): - # max_int = 2147483647 - # if x == 0: - # return 0 - # isPos = True - # if x < 0: - # x *= (-1) - # isPos = False - # ltemp = [] - # while x != 0: - # temp = x % 10 - # ltemp.append(temp) - # x /= 10 - # result = 0 - # # the main solution - # for t in ltemp: - # result = result * 10 + t - # if result > max_int: - # result = 0 - # if isPos: - # return result - # else: - # return -1 * result + if flag: + x = "-" + x - def reverse(self, x): - # Note that in Python -1 / 10 = -1 - res, isPos = 0, 1 - if x < 0: - isPos = -1 - x = -1 * x - while x != 0: - res = res * 10 + x % 10 - if res > 2147483647: - return 0 - x /= 10 - return res * isPos \ No newline at end of file + value = 2 ** 31 + x = int(x) + if -value <= x < value: + return x + return 0 + From b369d126fe6e5fb5ab5111e745222f34ed3aa533 Mon Sep 17 00:00:00 2001 From: Subin Kim <70996958+green-study@users.noreply.github.com> Date: Sun, 4 Dec 2022 11:34:34 +0900 Subject: [PATCH 08/12] Update 006_ZigZag_conversion.java (#77) * Update 006_ZigZag_conversion.java Contributed by @green-study --- java/006_ZigZag_Conversion.java | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/java/006_ZigZag_Conversion.java b/java/006_ZigZag_Conversion.java index ea64188..97e4b1a 100644 --- a/java/006_ZigZag_Conversion.java +++ b/java/006_ZigZag_Conversion.java @@ -1 +1,33 @@ -//TODO \ No newline at end of file +//006_ZigZag_Conversion.java +class Solution { + public String convert(String s, int numRows) { + if(numRows==1) { + return s; + } + + String answer = ""; + String[] str_array = new String[numRows]; + + for(int i=0;i= numRows) { + index = 2*(numRows-1) - index; + } + str_array[index]+=c; + } + + for(int i=0;i Date: Thu, 16 Feb 2023 13:46:59 +0530 Subject: [PATCH 09/12] 2553_Separate_the_Digits_in_an_Array.cpp (#78) * Contributed by @ritikraj018 --- cpp/2553_Separate_the_Digits_in_an_Array.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cpp/2553_Separate_the_Digits_in_an_Array.cpp diff --git a/cpp/2553_Separate_the_Digits_in_an_Array.cpp b/cpp/2553_Separate_the_Digits_in_an_Array.cpp new file mode 100644 index 0000000..e88e8c6 --- /dev/null +++ b/cpp/2553_Separate_the_Digits_in_an_Array.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector separateDigits(vector& nums) { + vector answer; + int n; + for( int i=0; i < nums.size(); i++){ + n=nums[i]; + vector temp; + while( n>0 ){ + temp.push_back(n%10); + n = n / 10; + } + for(int j= temp.size()-1; j>=0; j--){ + answer.push_back(temp[j]); + } + } + return answer; + } +}; From 5a52cd9b54553d68ee10cfe87044bed79ee5fb93 Mon Sep 17 00:00:00 2001 From: Yuchen Zhou <72342196+ROMEEZHOU@users.noreply.github.com> Date: Tue, 21 Mar 2023 21:31:55 -0400 Subject: [PATCH 10/12] Add a new method for 007_Reverse_Integer.py (#80) * Add a new method for 007 reverse integer Contributed by @ROMEEZHOU --- python/007_Reverse_Integer.py | 39 +++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/python/007_Reverse_Integer.py b/python/007_Reverse_Integer.py index 9f3f20f..19aeba8 100644 --- a/python/007_Reverse_Integer.py +++ b/python/007_Reverse_Integer.py @@ -1,17 +1,34 @@ class Solution: def reverse(self, x): # https://leetcode.com/problems/reverse-integer/ - flag = True if x < 0 else False - if flag: +# flag = True if x < 0 else False +# if flag: +# x = -x +# x = str(x)[::-1] + +# if flag: +# x = "-" + x + +# value = 2 ** 31 +# x = int(x) +# if -value <= x < value: +# return x +# return 0 + + is_neg = False + if x < 0: x = -x - x = str(x)[::-1] + is_neg = True - if flag: - x = "-" + x + res = 0 + while x > 0: + res *= 10 + res += x % 10 + x //= 10 + if is_neg: + res = -res - value = 2 ** 31 - x = int(x) - if -value <= x < value: - return x - return 0 - + if res < -2**31 or res > 2**31-1: + return 0 + return res + \ No newline at end of file From 9adfbdf343963ee16696aa85f0846fe544105430 Mon Sep 17 00:00:00 2001 From: Ajay <86017484+ajay2614@users.noreply.github.com> Date: Fri, 19 May 2023 13:05:04 +0530 Subject: [PATCH 11/12] Create 442_Find_All_Duplicates_in_an_Array.java (#81) Contributed by @ajay2614 --- java/442_Find_All_Duplicates_in_an_Array.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 java/442_Find_All_Duplicates_in_an_Array.java diff --git a/java/442_Find_All_Duplicates_in_an_Array.java b/java/442_Find_All_Duplicates_in_an_Array.java new file mode 100644 index 0000000..b12d6f5 --- /dev/null +++ b/java/442_Find_All_Duplicates_in_an_Array.java @@ -0,0 +1,17 @@ +class Solution { + public List findDuplicates(int[] nums) { + int n = nums.length; + + List ans = new ArrayList<>(); + + for(int i=0;i Date: Tue, 3 Oct 2023 06:22:10 -0400 Subject: [PATCH 12/12] 88. Merge Sorted Array Java (#82) Added the solution to 'Merge Sorted Array' which is number 88 in LeetCode Contributed by @avash-poudel --- java/088_Merge_Sorted_Array.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 java/088_Merge_Sorted_Array.java diff --git a/java/088_Merge_Sorted_Array.java b/java/088_Merge_Sorted_Array.java new file mode 100644 index 0000000..95c7c8e --- /dev/null +++ b/java/088_Merge_Sorted_Array.java @@ -0,0 +1,29 @@ +class Solution { + //we are being given two int arrays and two int variables that state the number of elements in each array, respectively + public void merge(int[] nums1, int m, int[] nums2, int n) { + //I am using a counter so that I can iterate through the second array inside the for loop + int counter = 0; + //We know that nums1 is of the size n + m, so we can add all the elements to the array and sort them later + //For loop adds all values of nums2 to the end of nums1 + for(int i=m; i nums1[j+1]){ + int temp = nums1[j+1]; + nums1[j+1] = nums1[j]; + nums1[j] = temp; + } + } + } + //The following function simply prints out everything that is contained within our num1 array + for(int i=0; i