From fbf7bb94e7282bd82670d3290e880de4bbcbd81e Mon Sep 17 00:00:00 2001 From: yennanliu Date: Mon, 27 May 2024 18:08:48 +0800 Subject: [PATCH] update 20 java, cheatsheet, progress --- data/progress.txt | 2 +- data/to_review.txt | 18 +++++----- doc/cheatsheet/java_trick.md | 10 ++++++ .../LeetCodeJava/Stack/ValidParentheses.java | 35 +++++++++++++++++++ .../src/main/java/dev/workspace3.java | 32 +++++++++++++++++ 5 files changed, 87 insertions(+), 10 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index 9bd199f4..1117258e 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,4 +1,4 @@ -20240527: 21 +20240527: 21,20 20240526: 417(again),33,153,152 20240525: 424,297(todo),295(todo),39 20240524: 55,54(todo),53(again),435(again),49,48(todo),300(again) diff --git a/data/to_review.txt b/data/to_review.txt index 22d9d3b2..632fea2f 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-07-21 -> ['21'] +2024-07-21 -> ['21,20'] 2024-07-20 -> ['417(again),33,153,152'] 2024-07-19 -> ['424,297(todo),295(todo),39'] 2024-07-18 -> ['55,54(todo),53(again),435(again),49,48(todo),300(again)'] @@ -16,7 +16,7 @@ 2024-07-06 -> ['371'] 2024-07-05 -> ['121,252'] 2024-07-04 -> ['125'] -2024-06-30 -> ['21'] +2024-06-30 -> ['21,20'] 2024-06-29 -> ['417(again),33,153,152'] 2024-06-28 -> ['424,297(todo),295(todo),39'] 2024-06-27 -> ['55,54(todo),53(again),435(again),49,48(todo),300(again)'] @@ -29,7 +29,7 @@ 2024-06-20 -> ['347,253(todo),91(todo),217'] 2024-06-19 -> ['226,98,253(todo)'] 2024-06-18 -> ['104,230,102,100'] -2024-06-17 -> ['21', '105,106'] +2024-06-17 -> ['21,20', '105,106'] 2024-06-16 -> ['417(again),33,153,152', '242,235'] 2024-06-15 -> ['424,297(todo),295(todo),39', '371'] 2024-06-14 -> ['55,54(todo),53(again),435(again),49,48(todo),300(again)', '121,252'] @@ -37,19 +37,19 @@ 2024-06-12 -> ['62,572,57'] 2024-06-11 -> ['322,191,190'] 2024-06-10 -> ['73,200,70,323(again)'] -2024-06-09 -> ['21', '207,79,206,213,198'] +2024-06-09 -> ['21,20', '207,79,206,213,198'] 2024-06-08 -> ['417(again),33,153,152', '212(todo),211,338,208(again)'] 2024-06-07 -> ['424,297(todo),295(todo),39', '347,253(todo),91(todo),217'] 2024-06-06 -> ['55,54(todo),53(again),435(again),49,48(todo),300(again)', '226,98,253(todo)'] 2024-06-05 -> ['56', '104,230,102,100'] -2024-06-04 -> ['21', '62,572,57', '105,106'] +2024-06-04 -> ['21,20', '62,572,57', '105,106'] 2024-06-03 -> ['417(again),33,153,152', '322,191,190', '242,235'] 2024-06-02 -> ['424,297(todo),295(todo),39', '73,200,70,323(again)', '371'] -2024-06-01 -> ['21', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '207,79,206,213,198', '121,252'] +2024-06-01 -> ['21,20', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '207,79,206,213,198', '121,252'] 2024-05-31 -> ['417(again),33,153,152', '56', '212(todo),211,338,208(again)', '125'] -2024-05-30 -> ['21', '424,297(todo),295(todo),39', '62,572,57', '347,253(todo),91(todo),217'] -2024-05-29 -> ['21', '417(again),33,153,152', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '322,191,190', '226,98,253(todo)'] -2024-05-28 -> ['21', '417(again),33,153,152', '424,297(todo),295(todo),39', '56', '73,200,70,323(again)', '104,230,102,100'] +2024-05-30 -> ['21,20', '424,297(todo),295(todo),39', '62,572,57', '347,253(todo),91(todo),217'] +2024-05-29 -> ['21,20', '417(again),33,153,152', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '322,191,190', '226,98,253(todo)'] +2024-05-28 -> ['21,20', '417(again),33,153,152', '424,297(todo),295(todo),39', '56', '73,200,70,323(again)', '104,230,102,100'] 2024-05-27 -> ['417(again),33,153,152', '424,297(todo),295(todo),39', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '62,572,57', '207,79,206,213,198', '105,106'] 2024-05-26 -> ['424,297(todo),295(todo),39', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '56', '322,191,190', '212(todo),211,338,208(again)', '242,235'] 2024-05-25 -> ['55,54(todo),53(again),435(again),49,48(todo),300(again)', '56', '62,572,57', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371'] diff --git a/doc/cheatsheet/java_trick.md b/doc/cheatsheet/java_trick.md index 6ff308f6..bdc4a068 100644 --- a/doc/cheatsheet/java_trick.md +++ b/doc/cheatsheet/java_trick.md @@ -709,4 +709,14 @@ return check_(root, smallest_val, biggest_val); // LC 338 +``` + +### 2-5) Init Queue + +- https://stackoverflow.com/questions/4626812/how-do-i-instantiate-a-queue-object-in-java + +- A Queue is an `interface`, which means you cannot construct a Queue directly. +- Consinder use one of below implementation: +``` + AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, or SynchronousQueue. ``` \ No newline at end of file diff --git a/leetcode_java/src/main/java/LeetCodeJava/Stack/ValidParentheses.java b/leetcode_java/src/main/java/LeetCodeJava/Stack/ValidParentheses.java index f4241fb8..cc3f2cf4 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Stack/ValidParentheses.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Stack/ValidParentheses.java @@ -5,6 +5,7 @@ // https://www.softwaretestinghelp.com/java-queue-interface/ import java.util.HashMap; +import java.util.Map; import java.util.Stack; public class ValidParentheses { @@ -13,6 +14,40 @@ public class ValidParentheses { // IDEA : STACK + DICT public boolean isValid(String s) { + if (s == null || s.length() == 0){ + return true; + } + + Map map = new HashMap<>(); + map.put("(", ")"); + map.put("{", "}"); + map.put("[", "]"); + + /** NOTE !!! use stack here, FILO */ + Stack stack = new Stack<>(); + + for (String x : s.split("")){ + //System.out.println("x = " + x + " stack = " + stack); + if (map.containsKey(x)){ + stack.add(x); + }else{ + if (stack.isEmpty()){ + return false; + } + String last = stack.pop(); + if (!map.get(last).equals(x)){ + return false; + } + } + } + + return stack.isEmpty(); + } + + // V0' + // IDEA : STACK + DICT + public boolean isValid_0(String s) { + if (s.length() % 2 != 0){ return false; } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index ad0ffcc2..edc03e28 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -2312,4 +2312,36 @@ public ListNode mergeTwoLists(ListNode list1, ListNode list2) { return root.next; } + // LC 20 + public boolean isValid(String s) { + + if (s == null || s.length() == 0){ + return true; + } + + Map map = new HashMap<>(); + map.put("(", ")"); + map.put("{", "}"); + map.put("[", "]"); + + Stack stack = new Stack<>(); // TODO : check + + for (String x : s.split("")){ + System.out.println("x = " + x + " stack = " + stack); + if (map.containsKey(x)){ + stack.add(x); + }else{ + if (stack.isEmpty()){ + return false; + } + String last = stack.pop(); + if (!map.get(last).equals(x)){ + return false; + } + } + } + + return stack.isEmpty(); + } + }