From 3f2ee2744593254ab0379457a9f98b4331ffb5f2 Mon Sep 17 00:00:00 2001 From: karanjamwal21 Date: Mon, 16 Oct 2023 14:22:50 +0530 Subject: [PATCH 1/5] new change --- Hard/10. Regular Expression Matching/soljava.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Hard/10. Regular Expression Matching/soljava.java b/Hard/10. Regular Expression Matching/soljava.java index 1bf35e4..01708bc 100644 --- a/Hard/10. Regular Expression Matching/soljava.java +++ b/Hard/10. Regular Expression Matching/soljava.java @@ -8,7 +8,8 @@ public boolean isMatch(String s, String p) { } - // the sub-problem: to match[i, m + // the sub-problem: to match[i, m] + private boolean dfs(String s, String p, int i, int j, int m, int n, Boolean[][] M) { @@ -27,6 +28,7 @@ private boolean dfs(String s, String p, int i, int j, int m, int n, Boolean[][] private boolean match(char c1, char c2) { if (c2 == '.') return true; return c1 == c2; + } - + } \ No newline at end of file From 48ac06f6fadb7aaafbacaa9d3d52f81c2c48309f Mon Sep 17 00:00:00 2001 From: karanjamwal21 Date: Mon, 16 Oct 2023 14:33:31 +0530 Subject: [PATCH 2/5] newfile --- Medium/18.4sum/solinjava.java | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Medium/18.4sum/solinjava.java diff --git a/Medium/18.4sum/solinjava.java b/Medium/18.4sum/solinjava.java new file mode 100644 index 0000000..83fd005 --- /dev/null +++ b/Medium/18.4sum/solinjava.java @@ -0,0 +1,55 @@ +public class solinjava { + + + public List> fourSum(int[] nums, int target) { + List> ans = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 3; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + for (int j = i + 1; j < nums.length - 2; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; + } + + int k = j + 1; + int l = nums.length - 1; + + while (k < l) { + long sum = (long) nums[i] + nums[j] + nums[k] + nums[l]; + + if (sum == target) { + List temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[j]); + temp.add(nums[k]); + temp.add(nums[l]); + ans.add(temp); + + k++; + l--; + + while (k < l && nums[k] == nums[k - 1]) { + k++; + } + + while (k < l && nums[l] == nums[l + 1]) { + l--; + } + } else if (sum < target) { + k++; + } else { + l--; + } + } + } + } + + return ans; + } + + +} From df94b06dd8ab386e2b49f15dba2da09368c345cd Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Tue, 17 Oct 2023 09:28:49 +0800 Subject: [PATCH 3/5] Delete Hard/10. Regular Expression Matching/soljava.java --- .../soljava.java | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 Hard/10. Regular Expression Matching/soljava.java diff --git a/Hard/10. Regular Expression Matching/soljava.java b/Hard/10. Regular Expression Matching/soljava.java deleted file mode 100644 index 01708bc..0000000 --- a/Hard/10. Regular Expression Matching/soljava.java +++ /dev/null @@ -1,34 +0,0 @@ -class Solution { - - - public boolean isMatch(String s, String p) { - int m = s.length(), n = p.length(); - Boolean[][] M = new Boolean[m+1][n]; // Why Boolean: null indicates the sub-problem not processed yet - return dfs(s, p, 0, 0, m, n, M); // The problem: to match s[i, m), p[j, n) - } - - - // the sub-problem: to match[i, m] - - - - private boolean dfs(String s, String p, int i, int j, int m, int n, Boolean[][] M) { - if (j == n) return i == m; - if (M[i][j] != null) return M[i][j]; - - char c2 = p.charAt(j); - if (j < n - 1 && p.charAt(j + 1) == '*') - return M[i][j] = dfs(s, p, i, j+2, m, n, M) || // do not match 'x*' , x means any char or . (use'*' as 0 char) - i < m && match(s.charAt(i), c2) && dfs(s, p, i+1, j, m, n, M); // match 1 char in string for '*' - - return M[i][j] = i < m && match(s.charAt(i), c2) && dfs(s, p, i+1, j+1, m, n, M); // match 1 char from both sides - } - - - private boolean match(char c1, char c2) { - if (c2 == '.') return true; - return c1 == c2; - - } - -} \ No newline at end of file From e4420772fd9b93c3c2fa2be636995cfc0a2026a5 Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Tue, 17 Oct 2023 09:29:14 +0800 Subject: [PATCH 4/5] Delete Medium/2. Add Two Numbers/newsol.txt --- Medium/2. Add Two Numbers/newsol.txt | 49 ---------------------------- 1 file changed, 49 deletions(-) delete mode 100644 Medium/2. Add Two Numbers/newsol.txt diff --git a/Medium/2. Add Two Numbers/newsol.txt b/Medium/2. Add Two Numbers/newsol.txt deleted file mode 100644 index 13793db..0000000 --- a/Medium/2. Add Two Numbers/newsol.txt +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Definition for singly-linked list. - * public class ListNode { - * int val; - * ListNode next; - * ListNode() {} - * ListNode(int val) { this.val = val; } - * ListNode(int val, ListNode next) { this.val = val; this.next = next; } - * } - */ - - -class Solution { - public ListNode addTwoNumbers(ListNode l1, ListNode l2) { - ListNode d=new ListNode(0); - ListNode t=d; - int s=0,c=0; - while(l1!=null || l2!=null){ - s=0; - if(l1!=null) { - s+=l1.val; - l1=l1.next; - } - if(l2!=null) { - s+=l2.val; - l2=l2.next; - } - if(c!=0) s+=c; - if(s/10!=0){ - ListNode x= new ListNode(s%10); - t.next=x; - c=s/10; - t=x; - } - else{ - ListNode x= new ListNode(s); - t.next=x; - c=0; - t=x; - } - } - if(c!=0){ - ListNode x= new ListNode(c); - t.next=x; - t=x; - } - return d.next; - } -} \ No newline at end of file From 4861119b75a092424e0cc7421fe449af73027f65 Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Tue, 17 Oct 2023 09:33:57 +0800 Subject: [PATCH 5/5] Update and rename solinjava.java to Solution.java --- Medium/18. 4Sum/Solution.java | 50 +++++++++++++++++++++++++++++++ Medium/18.4sum/solinjava.java | 55 ----------------------------------- 2 files changed, 50 insertions(+), 55 deletions(-) create mode 100644 Medium/18. 4Sum/Solution.java delete mode 100644 Medium/18.4sum/solinjava.java diff --git a/Medium/18. 4Sum/Solution.java b/Medium/18. 4Sum/Solution.java new file mode 100644 index 0000000..6dcc1e0 --- /dev/null +++ b/Medium/18. 4Sum/Solution.java @@ -0,0 +1,50 @@ +public class Solution { + public List> fourSum(int[] nums, int target) { + List> ans = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 3; i++) { + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + for (int j = i + 1; j < nums.length - 2; j++) { + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; + } + + int k = j + 1; + int l = nums.length - 1; + + while (k < l) { + long sum = (long) nums[i] + nums[j] + nums[k] + nums[l]; + + if (sum == target) { + List temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[j]); + temp.add(nums[k]); + temp.add(nums[l]); + ans.add(temp); + + k++; + l--; + + while (k < l && nums[k] == nums[k - 1]) { + k++; + } + + while (k < l && nums[l] == nums[l + 1]) { + l--; + } + } else if (sum < target) { + k++; + } else { + l--; + } + } + } + } + return ans; + } +} diff --git a/Medium/18.4sum/solinjava.java b/Medium/18.4sum/solinjava.java deleted file mode 100644 index 83fd005..0000000 --- a/Medium/18.4sum/solinjava.java +++ /dev/null @@ -1,55 +0,0 @@ -public class solinjava { - - - public List> fourSum(int[] nums, int target) { - List> ans = new ArrayList<>(); - Arrays.sort(nums); - - for (int i = 0; i < nums.length - 3; i++) { - if (i > 0 && nums[i] == nums[i - 1]) { - continue; - } - - for (int j = i + 1; j < nums.length - 2; j++) { - if (j > i + 1 && nums[j] == nums[j - 1]) { - continue; - } - - int k = j + 1; - int l = nums.length - 1; - - while (k < l) { - long sum = (long) nums[i] + nums[j] + nums[k] + nums[l]; - - if (sum == target) { - List temp = new ArrayList<>(); - temp.add(nums[i]); - temp.add(nums[j]); - temp.add(nums[k]); - temp.add(nums[l]); - ans.add(temp); - - k++; - l--; - - while (k < l && nums[k] == nums[k - 1]) { - k++; - } - - while (k < l && nums[l] == nums[l + 1]) { - l--; - } - } else if (sum < target) { - k++; - } else { - l--; - } - } - } - } - - return ans; - } - - -}