From 605cff4fa91252f76fd3b84775bbb1c8da139f03 Mon Sep 17 00:00:00 2001 From: "wangjunchao.o" Date: Sun, 28 Jun 2020 07:56:39 +0800 Subject: [PATCH 01/18] commit Change-Id: I0240eac01220703bd1ba165e51b0aee17dfad544 --- .idea/modules.xml | 1 + .idea/workspace.xml | 113 ++++++++---------- .../[0447][Number of Boomerangs].iml | 21 ++++ .../src/Solution.java | 69 +++++++++++ .../src/SolutionTest.java | 26 ++++ 5 files changed, 170 insertions(+), 60 deletions(-) create mode 100644 [0447][Number of Boomerangs]/[0447][Number of Boomerangs].iml create mode 100644 [0447][Number of Boomerangs]/src/Solution.java create mode 100644 [0447][Number of Boomerangs]/src/SolutionTest.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 1268fe5..e85fdc2 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -226,6 +226,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f0fc058..4d3d05d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,31 +2,11 @@ - - - - - - - - - - - - - - - - - - - - - - + + + - - - - + + + - - + + - - + + - + + - @@ -221,7 +203,7 @@ - + 1560552440693 @@ -585,58 +567,58 @@ - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - - + + - + @@ -649,27 +631,28 @@ - + - + - + - - + + - + - - + + + @@ -904,6 +887,16 @@ 57 diff --git a/.idea/modules.xml b/.idea/modules.xml index e85fdc2..618f410 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -228,6 +228,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4d3d05d..485bf29 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,9 @@ - - - + + + @@ -26,6 +26,7 @@ + @@ -50,6 +51,9 @@ + + + @@ -58,7 +62,8 @@ - + + @@ -72,7 +77,7 @@ - + - - + + - - + + @@ -135,19 +141,19 @@ - + + - @@ -204,6 +210,9 @@ + + + 1560552440693 @@ -567,34 +576,34 @@ - + - + - + - + - + - + - + - + @@ -624,17 +633,19 @@ - - + + + + - + - + @@ -642,17 +653,19 @@ - + + - + + @@ -778,7 +791,7 @@ file://$PROJECT_DIR$/[0429][N-ary Tree Level Order Traversal]/src/Main.java - 38 + 37 @@ -828,12 +841,12 @@ file://$PROJECT_DIR$/[0241][Different Ways to Add Parentheses]/src/Solution.java - 75 + 76 file://$PROJECT_DIR$/[0241][Different Ways to Add Parentheses]/src/Solution.java - 69 + 70 @@ -844,7 +857,7 @@ file://$PROJECT_DIR$/[0207][Course Schedule]/src/Solution.java - 64 + 60 @@ -854,7 +867,7 @@ file://$PROJECT_DIR$/[0475][Heaters]/src/Solution.java - 46 + 47 @@ -874,12 +887,12 @@ file://$PROJECT_DIR$/[0392][Is Subsequence]/src/Solution.java - 66 + 65 file://$PROJECT_DIR$/[0441][Arranging Coins]/src/Solution.java - 67 + 66 diff --git a/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/[0453][Minimum Moves to Equal Array Elements].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java new file mode 100644 index 0000000..a50bf0f --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java @@ -0,0 +1,36 @@ +/** + * Author: 王俊超 + * Time: 2020-06-28 21:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路: + * 假设数组最小的值是xmin,一共加了k次,加完后的值是W,元素个数是n + * 则:W <= xmin + k + * k(n-1) + sum(nums) = W*n + * ==> + * k(n-1) + sum(nums) <= (xmin + k)n + * ==> + * sum(nums) - n*xmin <= k + * + * @param nums + * @return + */ + public int minMoves(int[] nums) { + + int sum = 0; + int min = Integer.MAX_VALUE; + + for (int i : nums) { + sum += i; + if (i < min) { + min = i; + } + } + + return sum - min * nums.length; + } +} diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java new file mode 100644 index 0000000..1b8b239 --- /dev/null +++ b/[0453][Minimum Moves to Equal Array Elements]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.lang.annotation.Target; + +/** + * Author: 王俊超 + * Time: 2020-06-28 22:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + @Test + public void test1() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 2, 3}, 3}, + {new int[]{1, 1, 1}, 0}, + {new int[]{1, 1, 2}, 1}, + }; + + for (Object[] d : data) { + Assert.assertEquals(s.minMoves((int[]) d[0]), d[1]); + } + } +} From 1c7af47229b791a6b127eb96dfadb7c21ecd4bbf Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Mon, 29 Jun 2020 08:12:39 +0800 Subject: [PATCH 04/18] commit Change-Id: If5826faa98325213bca778dc3fcf3c06035c00b6 --- [0506][Relative Ranks]/src/Solution.java | 11 +++++++++++ [0506][Relative Ranks]/src/SolutionTest.java | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 [0506][Relative Ranks]/src/Solution.java create mode 100644 [0506][Relative Ranks]/src/SolutionTest.java diff --git a/[0506][Relative Ranks]/src/Solution.java b/[0506][Relative Ranks]/src/Solution.java new file mode 100644 index 0000000..52a6ed4 --- /dev/null +++ b/[0506][Relative Ranks]/src/Solution.java @@ -0,0 +1,11 @@ +package PACKAGE_NAME; + +/** + * Author: 王俊超 + * Time: 2020-06-29 07:34 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { +} diff --git a/[0506][Relative Ranks]/src/SolutionTest.java b/[0506][Relative Ranks]/src/SolutionTest.java new file mode 100644 index 0000000..53129d4 --- /dev/null +++ b/[0506][Relative Ranks]/src/SolutionTest.java @@ -0,0 +1,11 @@ +package PACKAGE_NAME; + +/** + * Author: 王俊超 + * Time: 2020-06-29 07:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { +} From c07b4594f01e644f3ac5cc01335b31e3947cf0f6 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Mon, 29 Jun 2020 08:29:15 +0800 Subject: [PATCH 05/18] commit Change-Id: I08806c5b8ee5a373800efe2028bc4e00f44e755f --- .gitignore | 1 - .idea/modules.xml | 1 + .idea/workspace.xml | 59 ++++---- .../src/Solution.java | 4 + .../[0506][Relative Ranks].iml | 21 +++ [0506][Relative Ranks]/src/Solution.java | 130 +++++++++++++++++- [0506][Relative Ranks]/src/SolutionTest.java | 16 ++- 7 files changed, 204 insertions(+), 28 deletions(-) create mode 100644 [0506][Relative Ranks]/[0506][Relative Ranks].iml diff --git a/.gitignore b/.gitignore index f18f368..2304249 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,4 @@ out # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -*/*.iml diff --git a/.idea/modules.xml b/.idea/modules.xml index 618f410..4ffbbd5 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -242,6 +242,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 485bf29..5249778 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,13 @@ - - - + + + + + @@ -77,7 +79,7 @@ - + - - + + - + + - @@ -213,6 +215,7 @@ + 1560552440693 @@ -572,38 +575,42 @@ + + + + - + - + - + - + - + - + - + - + @@ -629,23 +636,24 @@ - - + + + - + - + - + - + @@ -653,13 +661,14 @@ - + - + + diff --git a/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java index a50bf0f..395fde3 100644 --- a/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java +++ b/[0453][Minimum Moves to Equal Array Elements]/src/Solution.java @@ -21,6 +21,10 @@ public class Solution { */ public int minMoves(int[] nums) { + if (nums == null) { + return 0; + } + int sum = 0; int min = Integer.MAX_VALUE; diff --git a/[0506][Relative Ranks]/[0506][Relative Ranks].iml b/[0506][Relative Ranks]/[0506][Relative Ranks].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0506][Relative Ranks]/[0506][Relative Ranks].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0506][Relative Ranks]/src/Solution.java b/[0506][Relative Ranks]/src/Solution.java index 52a6ed4..3cc25f6 100644 --- a/[0506][Relative Ranks]/src/Solution.java +++ b/[0506][Relative Ranks]/src/Solution.java @@ -1,4 +1,4 @@ -package PACKAGE_NAME; +import java.util.*; /** * Author: 王俊超 @@ -8,4 +8,132 @@ * Declaration: All Rights Reserved !!! **/ public class Solution { + + class Rank { + Rank(int s, int i) { + this.Score = s; + this.Index = i; + } + int Score; + int Index; + } + + /** + * 题解思路: + * 1、记录分数对应的下标; + * 2、对分数进行排序,求行排名 + * 3、再根据分数将原始数组的下标和排名对应起来 + * + * @param nums + * @return + */ + public String[] findRelativeRanks(int[] nums) { + if (nums == null) { + return null; + } + + Rank[] ranks = new Rank[nums.length]; + for (int i = 0; i < nums.length; i++) { + ranks[i] = new Rank(nums[i], i); + } + + Arrays.sort(ranks, new Comparator() { + @Override + public int compare(Rank o1, Rank o2) { + return -(o1.Score - o2.Score); + } + }); + + String[] result = new String[nums.length]; + for (int i = 0; i < ranks.length; i++) { + switch (i) { + case 0: + result[ranks[i].Index] = "Gold Medal"; + break; + case 1: + result[ranks[i].Index] = "Silver Medal"; + break; + + case 2: + result[ranks[i].Index] = "Bronze Medal"; + break; + default: + result[ranks[i].Index] = "" + (i + 1); + } + } + return result; + } + + public String[] findRelativeRanks3(int[] nums) { + if (nums == null) { + return null; + } + + Map map = new HashMap<>(); + Queue queue = new PriorityQueue<>(nums.length + 1); // 使用优先堆,加1是为了防止参数为0 + + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], i); + queue.add(nums[i]); // 创建小顶堆 + } + + + String[] result = new String[nums.length]; + int i = nums.length + 1; + while (!queue.isEmpty()) { + int score = queue.poll(); + i--; + switch (i) { + case 1: + result[map.get(score)] = "Gold Medal"; + break; + case 2: + result[map.get(score)] = "Silver Medal"; + break; + case 3: + result[map.get(score)] = "Bronze Medal"; + break; + default: + result[map.get(score)] = "" + i; + } + + } + + return result; + } + + public String[] findRelativeRanks2(int[] nums) { + if (nums == null) { + return null; + } + + Map map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], i); + } + + String[] result = new String[nums.length]; + Arrays.sort(nums); + + for (int i = 0; i < nums.length; i++) { + int score = nums[nums.length - 1 - i]; + switch (i) { + case 0: + result[map.get(score)] = "Gold Medal"; + break; + case 1: + result[map.get(score)] = "Silver Medal"; + break; + + case 2: + result[map.get(score)] = "Bronze Medal"; + break; + default: + result[map.get(score)] = "" + (i + 1); + } + } + + return result; + } } diff --git a/[0506][Relative Ranks]/src/SolutionTest.java b/[0506][Relative Ranks]/src/SolutionTest.java index 53129d4..6af869f 100644 --- a/[0506][Relative Ranks]/src/SolutionTest.java +++ b/[0506][Relative Ranks]/src/SolutionTest.java @@ -1,4 +1,5 @@ -package PACKAGE_NAME; +import org.junit.Assert; +import org.junit.Test; /** * Author: 王俊超 @@ -8,4 +9,17 @@ * Declaration: All Rights Reserved !!! **/ public class SolutionTest { + + @Test + public void test1() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{5, 4, 3, 2, 1}, new String[]{"Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"}}, + {new int[]{1, 2, 3, 4, 5}, new String[]{"5", "4", "Bronze Medal", "Silver Medal", "Gold Medal"}}, + }; + + for (Object[] d: data) { + Assert.assertArrayEquals(s.findRelativeRanks((int[]) d[0]), (Object[]) d[1]); + } + } } From d0747cc1dafb5c72068c0be687a4b1f8068ccb3f Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Tue, 30 Jun 2020 09:32:30 +0800 Subject: [PATCH 06/18] commit Change-Id: I063540e070f881de78b6f278119ce7a7e05f8e75 --- .idea/modules.xml | 6 + .idea/workspace.xml | 256 ++++++++---------- .../[0507][Perfect Number].iml | 21 ++ [0507][Perfect Number]/src/Solution.java | 26 ++ [0507][Perfect Number]/src/SolutionTest.java | 29 ++ .../[0509][Fibonacci Number].iml | 21 ++ [0509][Fibonacci Number]/src/Solution.java | 27 ++ .../src/SolutionTest.java | 26 ++ .../[0520][Detect Capital].iml | 11 + [0520][Detect Capital]/src/Solution.java | 73 +++++ ...[0521][Longest Uncommon Subsequence I].iml | 11 + .../src/Solution.java | 20 ++ .../[0523][K Diff Pairs In An Array].iml | 21 ++ .../src/Solution.java | 40 +++ .../src/SolutionTest.java | 27 ++ ...0][Minimum Absolute Difference in BST].iml | 21 ++ .../src/Solution.java | 93 +++++++ .../src/SolutionTest.java | 35 +++ .../src/TreeNode.java | 16 ++ 19 files changed, 644 insertions(+), 136 deletions(-) create mode 100644 [0507][Perfect Number]/[0507][Perfect Number].iml create mode 100644 [0507][Perfect Number]/src/Solution.java create mode 100644 [0507][Perfect Number]/src/SolutionTest.java create mode 100644 [0509][Fibonacci Number]/[0509][Fibonacci Number].iml create mode 100644 [0509][Fibonacci Number]/src/Solution.java create mode 100644 [0509][Fibonacci Number]/src/SolutionTest.java create mode 100644 [0520][Detect Capital]/[0520][Detect Capital].iml create mode 100644 [0520][Detect Capital]/src/Solution.java create mode 100644 [0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml create mode 100644 [0521][Longest Uncommon Subsequence I]/src/Solution.java create mode 100644 [0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml create mode 100644 [0523][K Diff Pairs In An Array]/src/Solution.java create mode 100644 [0523][K Diff Pairs In An Array]/src/SolutionTest.java create mode 100644 [0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml create mode 100644 [0530][Minimum Absolute Difference in BST]/src/Solution.java create mode 100644 [0530][Minimum Absolute Difference in BST]/src/SolutionTest.java create mode 100644 [0530][Minimum Absolute Difference in BST]/src/TreeNode.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 4ffbbd5..63e77e6 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -243,6 +243,12 @@ + + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5249778..01614ce 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,13 +2,23 @@ + + + + + + + + + + + + + + + - - - - - @@ -53,9 +64,12 @@ + + + @@ -68,67 +82,73 @@ + + + + + + - - - + + + - - + + - - + + - - + + - - + + - - - - - + + + + + - - - - - + + + + + @@ -215,7 +235,8 @@ - + + 1560552440693 @@ -571,10 +592,14 @@ - + - + + + + + @@ -583,54 +608,74 @@ - + - + + + + - + - + + + + - + - + + + + - + - + + + + - + - - + + + + - - + + + + - - + + + + - + + + @@ -641,39 +686,45 @@ - + - + - + - + + + + + + + - + - - + + - + - + @@ -919,6 +970,16 @@ 65 + + file://$PROJECT_DIR$/[0507][Perfect Number]/src/Solution.java + 13 + + + file://$PROJECT_DIR$/[0523][K Diff Pairs In An Array]/src/Solution.java + 19 + file://$PROJECT_DIR$/[081][Search In Rotated Sorted Array II]/src/Solution2.java 17 @@ -1024,81 +1085,4 @@ - - - - - - - - - - - - - No facets are configured - - - - - - - - Bundled Protobuf Distribution - - - - - - - - 1.7 - - - - - - - - [0419][Battleships in a Board] - - - - - - - - R User Library - - - - - - - \ No newline at end of file diff --git a/[0507][Perfect Number]/[0507][Perfect Number].iml b/[0507][Perfect Number]/[0507][Perfect Number].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0507][Perfect Number]/[0507][Perfect Number].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0507][Perfect Number]/src/Solution.java b/[0507][Perfect Number]/src/Solution.java new file mode 100644 index 0000000..036581e --- /dev/null +++ b/[0507][Perfect Number]/src/Solution.java @@ -0,0 +1,26 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:33 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean checkPerfectNumber(int num) { + if (num < 6) { + return false; + } + + int sum = 1; + int sqrt = (int) Math.sqrt(num); + + for (int i = 2; i <= sqrt; i++) { + if (num % i == 0) { + sum += i; + sum += num / i; + } + } + + return sum == num; + } +} diff --git a/[0507][Perfect Number]/src/SolutionTest.java b/[0507][Perfect Number]/src/SolutionTest.java new file mode 100644 index 0000000..287f91d --- /dev/null +++ b/[0507][Perfect Number]/src/SolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 08:36 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void checkPerfectNumber() { + Solution s = new Solution(); + Object[][] data = { + {28, true}, + {-1, false}, + {0, false}, + {1, false}, + {6, true}, + {7, false}, + {8, false}, + }; + + for (Object[] d: data) { + Assert.assertEquals(s.checkPerfectNumber((Integer) d[0]), d[1]); + } + } +} \ No newline at end of file diff --git a/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0509][Fibonacci Number]/[0509][Fibonacci Number].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0509][Fibonacci Number]/src/Solution.java b/[0509][Fibonacci Number]/src/Solution.java new file mode 100644 index 0000000..4e5fa95 --- /dev/null +++ b/[0509][Fibonacci Number]/src/Solution.java @@ -0,0 +1,27 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:44 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int fib(int N) { + if (N <= 0) { + return 0; + } else if (N == 1) { + return 1; + } + + int n2 = 0; + int n1 = 1; + int n0; + for (int i = 2; i <= N; i++) { + n0 = n1 + n2; + n2 = n1; + n1 = n0; + } + + return n1; + } +} diff --git a/[0509][Fibonacci Number]/src/SolutionTest.java b/[0509][Fibonacci Number]/src/SolutionTest.java new file mode 100644 index 0000000..c959dd8 --- /dev/null +++ b/[0509][Fibonacci Number]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 08:48 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void fib() { + Solution s = new Solution(); + Object[][] data = { + {0, 0}, + {1, 1}, + {2, 1}, + {3, 2}, + }; + + for (Object[] d : data) { + Assert.assertEquals(s.fib((Integer) d[0]), d[1]); + } + } +} \ No newline at end of file diff --git a/[0520][Detect Capital]/[0520][Detect Capital].iml b/[0520][Detect Capital]/[0520][Detect Capital].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0520][Detect Capital]/[0520][Detect Capital].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0520][Detect Capital]/src/Solution.java b/[0520][Detect Capital]/src/Solution.java new file mode 100644 index 0000000..c8c6c13 --- /dev/null +++ b/[0520][Detect Capital]/src/Solution.java @@ -0,0 +1,73 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:42 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + *
+     * Given a word, you need to judge whether the usage of capitals in it is right or not.
+     *
+     * We define the usage of capitals in a word to be right when one of the following
+     * cases holds:
+     *
+     * All letters in this word are capitals, like "USA".
+     * All letters in this word are not capitals, like "leetcode".
+     * Only the first letter in this word is capital, like "Google".
+     * Otherwise, we define that this word doesn't use capitals in a right way.
+     *
+     *
+     * Example 1:
+     *
+     * Input: "USA"
+     * Output: True
+     *
+     *
+     * Example 2:
+     *
+     * Input: "FlaG"
+     * Output: False
+     *
+     *
+     * Note: The input will be a non-empty word consisting of uppercase
+     * and lowercase latin letters.
+     * 
+ * + * @param word + * @return + */ + public boolean detectCapitalUse(String word) { + + if (Character.isUpperCase(word.charAt(0))) { + return isAllUpperCase(word.substring(1)) || isAllLowerCase(word.substring(1)); + } else if (Character.isLowerCase(word.charAt(0))) { + return isAllLowerCase(word.substring(1)); + } + + return false; + } + + private boolean isAllLowerCase(String s) { + for (int i = 0; i < s.length(); i++) { + if (!Character.isLowerCase(s.charAt(i))) { + return false; + } + } + + return true; + } + + private boolean isAllUpperCase(String s) { + + for (int i = 0; i < s.length(); i++) { + if (!Character.isUpperCase(s.charAt(i))) { + return false; + } + } + + return true; + } +} + diff --git a/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0521][Longest Uncommon Subsequence I]/[0521][Longest Uncommon Subsequence I].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0521][Longest Uncommon Subsequence I]/src/Solution.java b/[0521][Longest Uncommon Subsequence I]/src/Solution.java new file mode 100644 index 0000000..7f99841 --- /dev/null +++ b/[0521][Longest Uncommon Subsequence I]/src/Solution.java @@ -0,0 +1,20 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 08:55 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * @param a + * @param b + * @return + */ + public int findLUSlength(String a, String b) { + if (a.equals(b)) { + return -1; + } + return Math.max(a.length(), b.length()); + } +} diff --git a/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/[0523][K Diff Pairs In An Array].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0523][K Diff Pairs In An Array]/src/Solution.java b/[0523][K Diff Pairs In An Array]/src/Solution.java new file mode 100644 index 0000000..c461f31 --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/src/Solution.java @@ -0,0 +1,40 @@ +import java.util.Arrays; + +/** + * Author: 王俊超 + * Time: 2020-06-30 08:52 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int findPairs(int[] nums, int k) { + if (nums == null || nums.length < 2 || k < 0) { + return 0; + } + + int result = 0; + + Arrays.sort(nums); + + for (int i = 0; i < nums.length - 1;i++) { + + for (int j = i + 1; j < nums.length && nums[j] - nums[i] <= k; j++) { + while (j + 1 < nums.length && nums[j] == nums[j + 1]) { + j++; + } + + if (nums[j] - nums[i] == k) { + result++; + } + } + + while (i + 1 < nums.length && nums[i] == nums[i + 1]) { + i++; + } + + } + + return result; + } +} diff --git a/[0523][K Diff Pairs In An Array]/src/SolutionTest.java b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java new file mode 100644 index 0000000..2e368fa --- /dev/null +++ b/[0523][K Diff Pairs In An Array]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-30 08:56 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findPairs() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{3, 1, 4, 1, 5}, 2, 2}, + {new int[]{1, 2, 3, 4, 5}, 1, 4}, + {new int[]{1, 3, 1, 5, 4}, 0, 1}, + {new int[]{1, 1, 1, 1, 1}, 0, 1}, + {new int[]{1, 1, 2, 2, 1}, 0, 2}, + }; + + for (Object[] d: data) { + Assert.assertEquals(d[2], s.findPairs((int[]) d[0], (Integer) d[1])); + } + } +} \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/[0530][Minimum Absolute Difference in BST].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/src/Solution.java b/[0530][Minimum Absolute Difference in BST]/src/Solution.java new file mode 100644 index 0000000..f42a167 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/Solution.java @@ -0,0 +1,93 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-06-29 10:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * + * @param root + * @return + */ + public int getMinimumDifference(TreeNode root) { + + if (root == null) { + return 0; + } + Deque s = new LinkedList<>(); + int min = Integer.MAX_VALUE; + int prev = 0; + boolean first = true; + while (root != null || !s.isEmpty()) { + while (root != null) { + s.push(root);//先访问再入栈 + root = root.left; + } + root = s.pop(); + + System.out.println(root.val); + if (first) { + first = false; + } else { + min = Math.min(min, root.val - prev); + } + + prev = root.val; + root = root.right;//如果是null,出栈并处理右子树 + } + + return min; + } + + public int getMinimumDifference2(TreeNode root) { + + if (root == null) { + return 0; + } + + List list = new ArrayList<>(); + + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + list.add(node.val); + if (node.right != null) { + queue.addFirst(node.right); + } + if (node.left != null) { + queue.addFirst(node.left); + } + } + + + if (list.size() == 1) { + return list.get(0); + } + + list.sort(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + int min = Integer.MAX_VALUE; + + for (int i = 1; i < list.size(); i++) { + min = Math.min(min, Math.abs(list.get(i - 1) - list.get(i))); + } + + return min; + } +} diff --git a/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java new file mode 100644 index 0000000..b0523af --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/SolutionTest.java @@ -0,0 +1,35 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-06-29 11:33 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void getMinimumDifference() { + TreeNode root = new TreeNode(1); + root.right = new TreeNode(3); + root.right.left = new TreeNode(2); + + Solution s = new Solution(); + Assert.assertEquals(s.getMinimumDifference(root), 1); + } + +// [236,104,701,null,227,null,911] + + @org.junit.Test + public void getMinimumDifference2() { + TreeNode root = new TreeNode(236); + root.left = new TreeNode(104); + root.left.right = new TreeNode(227); + root.right = new TreeNode(701); + root.right.right = new TreeNode(911); + + Solution s = new Solution(); + Assert.assertEquals(s.getMinimumDifference(root), 9); + } +} \ No newline at end of file diff --git a/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0530][Minimum Absolute Difference in BST]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} From bd6427277a8867557ed4ed7466ce829ddc36aa3c Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Tue, 30 Jun 2020 09:34:18 +0800 Subject: [PATCH 07/18] commit Change-Id: I885727ba0041e8de9560dce207138f93d16bbd3f --- .idea/workspace.xml | 38 +++++++++---------- .../src/Solution.java | 1 - 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 01614ce..3323cba 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -236,7 +236,7 @@ - + 1560552440693 @@ -608,10 +608,10 @@ - + - + @@ -619,10 +619,10 @@ - + - + @@ -630,10 +630,10 @@ - + - + @@ -641,10 +641,10 @@ - + - + @@ -652,28 +652,28 @@ - + - + - + - + - + - + - + - + @@ -721,10 +721,10 @@ - + - + diff --git a/[0530][Minimum Absolute Difference in BST]/src/Solution.java b/[0530][Minimum Absolute Difference in BST]/src/Solution.java index f42a167..603e965 100644 --- a/[0530][Minimum Absolute Difference in BST]/src/Solution.java +++ b/[0530][Minimum Absolute Difference in BST]/src/Solution.java @@ -35,7 +35,6 @@ public int getMinimumDifference(TreeNode root) { } root = s.pop(); - System.out.println(root.val); if (first) { first = false; } else { From 743227aeddd5b23d9fca340d96e8ddd812f20871 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Wed, 1 Jul 2020 09:25:28 +0800 Subject: [PATCH 08/18] commit Change-Id: Id41004d654d277ea27da6318cb54af6f85131431 --- .../[0538][Convert BST to Greater Tree].iml | 21 +++++ .../src/Solution.java | 11 +++ .../src/SolutionTest.java | 12 +++ .../src/TreeNode.java | 16 ++++ .../[0541][Reverse String II].iml | 11 +++ [0541][Reverse String II]/src/Solution.java | 11 +++ .../src/SolutionTest.java | 12 +++ .../[0543][Diameter of Binary Tree].iml | 11 +++ .../src/Solution.java | 11 +++ .../src/SolutionTest.java | 12 +++ .../src/TreeNode.java | 16 ++++ ...3][Minimum Distance Between BST Nodes].iml | 11 +++ .../src/Solution.java | 92 +++++++++++++++++++ .../src/TreeNode.java | 16 ++++ 14 files changed, 263 insertions(+) create mode 100644 [0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml create mode 100644 [0538][Convert BST to Greater Tree]/src/Solution.java create mode 100644 [0538][Convert BST to Greater Tree]/src/SolutionTest.java create mode 100644 [0538][Convert BST to Greater Tree]/src/TreeNode.java create mode 100644 [0541][Reverse String II]/[0541][Reverse String II].iml create mode 100644 [0541][Reverse String II]/src/Solution.java create mode 100644 [0541][Reverse String II]/src/SolutionTest.java create mode 100644 [0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml create mode 100644 [0543][Diameter of Binary Tree]/src/Solution.java create mode 100644 [0543][Diameter of Binary Tree]/src/SolutionTest.java create mode 100644 [0543][Diameter of Binary Tree]/src/TreeNode.java create mode 100644 [0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml create mode 100644 [0783][Minimum Distance Between BST Nodes]/src/Solution.java create mode 100644 [0783][Minimum Distance Between BST Nodes]/src/TreeNode.java diff --git a/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/[0538][Convert BST to Greater Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0538][Convert BST to Greater Tree]/src/Solution.java b/[0538][Convert BST to Greater Tree]/src/Solution.java new file mode 100644 index 0000000..53423a6 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/Solution.java @@ -0,0 +1,11 @@ +package PACKAGE_NAME; + +/** + * Author: 王俊超 + * Time: 2020-06-30 10:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { +} diff --git a/[0538][Convert BST to Greater Tree]/src/SolutionTest.java b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java new file mode 100644 index 0000000..de98b6a --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java @@ -0,0 +1,12 @@ +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-06-30 10:14 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + +} \ No newline at end of file diff --git a/[0538][Convert BST to Greater Tree]/src/TreeNode.java b/[0538][Convert BST to Greater Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0538][Convert BST to Greater Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0541][Reverse String II]/[0541][Reverse String II].iml b/[0541][Reverse String II]/[0541][Reverse String II].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0541][Reverse String II]/[0541][Reverse String II].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0541][Reverse String II]/src/Solution.java b/[0541][Reverse String II]/src/Solution.java new file mode 100644 index 0000000..1742f1b --- /dev/null +++ b/[0541][Reverse String II]/src/Solution.java @@ -0,0 +1,11 @@ +package PACKAGE_NAME; + +/** + * Author: 王俊超 + * Time: 2020-06-30 12:50 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { +} diff --git a/[0541][Reverse String II]/src/SolutionTest.java b/[0541][Reverse String II]/src/SolutionTest.java new file mode 100644 index 0000000..ae1b6bd --- /dev/null +++ b/[0541][Reverse String II]/src/SolutionTest.java @@ -0,0 +1,12 @@ +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-06-30 12:54 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + +} \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/src/Solution.java b/[0543][Diameter of Binary Tree]/src/Solution.java new file mode 100644 index 0000000..d7b5537 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/Solution.java @@ -0,0 +1,11 @@ +package PACKAGE_NAME; + +/** + * Author: 王俊超 + * Time: 2020-07-01 08:53 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { +} diff --git a/[0543][Diameter of Binary Tree]/src/SolutionTest.java b/[0543][Diameter of Binary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..8229f78 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/SolutionTest.java @@ -0,0 +1,12 @@ +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-01 08:57 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + +} \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/src/TreeNode.java b/[0543][Diameter of Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0543][Diameter of Binary Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/[0783][Minimum Distance Between BST Nodes].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0783][Minimum Distance Between BST Nodes]/src/Solution.java b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java new file mode 100644 index 0000000..603e965 --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java @@ -0,0 +1,92 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-06-29 10:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * + * @param root + * @return + */ + public int getMinimumDifference(TreeNode root) { + + if (root == null) { + return 0; + } + Deque s = new LinkedList<>(); + int min = Integer.MAX_VALUE; + int prev = 0; + boolean first = true; + while (root != null || !s.isEmpty()) { + while (root != null) { + s.push(root);//先访问再入栈 + root = root.left; + } + root = s.pop(); + + if (first) { + first = false; + } else { + min = Math.min(min, root.val - prev); + } + + prev = root.val; + root = root.right;//如果是null,出栈并处理右子树 + } + + return min; + } + + public int getMinimumDifference2(TreeNode root) { + + if (root == null) { + return 0; + } + + List list = new ArrayList<>(); + + Deque queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + list.add(node.val); + if (node.right != null) { + queue.addFirst(node.right); + } + if (node.left != null) { + queue.addFirst(node.left); + } + } + + + if (list.size() == 1) { + return list.get(0); + } + + list.sort(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1 - o2; + } + }); + + int min = Integer.MAX_VALUE; + + for (int i = 1; i < list.size(); i++) { + min = Math.min(min, Math.abs(list.get(i - 1) - list.get(i))); + } + + return min; + } +} diff --git a/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0783][Minimum Distance Between BST Nodes]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} From 4683e2914ab0e9562670ce4e554e07424bc83141 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Wed, 1 Jul 2020 09:25:55 +0800 Subject: [PATCH 09/18] commit Change-Id: I5b2163cca9a6ff8b4a7e72d91dab154f747dd7bd --- .idea/modules.xml | 4 + .idea/workspace.xml | 143 ++++++++++-------- .../src/Solution.java | 43 +++++- .../src/SolutionTest.java | 27 ++++ .../[0541][Reverse String II].iml | 10 ++ [0541][Reverse String II]/src/Solution.java | 25 ++- .../src/SolutionTest.java | 14 +- .../[0543][Diameter of Binary Tree].iml | 10 ++ .../src/Solution.java | 52 ++++++- .../src/SolutionTest.java | 13 ++ .../src/Solution.java | 44 +----- 11 files changed, 272 insertions(+), 113 deletions(-) diff --git a/.idea/modules.xml b/.idea/modules.xml index 63e77e6..24f3230 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -249,10 +249,14 @@ + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3323cba..eb7ed93 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,21 +2,20 @@ - - - - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -99,23 +98,23 @@ - - - + + + - - + + - + - - + + - - - + + + + + + - - - @@ -236,7 +235,10 @@ - + + + + 1560552440693 @@ -608,73 +610,85 @@ - + - + - + + + - + - + - + + + - + - + - + + + - + - + - + + + - + - + + - + - + + - + - + + - + - + + @@ -686,38 +700,39 @@ - + - + - + - + - + - + - + + - + - + - + diff --git a/[0538][Convert BST to Greater Tree]/src/Solution.java b/[0538][Convert BST to Greater Tree]/src/Solution.java index 53423a6..0e52f13 100644 --- a/[0538][Convert BST to Greater Tree]/src/Solution.java +++ b/[0538][Convert BST to Greater Tree]/src/Solution.java @@ -1,4 +1,5 @@ -package PACKAGE_NAME; +import java.util.Deque; +import java.util.LinkedList; /** * Author: 王俊超 @@ -8,4 +9,44 @@ * Declaration: All Rights Reserved !!! **/ public class Solution { + /** + * 解题思路 + * root提个二叉搜索树 + * 二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: + * 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; + * 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; + * 它的左、右子树也分别为二叉排序树。 + * 先处理右子树,再处理自己,再处理左子树 + * + * @param root + * @return + */ + public TreeNode convertBST(TreeNode root) { + + if (root == null) { + return null; + } + Deque s = new LinkedList<>(); + TreeNode prev = null; + TreeNode curr = root; + + while (curr != null || !s.isEmpty()) { + while (curr != null) { + s.push(curr);//先访问再入栈 + curr = curr.right; + } + curr = s.pop(); + + if (prev == null) { + prev = curr; + } else { + curr.val += prev.val; + } + + prev = curr; + curr = curr.left;//如果是null,出栈并处理左子树 + } + + return root; + } } diff --git a/[0538][Convert BST to Greater Tree]/src/SolutionTest.java b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java index de98b6a..5b181bf 100644 --- a/[0538][Convert BST to Greater Tree]/src/SolutionTest.java +++ b/[0538][Convert BST to Greater Tree]/src/SolutionTest.java @@ -9,4 +9,31 @@ **/ public class SolutionTest { + @org.junit.Test + public void convertBST() { + Solution s = new Solution(); + + TreeNode root = new TreeNode(4); + root.left = new TreeNode(2); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(3); + root.right =new TreeNode(6); + root.right.left = new TreeNode(5); + root.right.right = new TreeNode(7); + + s.convertBST(root); + + print(root); + } + + private void print(TreeNode root) { + if (root == null) { + return; + } + + print(root.left); + System.out.println(root.val); + print(root.right); + + } } \ No newline at end of file diff --git a/[0541][Reverse String II]/[0541][Reverse String II].iml b/[0541][Reverse String II]/[0541][Reverse String II].iml index c90834f..c70cbf1 100644 --- a/[0541][Reverse String II]/[0541][Reverse String II].iml +++ b/[0541][Reverse String II]/[0541][Reverse String II].iml @@ -7,5 +7,15 @@ + + + + + + + + + +
\ No newline at end of file diff --git a/[0541][Reverse String II]/src/Solution.java b/[0541][Reverse String II]/src/Solution.java index 1742f1b..df9a379 100644 --- a/[0541][Reverse String II]/src/Solution.java +++ b/[0541][Reverse String II]/src/Solution.java @@ -1,5 +1,3 @@ -package PACKAGE_NAME; - /** * Author: 王俊超 * Time: 2020-06-30 12:50 @@ -8,4 +6,27 @@ * Declaration: All Rights Reserved !!! **/ public class Solution { + public String reverseStr(String s, int k) { + if (s == null || s.length() < 2 || k < 1) { + return s; + } + char[] chars = s.toCharArray(); + int index = 0; + while (index < chars.length) { + swap(chars, index, Math.min(chars.length - 1, index + k - 1)); + index += 2 * k; + } + + return new String(chars); + } + + public void swap(char[] chars, int i, int j) { + while (i < j) { + char ch = chars[i]; + chars[i] = chars[j]; + chars[j] = ch; + i++; + j--; + } + } } diff --git a/[0541][Reverse String II]/src/SolutionTest.java b/[0541][Reverse String II]/src/SolutionTest.java index ae1b6bd..373a1d1 100644 --- a/[0541][Reverse String II]/src/SolutionTest.java +++ b/[0541][Reverse String II]/src/SolutionTest.java @@ -1,4 +1,4 @@ -import static org.junit.Assert.*; +import org.junit.Assert; /** * Author: 王俊超 @@ -9,4 +9,16 @@ **/ public class SolutionTest { + @org.junit.Test + public void reverseStr() { + Solution s = new Solution(); + Object[][] data = { + {"abcdefg", 2, "bacdfeg"}, + {"abcdefgh", 2, "bacdfegh"}, + }; + + for (Object[] d : data) { + Assert.assertEquals(d[2], s.reverseStr((String) d[0], (Integer) d[1])); + } + } } \ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml index c90834f..c70cbf1 100644 --- a/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml +++ b/[0543][Diameter of Binary Tree]/[0543][Diameter of Binary Tree].iml @@ -7,5 +7,15 @@ + + + + + + + + + +
\ No newline at end of file diff --git a/[0543][Diameter of Binary Tree]/src/Solution.java b/[0543][Diameter of Binary Tree]/src/Solution.java index d7b5537..5bca04a 100644 --- a/[0543][Diameter of Binary Tree]/src/Solution.java +++ b/[0543][Diameter of Binary Tree]/src/Solution.java @@ -1,5 +1,3 @@ -package PACKAGE_NAME; - /** * Author: 王俊超 * Time: 2020-07-01 08:53 @@ -8,4 +6,54 @@ * Declaration: All Rights Reserved !!! **/ public class Solution { + public int diameterOfBinaryTree(TreeNode root) { + return layerAndDiameter(root)[1]; + } + + /** + * 解题思路: + * 最大直径的三种情况: + * 1、在左子树中 + * 2、在右子树中 + * 3、对过root节点,如果经过root节点,那么直径就是 Height(root.left) + Height(root.right) + 2 + * 对每个节点都是同样的。所以可以使用递归求解,先求左右子树的高度和最大直径,再求通过root的高度和直径,再找出最大的直径 + * + * + * 第一个反回参数表示root最大有多少层, + * 第二个返回参数表示root的最大直径 + * + * @param root + * @return + */ + public int[] layerAndDiameter(TreeNode root) { + if (root == null) { // 边界条件 + return new int[]{0, 0}; + } + + if (root.left == null && root.right == null) { // 边界条件 + return new int[]{0, 0}; + } else if (root.left != null && root.right == null) { + int[] result = layerAndDiameter(root.left); + result[0] += 1; // 层升高1 + result[1] = Math.max(result[0], result[1]); // 因为没有右子树,最大直径要么经过root,要么不经过 + + return result; + } else if (root.left == null && root.right != null) { + int[] result = layerAndDiameter(root.right); + result[0] += 1; // 层升高1 + result[1] = Math.max(result[0], result[1]); // 因为没有左子树,最大直径要么经过root,要么不经过 + return result; + } + + int[] left = layerAndDiameter(root.left); + int[] right = layerAndDiameter(root.right); + + int[] result = {0, 0}; + result[0] = 1 + Math.max(left[0], right[0]); // 层升高1 + + result[1] = Math.max(left[1], right[1]); // 最大直径可能在左子树或者右子树中 + result[1] = Math.max(result[1], 2 + left[0] + right[0]); // 最大直径可能经过root + + return result; + } } diff --git a/[0543][Diameter of Binary Tree]/src/SolutionTest.java b/[0543][Diameter of Binary Tree]/src/SolutionTest.java index 8229f78..c3cab02 100644 --- a/[0543][Diameter of Binary Tree]/src/SolutionTest.java +++ b/[0543][Diameter of Binary Tree]/src/SolutionTest.java @@ -1,3 +1,5 @@ +import org.junit.Assert; + import static org.junit.Assert.*; /** @@ -9,4 +11,15 @@ **/ public class SolutionTest { + @org.junit.Test + public void diameterOfBinaryTree() { + Solution s = new Solution(); + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + root.left.left = new TreeNode(4); + root.left.right = new TreeNode(5); + + Assert.assertEquals(3, s.diameterOfBinaryTree(root)); + } } \ No newline at end of file diff --git a/[0783][Minimum Distance Between BST Nodes]/src/Solution.java b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java index 603e965..0230f1b 100644 --- a/[0783][Minimum Distance Between BST Nodes]/src/Solution.java +++ b/[0783][Minimum Distance Between BST Nodes]/src/Solution.java @@ -19,8 +19,7 @@ public class Solution { * @param root * @return */ - public int getMinimumDifference(TreeNode root) { - + public int minDiffInBST(TreeNode root) { if (root == null) { return 0; } @@ -48,45 +47,4 @@ public int getMinimumDifference(TreeNode root) { return min; } - public int getMinimumDifference2(TreeNode root) { - - if (root == null) { - return 0; - } - - List list = new ArrayList<>(); - - Deque queue = new LinkedList<>(); - queue.add(root); - while (!queue.isEmpty()) { - TreeNode node = queue.poll(); - list.add(node.val); - if (node.right != null) { - queue.addFirst(node.right); - } - if (node.left != null) { - queue.addFirst(node.left); - } - } - - - if (list.size() == 1) { - return list.get(0); - } - - list.sort(new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - return o1 - o2; - } - }); - - int min = Integer.MAX_VALUE; - - for (int i = 1; i < list.size(); i++) { - min = Math.min(min, Math.abs(list.get(i - 1) - list.get(i))); - } - - return min; - } } From 468285f92051304ea750522b2c5790a034ba35d5 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Thu, 2 Jul 2020 09:23:52 +0800 Subject: [PATCH 10/18] commit Change-Id: Iab81390de180fb722126be2d4520939693d4c78a --- .idea/modules.xml | 5 + .idea/workspace.xml | 264 +++++++----------- .../[0551][Student Attendance Record I].iml | 21 ++ .../src/Solution.java | 41 +++ .../src/SolutionTest.java | 27 ++ .../[0559][Maximum Depth of N-ary Tree].iml | 21 ++ .../src/Node.java | 25 ++ .../src/Solution.java | 27 ++ .../src/SolutionTest.java | 31 ++ .../[0563][Binary Tree Tilt].iml | 21 ++ [0563][Binary Tree Tilt]/src/Solution.java | 47 ++++ .../src/SolutionTest.java | 22 ++ [0563][Binary Tree Tilt]/src/TreeNode.java | 16 ++ .../[0566][Reshape the Matrix].iml | 21 ++ [0566][Reshape the Matrix]/src/Solution.java | 29 ++ .../src/SolutionTest.java | 32 +++ .../[0572][Subtree of Another Tree].iml | 21 ++ .../src/Solution.java | 56 ++++ .../src/SolutionTest.java | 46 +++ .../src/TreeNode.java | 22 ++ 20 files changed, 637 insertions(+), 158 deletions(-) create mode 100644 [0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml create mode 100644 [0551][Student Attendance Record I]/src/Solution.java create mode 100644 [0551][Student Attendance Record I]/src/SolutionTest.java create mode 100644 [0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml create mode 100644 [0559][Maximum Depth of N-ary Tree]/src/Node.java create mode 100644 [0559][Maximum Depth of N-ary Tree]/src/Solution.java create mode 100644 [0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java create mode 100644 [0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml create mode 100644 [0563][Binary Tree Tilt]/src/Solution.java create mode 100644 [0563][Binary Tree Tilt]/src/SolutionTest.java create mode 100644 [0563][Binary Tree Tilt]/src/TreeNode.java create mode 100644 [0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml create mode 100644 [0566][Reshape the Matrix]/src/Solution.java create mode 100644 [0566][Reshape the Matrix]/src/SolutionTest.java create mode 100644 [0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml create mode 100644 [0572][Subtree of Another Tree]/src/Solution.java create mode 100644 [0572][Subtree of Another Tree]/src/SolutionTest.java create mode 100644 [0572][Subtree of Another Tree]/src/TreeNode.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 24f3230..c99a8c1 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -253,8 +253,13 @@ + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index eb7ed93..1aca74a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,20 +2,24 @@ - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + @@ -84,70 +88,69 @@ - + + + + + - + - - - - - - - + + + - - + + - - + + - - + + - - + + - - - - - + + + + + - - - - - + + + + + @@ -238,7 +241,8 @@ - + + 1560552440693 @@ -598,10 +602,11 @@ - - + + + @@ -610,12 +615,13 @@ - + - + + @@ -623,12 +629,13 @@ - + - + + @@ -636,12 +643,13 @@ - + - + + @@ -649,12 +657,13 @@ - + - + + @@ -662,33 +671,37 @@ - + - + + - + - + + - + - + + - + - + + @@ -709,28 +722,28 @@ - + - + - + - + - + - + @@ -995,6 +1008,27 @@ 19 - - - - course == 1 - JAVA - EXPRESSION - - - i == 3 - JAVA - EXPRESSION - - - index >= s.length() - JAVA - EXPRESSION - - - path.size()==4 - JAVA - EXPRESSION - - - path.size() == 3 - JAVA - EXPRESSION - - - idx >= nums.length - JAVA - EXPRESSION - - - start2 + k - 1 >= nums2.length - JAVA - EXPRESSION - - - - - t.charAt(i) - JAVA - EXPRESSION - - - (2 << h1) - 1 - JAVA - EXPRESSION - - - dependency[1] - JAVA - EXPRESSION - - - operator.subList(i + 1, operator.size()) - JAVA - EXPRESSION - - - operand.subList(i + 1, operand.size()) - JAVA - EXPRESSION - - - new ArrayList<>(operator.subList(0, i)) - JAVA - EXPRESSION - - - operand.subList(0, i + 1) - JAVA - EXPRESSION - - - nums[i - 1] - 1 - JAVA - EXPRESSION - - - result.first() - JAVA - EXPRESSION - - - \ No newline at end of file diff --git a/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0551][Student Attendance Record I]/[0551][Student Attendance Record I].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0551][Student Attendance Record I]/src/Solution.java b/[0551][Student Attendance Record I]/src/Solution.java new file mode 100644 index 0000000..3ca6969 --- /dev/null +++ b/[0551][Student Attendance Record I]/src/Solution.java @@ -0,0 +1,41 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:05 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean checkRecord(String s) { + int a = 0; + int l = 0; + int preLIndex = -1; + if (s != null) { + for (int i = 0; i < s.length(); i++) { + switch (s.charAt(i)) { + case 'A': + a++; + break; + case 'L': + if (l < 3) { // 连续两个L后面步不用记录了 + if (preLIndex == -1) { // 刚开始记录 + preLIndex = i; + l = 1; + } else if (preLIndex + 1 == i) { // 当前和前一个都是L + l++; + preLIndex = i; + } else { // 前一个不是L,重新记数 + preLIndex = i; + l = 1; + } + } + break; + default: + // do nothing + } + } + } + + return a <= 1 && l < 3; + } +} diff --git a/[0551][Student Attendance Record I]/src/SolutionTest.java b/[0551][Student Attendance Record I]/src/SolutionTest.java new file mode 100644 index 0000000..4c102f4 --- /dev/null +++ b/[0551][Student Attendance Record I]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; +import org.junit.Test; + + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:12 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @Test + public void checkRecord() { + Solution s = new Solution(); + Object[][] data = { + {"PPALLP", true}, + {"PPALLL", false}, + }; + + for (Object[] d : data) { + Assert.assertEquals(d[1], s.checkRecord((String) d[0])); + } + } + +} \ No newline at end of file diff --git a/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/[0559][Maximum Depth of N-ary Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Node.java b/[0559][Maximum Depth of N-ary Tree]/src/Node.java new file mode 100644 index 0000000..bf5b87a --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/Node.java @@ -0,0 +1,25 @@ +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-02 07:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Node { + public int val; + public List children; + + public Node() { + } + + public Node(int val) { + this.val = val; + } + + public Node(int val, List children) { + this.val = val; + this.children = children; + } +} diff --git a/[0559][Maximum Depth of N-ary Tree]/src/Solution.java b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java new file mode 100644 index 0000000..d43fa08 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/Solution.java @@ -0,0 +1,27 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 07:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int maxDepth(Node root) { + if (root == null) { + return 0; + } + + if (root.children == null || root.children.isEmpty()) { + return 1; + } + + int max = 0; + + for (Node child : root.children) { + max = Math.max(max, maxDepth(child)); + } + + return 1 + max; + } +} + diff --git a/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java new file mode 100644 index 0000000..4aebf27 --- /dev/null +++ b/[0559][Maximum Depth of N-ary Tree]/src/SolutionTest.java @@ -0,0 +1,31 @@ +import org.junit.Assert; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:00 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void maxDepth() { + Node root = new Node(1); + root.children = new ArrayList<>(); + root.children.add(new Node(3)); + root.children.add(new Node(2)); + root.children.add(new Node(4)); + root.children.get(0).children = new ArrayList<>(); + root.children.get(0).children.add(new Node(5)); + root.children.get(0).children.add(new Node(6)); + + Solution s =new Solution(); + + Assert.assertEquals(3, s.maxDepth(root)); + } +} \ No newline at end of file diff --git a/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0563][Binary Tree Tilt]/[0563][Binary Tree Tilt].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0563][Binary Tree Tilt]/src/Solution.java b/[0563][Binary Tree Tilt]/src/Solution.java new file mode 100644 index 0000000..a3ae597 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/Solution.java @@ -0,0 +1,47 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:26 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 解题思路 + * 一个树的坡度等于=左子树的坡度+右子树的坡度+根节点的坡度 + * 子树同样适应 + * + * @param root + * @return + */ + public int findTilt(TreeNode root) { + return findTiltHelp(root)[0]; + } + + /** + * 第一个返回值表示root的坡度 + * 第二个返回值表示root的为根的树的节点和 + * + * @param root + * @return + */ + public int[] findTiltHelp(TreeNode root) { + // 空结点或者左右孩子为都空的结点 + if (root == null) { + return new int[]{0, 0}; + } else if (root.left == null && root.right == null) { + return new int[]{0, root.val}; + } + + + int[] left = findTiltHelp(root.left); + int[] right = findTiltHelp(root.right); + + int[] result = {0, 0}; + + result[0] = left[0] + right[0] + Math.abs(left[1] - right[1]); + result[1] = left[1] + right[1] + root.val; + + return result; + } +} diff --git a/[0563][Binary Tree Tilt]/src/SolutionTest.java b/[0563][Binary Tree Tilt]/src/SolutionTest.java new file mode 100644 index 0000000..358cd67 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/SolutionTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findTilt() { + Solution s = new Solution(); + TreeNode root = new TreeNode(1); + root.left = new TreeNode(2); + root.right = new TreeNode(3); + Assert.assertEquals(1, s.findTilt(root)); + } +} \ No newline at end of file diff --git a/[0563][Binary Tree Tilt]/src/TreeNode.java b/[0563][Binary Tree Tilt]/src/TreeNode.java new file mode 100644 index 0000000..8b88e24 --- /dev/null +++ b/[0563][Binary Tree Tilt]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Date: 2015-08-21 + * Time: 18:45 + * Declaration: All Rights Reserved !!! + */ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + diff --git a/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0566][Reshape the Matrix]/[0566][Reshape the Matrix].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0566][Reshape the Matrix]/src/Solution.java b/[0566][Reshape the Matrix]/src/Solution.java new file mode 100644 index 0000000..f4516d8 --- /dev/null +++ b/[0566][Reshape the Matrix]/src/Solution.java @@ -0,0 +1,29 @@ +/** + * Author: 王俊超 + * Time: 2020-07-02 08:43 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int[][] matrixReshape(int[][] nums, int r, int c) { + int row = nums.length; + int col = nums[0].length; + if (row * col != r * c) { + return nums; + } + + int[][] result = new int[r][c]; + for (int i = 0; i < r; i++) { + result[i] = new int[c]; + } + + int num = row * col; + + for (int i = 0; i < num; i++) { + result[i / c][i % c] = nums[i / col][i % col]; + } + + return result; + } +} diff --git a/[0566][Reshape the Matrix]/src/SolutionTest.java b/[0566][Reshape the Matrix]/src/SolutionTest.java new file mode 100644 index 0000000..d21e1f9 --- /dev/null +++ b/[0566][Reshape the Matrix]/src/SolutionTest.java @@ -0,0 +1,32 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 08:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void matrixReshape() { + Solution s = new Solution(); + Object[][] data = { + {new int[][]{{1, 2}, {3, 4}}, 1, 4, new int[][]{{1, 2, 3, 4}}}, + {new int[][]{{1, 2}, {3, 4}}, 2, 4, new int[][]{{1, 2}, {3, 4}}}, + }; + + for (Object[] d : data) { + int[][] result = s.matrixReshape((int[][]) d[0], (Integer) d[1], (Integer) d[2]); + int[][] actual = (int[][]) d[3]; + Assert.assertEquals(result.length, actual.length); + + for (int i = 0; i < result.length; i++) { + Assert.assertArrayEquals(actual[i], result[i]); + } + } + } +} \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0572][Subtree of Another Tree]/[0572][Subtree of Another Tree].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/src/Solution.java b/[0572][Subtree of Another Tree]/src/Solution.java new file mode 100644 index 0000000..f14f241 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/Solution.java @@ -0,0 +1,56 @@ +import java.util.Deque; +import java.util.LinkedList; + +/** + * Author: 王俊超 + * Time: 2020-07-02 09:02 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean isSubtree(TreeNode s, TreeNode t) { + + if (s == t || t == null) { + return true; + } + + if (s == null) { + return false; + } + + Deque queue = new LinkedList<>(); + queue.addFirst(s); + while (!queue.isEmpty()) { + TreeNode root = queue.removeFirst(); + if (root.right != null) { + queue.addFirst(root.right); + } + if (root.left != null) { + queue.addFirst(root.left); + } + + if(isSame(root, t)) { + return true; + } + + } + + return false; + } + + public boolean isSame(TreeNode s, TreeNode t) { + if (s == t) { + return true; + } + + if (s == null || t == null) { + return false; + } + + boolean leftSame = isSame(s.left, t.left); + boolean rightSame = isSame(s.right, t.right); + + return s.val == t.val && leftSame && rightSame; + } +} diff --git a/[0572][Subtree of Another Tree]/src/SolutionTest.java b/[0572][Subtree of Another Tree]/src/SolutionTest.java new file mode 100644 index 0000000..15b8f13 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/SolutionTest.java @@ -0,0 +1,46 @@ +import org.junit.Assert; + +import static org.junit.Assert.*; + +/** + * Author: 王俊超 + * Time: 2020-07-02 09:11 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void isSubtree() { + Solution s = new Solution(); + TreeNode root = new TreeNode(3); + root.left = new TreeNode(4); + root.right = new TreeNode(5); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(2); + + TreeNode t = new TreeNode(4); + t.left = new TreeNode(1); + t.right = new TreeNode(2); + + Assert.assertEquals(true, s.isSubtree(root, t)); + } + + @org.junit.Test + public void isSubtree2() { + Solution s = new Solution(); + TreeNode root = new TreeNode(3); + root.left = new TreeNode(4); + root.right = new TreeNode(5); + root.left.left = new TreeNode(1); + root.left.right = new TreeNode(2); + root.left.right.left = new TreeNode(0); + + TreeNode t = new TreeNode(4); + t.left = new TreeNode(1); + t.right = new TreeNode(2); + + Assert.assertEquals(false, s.isSubtree(root, t)); + } +} \ No newline at end of file diff --git a/[0572][Subtree of Another Tree]/src/TreeNode.java b/[0572][Subtree of Another Tree]/src/TreeNode.java new file mode 100644 index 0000000..ac7b907 --- /dev/null +++ b/[0572][Subtree of Another Tree]/src/TreeNode.java @@ -0,0 +1,22 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} From 931f4b3cd7421c8c67339e960b3a4571ba58d5e7 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Sat, 4 Jul 2020 15:07:54 +0800 Subject: [PATCH 11/18] commit Change-Id: I98a51095df2f343d8a10ba5542749cb0f34e35ed --- .idea/modules.xml | 3 + .idea/workspace.xml | 186 ++++++++++-------- .../[0575][Distribute Candies].iml | 21 ++ [0575][Distribute Candies]/src/Solution.java | 32 +++ .../src/SolutionTest.java | 27 +++ .../[0589][N-ary Tree Preorder Traversal].iml | 11 ++ .../src/Node.java | 24 +++ .../src/Solution.java | 56 ++++++ ...[0590][N-ary Tree postorder Traversal].iml | 21 ++ .../src/Node.java | 24 +++ .../src/Solution.java | 63 ++++++ .../src/SolutionTest.java | 35 ++++ 12 files changed, 418 insertions(+), 85 deletions(-) create mode 100644 [0575][Distribute Candies]/[0575][Distribute Candies].iml create mode 100644 [0575][Distribute Candies]/src/Solution.java create mode 100644 [0575][Distribute Candies]/src/SolutionTest.java create mode 100644 [0589][N-ary Tree Preorder Traversal]/[0589][N-ary Tree Preorder Traversal].iml create mode 100644 [0589][N-ary Tree Preorder Traversal]/src/Node.java create mode 100644 [0589][N-ary Tree Preorder Traversal]/src/Solution.java create mode 100644 [0590][N-ary Tree postorder Traversal]/[0590][N-ary Tree postorder Traversal].iml create mode 100644 [0590][N-ary Tree postorder Traversal]/src/Node.java create mode 100644 [0590][N-ary Tree postorder Traversal]/src/Solution.java create mode 100644 [0590][N-ary Tree postorder Traversal]/src/SolutionTest.java diff --git a/.idea/modules.xml b/.idea/modules.xml index c99a8c1..e6b4609 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -260,6 +260,9 @@ + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1aca74a..a5b758c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,24 +2,16 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + @@ -77,47 +69,41 @@ + - + + + + - - - + + + - - - + + + - - - + + + - - + + + + - - @@ -242,7 +239,9 @@ - + + + 1560552440693 @@ -615,13 +614,15 @@ - - + + - + - + + + @@ -629,13 +630,15 @@ - - + + - + - + + + @@ -643,13 +646,15 @@ - - + + - + - + + + @@ -657,13 +662,15 @@ - - + + - + - + + + @@ -671,37 +678,41 @@ - - + + - + - + + - - + + - + - + + - - + + - + - + + - - + + - + - + + @@ -713,39 +724,39 @@ - - + + - + - - + + - + - - + + - + - - + + - + @@ -1029,6 +1040,11 @@ 32 1560552440693 @@ -614,10 +615,10 @@ - - + + - + @@ -630,10 +631,10 @@ - - + + - + @@ -646,10 +647,10 @@ - - + + - + @@ -662,10 +663,10 @@ - - + + - + @@ -678,37 +679,37 @@ - - + + - + - - + + - + - - + + - + - - + + - + @@ -733,28 +734,32 @@ - - + + - + - - + + - + + + + + - - + + - + @@ -1045,6 +1050,11 @@ 42 + + + \ No newline at end of file diff --git a/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/[0594][Longest Harmonious Subsequence].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0594][Longest Harmonious Subsequence]/src/Solution.java b/[0594][Longest Harmonious Subsequence]/src/Solution.java new file mode 100644 index 0000000..d3ec9aa --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/src/Solution.java @@ -0,0 +1,78 @@ +import java.util.Arrays; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +/** + * Author: 王俊超 + * Time: 2020-07-04 15:09 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + /** + * 求每个元素的个数,再找相邻元素之和最大的 + * 相邻指元素之差为1 + * + * @param nums + * @return + */ + public int findLHS(int[] nums) { + if (nums == null || nums.length < 2) { + return 0; + } + + int max = 0; + Arrays.sort(nums); + + int prevNum = Integer.MIN_VALUE; + int prevCnt = 0; + for (int i = 0; i < nums.length; ) { + int next = i + 1; + while (next < nums.length && nums[next] == nums[i]) { + next++; + } + if (prevNum + 1 == nums[i]) { + max = Math.max(max, prevCnt + next - i); + } + + prevNum = nums[i]; + prevCnt = next - i; + i = next; + + } + + return max; + } + + /** + * 求每个元素的个数,再找相邻元素之和最大的 + * 相邻指元素之差为1 + * + * @param nums + * @return + */ + public int findLHS2(int[] nums) { + if (nums == null || nums.length < 2) { + return 0; + } + + SortedMap map = new TreeMap<>(); + for (int n : nums) { + map.put(n, map.getOrDefault(n, 0) + 1); + } + + int max = 0; + Map.Entry prev = null; // 前一个遍历的元素 + for (Map.Entry entry : map.entrySet()) { + if (prev != null && prev.getKey() + 1 == entry.getKey()) { // 相邻元素 + max = Math.max(max, prev.getValue() + entry.getValue()); // 取较大的 + } + prev = entry; + } + + return max; + } +} diff --git a/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java new file mode 100644 index 0000000..f51d032 --- /dev/null +++ b/[0594][Longest Harmonious Subsequence]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 15:20 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findLHS() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 3, 2, 2, 5, 2, 3, 7}, 5}, + {new int[]{-2, 0, 2, 2, 5, 5, 5, 5}, 0}, + {new int[]{1, 1, 1, 1}, 0}, + }; + + for (Object[] d : data) { + int result = s.findLHS((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0598][Range Addition II]/[0598][Range Addition II].iml b/[0598][Range Addition II]/[0598][Range Addition II].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0598][Range Addition II]/[0598][Range Addition II].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0598][Range Addition II]/src/Solution.java b/[0598][Range Addition II]/src/Solution.java new file mode 100644 index 0000000..49747ce --- /dev/null +++ b/[0598][Range Addition II]/src/Solution.java @@ -0,0 +1,29 @@ +/** + * Author: 王俊超 + * Time: 2020-07-04 15:50 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + /** + * 数组的第一元素一定是最大的,要找最大的元素个数,只要找ops中min(ops[0])*min(ops[1]) + * + * @param m + * @param n + * @param ops + * @return + */ + public int maxCount(int m, int n, int[][] ops) { + + + int row = m; + int col = n; + for (int[] op : ops) { + row = Math.min(op[0], row); + col = Math.min(op[1], col); + } + + return row * col; + } +} diff --git a/[0598][Range Addition II]/src/SolutionTest.java b/[0598][Range Addition II]/src/SolutionTest.java new file mode 100644 index 0000000..8550371 --- /dev/null +++ b/[0598][Range Addition II]/src/SolutionTest.java @@ -0,0 +1,26 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:04 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void maxCount() { + Solution s = new Solution(); + Object[][] data = { + {3, 3, new int[][]{{2, 2}, {3, 3}}, 4}, + {3, 3, new int[][]{{2, 3}, {3, 2}}, 4}, + {3, 3, new int[][]{}, 9}, + }; + + for (Object[] d : data) { + Object result = s.maxCount((int) d[0], (int)d[1], (int[][]) d[2]); + Assert.assertEquals(d[3], result); + } + } +} \ No newline at end of file diff --git a/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/[0599][Minimum Index Sum of Two Lists].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0599][Minimum Index Sum of Two Lists]/src/Solution.java b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java new file mode 100644 index 0000000..238a202 --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/src/Solution.java @@ -0,0 +1,39 @@ +import java.util.*; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:13 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public String[] findRestaurant(String[] list1, String[] list2) { + if (list1 == null || list2 == null){ + return null; + } + + Map map = new HashMap<>(); + for (int i = 0; i < list1.length; i++) { + map.put(list1[i], i); + } + List result = new ArrayList<>(list1.length); + + int min = Integer.MAX_VALUE; + + for (int i = 0; i < list2.length; i++) { + if (map.containsKey(list2[i])) { + if (map.get(list2[i]) + i < min) { + result.clear(); + min = map.get(list2[i]) + i; + result.add(list2[i]); + } else if (map.get(list2[i]) + i == min) { + result.add(list2[i]); + } + + } + } + + return result.toArray(new String[0]); + } +} diff --git a/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java new file mode 100644 index 0000000..671b81b --- /dev/null +++ b/[0599][Minimum Index Sum of Two Lists]/src/SolutionTest.java @@ -0,0 +1,34 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:23 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findRestaurant() { + Solution s = new Solution(); + Object[][] data = { + { + new String[]{"Shogun", "Tapioca Express", "Burger King", "KFC"}, + new String[]{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"}, + new String[]{"Shogun"}, + }, + { + new String[]{"Shogun","Tapioca Express","Burger King","KFC"}, + new String[]{"KFC","Shogun","Burger King"}, + new String[]{"Shogun"}, + } + + }; + + for (Object[] d : data) { + String[] result = s.findRestaurant((String[]) d[0], (String[]) d[1]); + Assert.assertArrayEquals((String[]) d[2], result); + } + } +} \ No newline at end of file diff --git a/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/[0637][Average of Levels in Binary Tree].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0637][Average of Levels in Binary Tree]/src/Solution.java b/[0637][Average of Levels in Binary Tree]/src/Solution.java new file mode 100644 index 0000000..9e113fb --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/src/Solution.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-04 16:32 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public List averageOfLevels(TreeNode root) { + List result = new ArrayList<>(); + + if (root == null) { + return result; + } + + Deque curr = new LinkedList<>(); + Deque next = new LinkedList<>(); + curr.addFirst(root); + + while (!curr.isEmpty()) { + + double count = curr.size(); + double sum = 0; + while (!curr.isEmpty()) { + TreeNode node = curr.removeFirst(); + sum += node.val; + + if (node.left != null) { + next.addLast(node.left); + } + if (node.right != null) { + next.addLast(node.right); + } + } + + result.add(sum / count); + curr = next; + next = new LinkedList<>(); + } + + return result; + } +} diff --git a/[0637][Average of Levels in Binary Tree]/src/TreeNode.java b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java new file mode 100644 index 0000000..5554849 --- /dev/null +++ b/[0637][Average of Levels in Binary Tree]/src/TreeNode.java @@ -0,0 +1,13 @@ +/** + * @author: wangjunchao(王俊超) + * @time: 2019-07-09 18:52 + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} From 42224d22c543fb9b4e911c8ad0260f3a622a57b4 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Sat, 4 Jul 2020 20:56:46 +0800 Subject: [PATCH 13/18] commit Change-Id: I691b70a8c8e75476ab877915ebe2dfc7f7a0dc34 --- .idea/modules.xml | 5 + .idea/workspace.xml | 122 ++++++++++-------- ...Shortest Unsorted Continuous Subarray].iml | 21 +++ .../src/Solution.java | 39 ++++++ .../src/SolutionTest.java | 26 ++++ .../[0605][Can Place Flowers].iml | 21 +++ [0605][Can Place Flowers]/src/Solution.java | 54 ++++++++ .../src/SolutionTest.java | 31 +++++ ...06][Construct String from Binary Tree].iml | 21 +++ .../src/Solution.java | 41 ++++++ .../src/SolutionTest.java | 33 +++++ .../src/TreeNode.java | 16 +++ ...628][Maximum Product of Three Numbers].iml | 21 +++ .../src/Solution.java | 29 +++++ .../src/SolutionTest.java | 24 ++++ .../[0657][Robot Return to Origin].iml | 11 ++ .../src/Solution.java | 34 +++++ 17 files changed, 492 insertions(+), 57 deletions(-) create mode 100644 [0581][Shortest Unsorted Continuous Subarray]/[0581][Shortest Unsorted Continuous Subarray].iml create mode 100644 [0581][Shortest Unsorted Continuous Subarray]/src/Solution.java create mode 100644 [0581][Shortest Unsorted Continuous Subarray]/src/SolutionTest.java create mode 100644 [0605][Can Place Flowers]/[0605][Can Place Flowers].iml create mode 100644 [0605][Can Place Flowers]/src/Solution.java create mode 100644 [0605][Can Place Flowers]/src/SolutionTest.java create mode 100644 [0606][Construct String from Binary Tree]/[0606][Construct String from Binary Tree].iml create mode 100644 [0606][Construct String from Binary Tree]/src/Solution.java create mode 100644 [0606][Construct String from Binary Tree]/src/SolutionTest.java create mode 100644 [0606][Construct String from Binary Tree]/src/TreeNode.java create mode 100644 [0628][Maximum Product of Three Numbers]/[0628][Maximum Product of Three Numbers].iml create mode 100644 [0628][Maximum Product of Three Numbers]/src/Solution.java create mode 100644 [0628][Maximum Product of Three Numbers]/src/SolutionTest.java create mode 100644 [0657][Robot Return to Origin]/[0657][Robot Return to Origin].iml create mode 100644 [0657][Robot Return to Origin]/src/Solution.java diff --git a/.idea/modules.xml b/.idea/modules.xml index 84d7a15..eeab3fb 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -261,13 +261,18 @@ + + + + + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f34fe15..2c0e64c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,18 +2,20 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + @@ -100,9 +102,9 @@ - + - + - - + + - - + + - - + + - - + + - - - - + + + + + - - - - + + + @@ -242,7 +245,7 @@ - + 1560552440693 @@ -615,10 +618,10 @@ - + - + @@ -631,10 +634,10 @@ - + - + @@ -647,10 +650,10 @@ - + - + @@ -663,10 +666,10 @@ - + - + @@ -679,37 +682,37 @@ - + - + - + - + - + - + - + - + @@ -734,18 +737,18 @@ - + - + - + - + @@ -756,10 +759,10 @@ - + - + @@ -1055,6 +1058,11 @@ 23 1560552440693 @@ -618,10 +617,10 @@ - + - + @@ -634,10 +633,10 @@ - + - + @@ -650,10 +649,10 @@ - + - + @@ -666,10 +665,10 @@ - + - + @@ -682,37 +681,37 @@ - + - + - + - + - + - + - + - + @@ -737,18 +736,18 @@ - + - + - + - + @@ -759,10 +758,10 @@ - + - + @@ -1063,6 +1062,16 @@ 18 1560552440693 @@ -617,10 +615,10 @@ - + - + @@ -633,10 +631,10 @@ - + - + @@ -649,10 +647,10 @@ - + - + @@ -665,10 +663,10 @@ - + - + @@ -681,37 +679,37 @@ - + - + - + - + - + - + - + - + @@ -736,18 +734,18 @@ - + - + - + - + @@ -758,15 +756,19 @@ - + - + + + + + @@ -1069,9 +1071,19 @@ file://$PROJECT_DIR$/[0653][Two Sum IV - Input is a BST]/src/Solution.java - 34 + 49 + + file://$PROJECT_DIR$/[0661][Image Smoother]/src/SolutionTest.java + 23 + + + file://$PROJECT_DIR$/[0661][Image Smoother]/src/Solution.java + 25 + file://$PROJECT_DIR$/[081][Search In Rotated Sorted Array II]/src/Solution2.java 17 diff --git a/[0661][Image Smoother]/[0661][Image Smoother].iml b/[0661][Image Smoother]/[0661][Image Smoother].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0661][Image Smoother]/[0661][Image Smoother].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0661][Image Smoother]/src/Solution.java b/[0661][Image Smoother]/src/Solution.java new file mode 100644 index 0000000..a92e9d8 --- /dev/null +++ b/[0661][Image Smoother]/src/Solution.java @@ -0,0 +1,32 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:11 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + public int[][] imageSmoother(int[][] M) { + int[][] result = new int[M.length][]; + for (int i = 0; i < M.length; i++) { + result[i] = new int[M[i].length]; + } + + for (int i = 0; i < M.length; i++) { + for (int j = 0; j < M[0].length; j++) { + int count = 0; + int sum = 0; + for (int u = Math.max(0, i - 1); u <= i + 1 && u < M.length; u++) { + for (int v = Math.max(0, j - 1); v <= j + 1 && v < M[0].length; v++) { + count++; + sum += M[u][v]; + } + } + result[i][j] = sum / count; + } + } + + return result; + } +} diff --git a/[0661][Image Smoother]/src/SolutionTest.java b/[0661][Image Smoother]/src/SolutionTest.java new file mode 100644 index 0000000..311d241 --- /dev/null +++ b/[0661][Image Smoother]/src/SolutionTest.java @@ -0,0 +1,29 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-06 08:17 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void imageSmoother() { + Solution s = new Solution(); + Object[][] data = { + { + new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, + new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, + }, + }; + + for (Object[] d : data) { + int[][] result = s.imageSmoother((int[][]) d[0]); + for (int i = 0; i < result.length; i++) { + Assert.assertArrayEquals(((int[][]) d[1])[i], result[i]); + } + } + } +} \ No newline at end of file diff --git a/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0665][Non-decreasing Array]/[0665][Non-decreasing Array].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0665][Non-decreasing Array]/src/Solution.java b/[0665][Non-decreasing Array]/src/Solution.java new file mode 100644 index 0000000..db06448 --- /dev/null +++ b/[0665][Non-decreasing Array]/src/Solution.java @@ -0,0 +1,34 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:27 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + /** + * 解题思路: + * 其实说的简单点,有2种情况: + * 升序序列中,突然有个数字小了,则nums[i]=nums[i-1] + * 升序序列中,突然有个数字大了,则nums[i-1]=nums[i] + * + * @param nums + * @return + */ + public boolean checkPossibility(int[] nums) { + int fix = 0; + for (int i = 1; i < nums.length && fix <= 1; i++) { + if (nums[i] >= nums[i - 1]) { + continue; + } + fix++; + if (i - 2 >= 0 && (nums[i] < nums[i - 2])) { // 突然有个数字小了 + nums[i] = nums[i - 1]; + } else {// 突然有个数字大了 + nums[i - 1] = nums[i]; + } + } + return fix <= 1; + } +} diff --git a/[0665][Non-decreasing Array]/src/SolutionTest.java b/[0665][Non-decreasing Array]/src/SolutionTest.java new file mode 100644 index 0000000..1667886 --- /dev/null +++ b/[0665][Non-decreasing Array]/src/SolutionTest.java @@ -0,0 +1,28 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-06 08:38 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void checkPossibility() { + Solution s = new Solution(); + Object[][] data = { +// {new int[]{1, 2, 3, 4, 6, 5, 7, 8, 9}, true}, +// {new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, true}, +// {new int[]{4, 2, 3}, true}, +// {new int[]{4, 2, 1}, false}, + {new int[]{-1, 4, 2, 3}, false}, + }; + + for (Object[] d : data) { + boolean result = s.checkPossibility((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/[0669][Trim a Binary Search Tree].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0669][Trim a Binary Search Tree]/src/Solution.java b/[0669][Trim a Binary Search Tree]/src/Solution.java new file mode 100644 index 0000000..8bc8fa1 --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/src/Solution.java @@ -0,0 +1,33 @@ +/** + * Author: 王俊超 + * Time: 2020-07-06 08:58 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public TreeNode trimBST(TreeNode root, int L, int R) { + if (root == null) { + return null; + } + + if (root.val == L) { + root.left = null; + root.right = trimBST(root, L, R); + return root; + } else if (root.val == R) { + root.right = null; + root.left = trimBST(root.left, L, R); + return root; + } else if (root.val < L) { + return trimBST(root.right, L, R); + } else if (root.val > R) { + return trimBST(root.left, L, R); + } + + root.left = trimBST(root.left, L, R); + root.right = trimBST(root.right, L, R); + + return root; + } +} diff --git a/[0669][Trim a Binary Search Tree]/src/TreeNode.java b/[0669][Trim a Binary Search Tree]/src/TreeNode.java new file mode 100644 index 0000000..bff99a6 --- /dev/null +++ b/[0669][Trim a Binary Search Tree]/src/TreeNode.java @@ -0,0 +1,16 @@ +/** + * Author: 王俊超 + * Time: 2020-06-29 11:06 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} From 0790b0e84934cf6892932bff718d09eed8aec11b Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Mon, 6 Jul 2020 09:10:04 +0800 Subject: [PATCH 16/18] commit Change-Id: I820959b1cfc713576e66ee7653b8ab36ac7418c3 --- [0669][Trim a Binary Search Tree]/src/Solution.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/[0669][Trim a Binary Search Tree]/src/Solution.java b/[0669][Trim a Binary Search Tree]/src/Solution.java index 8bc8fa1..c86b80d 100644 --- a/[0669][Trim a Binary Search Tree]/src/Solution.java +++ b/[0669][Trim a Binary Search Tree]/src/Solution.java @@ -13,11 +13,11 @@ public TreeNode trimBST(TreeNode root, int L, int R) { if (root.val == L) { root.left = null; - root.right = trimBST(root, L, R); + root.right = trimBST(root.right, L, R); return root; } else if (root.val == R) { - root.right = null; root.left = trimBST(root.left, L, R); + root.right = null; return root; } else if (root.val < L) { return trimBST(root.right, L, R); From 7a208fa954fca7fec49334b86cc71683f6edc797 Mon Sep 17 00:00:00 2001 From: Wang-Jun-Chao Date: Tue, 7 Jul 2020 09:23:55 +0800 Subject: [PATCH 17/18] commit Change-Id: I7e09e7afc5094e80010f136cc272e55762f94407 --- .idea/modules.xml | 1 + .idea/workspace.xml | 111 +++++++++--------- ...[Second Minimum Node In a Binary Tree].iml | 21 ++++ .../src/Solution.java | 68 +++++++++++ .../src/SolutionTest.java | 24 ++++ .../src/TreeNode.java | 16 +++ 6 files changed, 186 insertions(+), 55 deletions(-) create mode 100644 [0671][Second Minimum Node In a Binary Tree]/[0671][Second Minimum Node In a Binary Tree].iml create mode 100644 [0671][Second Minimum Node In a Binary Tree]/src/Solution.java create mode 100644 [0671][Second Minimum Node In a Binary Tree]/src/SolutionTest.java create mode 100644 [0671][Second Minimum Node In a Binary Tree]/src/TreeNode.java diff --git a/.idea/modules.xml b/.idea/modules.xml index b70f6ff..6af2ce0 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -280,6 +280,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2a170ed..69b73b9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,10 @@ - - - - - - - - - + + + + @@ -28,8 +23,8 @@ @@ -97,7 +92,7 @@ - + - - + + - - + + - - + + - + + - @@ -242,7 +237,8 @@ - + + 1560552440693 @@ -615,11 +611,11 @@ - + - - + + @@ -631,11 +627,11 @@ - + - - + + @@ -647,11 +643,11 @@ - + - - + + @@ -663,11 +659,11 @@ - + - - + + @@ -679,38 +675,38 @@ - + - - + + - + - - + + - + - - + + - + - - + + @@ -734,18 +730,18 @@ - + - + - + - + @@ -756,19 +752,19 @@ - + - + - + - + @@ -1084,6 +1080,11 @@ 25 1560552440693 @@ -611,11 +616,11 @@ - + - - + + @@ -627,11 +632,11 @@ - + - - + + @@ -643,11 +648,11 @@ - + - - + + @@ -659,11 +664,11 @@ - + - - + + @@ -675,38 +680,38 @@ - + - + - + - + - + - + - + - + @@ -730,19 +735,19 @@ - + - + - + - + @@ -752,11 +757,11 @@ - + - + diff --git a/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/[0674][Longest Continuous Increasing Subsequence].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java new file mode 100644 index 0000000..5a15c9f --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/src/Solution.java @@ -0,0 +1,30 @@ +/** + * Author: 王俊超 + * Time: 2020-07-07 10:03 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + + public int findLengthOfLCIS(int[] nums) { + if (nums == null) { + return 0; + } else if (nums.length < 2) { + return nums.length; + } + + int prev = 0; + int max = 0; + for (int i = 1; i < nums.length; i++) { + while (i < nums.length && nums[i - 1] < nums[i]) { + i++; + } + + max = Math.max(max, i - prev); + prev = i; + } + + return max; + } +} diff --git a/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java new file mode 100644 index 0000000..351e6f5 --- /dev/null +++ b/[0674][Longest Continuous Increasing Subsequence]/src/SolutionTest.java @@ -0,0 +1,27 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-07 10:07 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void findLengthOfLCIS() { + Solution s = new Solution(); + Object[][] data = { + {new int[]{1, 3, 5, 4, 7}, 3}, + {new int[]{2, 2, 2, 2, 2}, 1}, + {new int[]{1, 2, 3, 4, 5}, 5}, + {new int[]{2, 2, 3, 4, 5}, 4}, + }; + + for (Object[] d : data) { + int result = s.findLengthOfLCIS((int[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file diff --git a/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/[0680][Valid Palindrome II]/[0680][Valid Palindrome II].iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/[0680][Valid Palindrome II]/src/Solution.java b/[0680][Valid Palindrome II]/src/Solution.java new file mode 100644 index 0000000..9ae5eb2 --- /dev/null +++ b/[0680][Valid Palindrome II]/src/Solution.java @@ -0,0 +1,35 @@ +/** + * Author: 王俊超 + * Time: 2020-07-07 10:12 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public boolean validPalindrome(String s) { + int start = 0; + int end = s.length() - 1; + while (start < end) { + // 有一个不相等,可以抛弃start或者end字符,抛弃一个字符后,后面的都要是回文 + if (s.charAt(start) != s.charAt(end)) { + return validPalindrome(s, start + 1, end) || validPalindrome(s, start, end - 1); + } + start++; + end--; + } + + return true; + } + + private boolean validPalindrome(String s, int start, int end) { + while (start < end) { + if (s.charAt(start) != s.charAt(end)) { + return false; + } + start++; + end--; + } + + return true; + } +} diff --git a/[0682][Baseball Game]/[0682][Baseball Game].iml b/[0682][Baseball Game]/[0682][Baseball Game].iml new file mode 100644 index 0000000..c70cbf1 --- /dev/null +++ b/[0682][Baseball Game]/[0682][Baseball Game].iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/[0682][Baseball Game]/src/Solution.java b/[0682][Baseball Game]/src/Solution.java new file mode 100644 index 0000000..4af7f26 --- /dev/null +++ b/[0682][Baseball Game]/src/Solution.java @@ -0,0 +1,43 @@ +import java.util.LinkedList; +import java.util.List; + +/** + * Author: 王俊超 + * Time: 2020-07-07 12:46 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class Solution { + public int calPoints(String[] ops) { + + if (ops == null || ops.length < 1) { + return 0; + } + List round = new LinkedList<>(); + + for (String o : ops) { + int size = round.size(); + switch (o) { + case "+": + round.add(round.get(size - 1) + round.get(size - 2)); + break; + case "D": + round.add(2 * round.get(size - 1)); + break; + case "C": + round.remove(size - 1); + break; + default: + round.add(Integer.parseInt(o)); + } + } + + int sum = 0; + for(Integer n : round) { + sum += n; + } + + return sum; + } +} diff --git a/[0682][Baseball Game]/src/SolutionTest.java b/[0682][Baseball Game]/src/SolutionTest.java new file mode 100644 index 0000000..1cf9c9d --- /dev/null +++ b/[0682][Baseball Game]/src/SolutionTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; + +/** + * Author: 王俊超 + * Time: 2020-07-07 12:53 + * CSDN: http://blog.csdn.net/derrantcm + * Github: https://github.com/Wang-Jun-Chao + * Declaration: All Rights Reserved !!! + **/ +public class SolutionTest { + + @org.junit.Test + public void calPoints() { + Solution s = new Solution(); + Object[][] data = { + {new String[]{"5", "2", "C", "D", "+"}, 30}, + {new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"}, 27}, + }; + + for (Object[] d : data) { + int result = s.calPoints((String[]) d[0]); + Assert.assertEquals(d[1], result); + } + } +} \ No newline at end of file