From 12480330e890125b6eadf0ad0d3170b6015d1e41 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 25 Mar 2021 13:17:11 +0800 Subject: [PATCH 01/19] =?UTF-8?q?feature=20=EF=BC=9Anode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/LinkedList/DeleteDuplicates.java | 54 +++++++++++++++++++ .../fancv/leetCode/LinkedList/ListNode.java | 24 +++++++++ .../LinkedList/RemoveNLinkedList.java | 16 ------ 3 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/fancv/leetCode/LinkedList/DeleteDuplicates.java create mode 100644 src/main/java/com/fancv/leetCode/LinkedList/ListNode.java diff --git a/src/main/java/com/fancv/leetCode/LinkedList/DeleteDuplicates.java b/src/main/java/com/fancv/leetCode/LinkedList/DeleteDuplicates.java new file mode 100644 index 0000000..0e2d00e --- /dev/null +++ b/src/main/java/com/fancv/leetCode/LinkedList/DeleteDuplicates.java @@ -0,0 +1,54 @@ +package com.fancv.leetCode.LinkedList; + +/** + * @author hamish-wu + */ +public class DeleteDuplicates { + + public static void main(String[] args) { + + ListNode a = new ListNode(4, null); + ListNode b = new ListNode(3, a); + ListNode c = new ListNode(2, b); + ListNode d = new ListNode(1, c); + ListNode e = new ListNode(1, d); + + ListNode result = deleteDuplicates(e); + + /* ListNode a1 = new ListNode(4, null); + ListNode b1 = new ListNode(3, a1); + ListNode c1 = new ListNode(2, b1);*/ + ListNode d1 = new ListNode(1, null); + ListNode e1 = new ListNode(1, d1); + ListNode resultq = deleteDuplicates(e1); + System.out.println(); + + } + + /** + * 递归思想 + * 1. 1 1 1 2 3 4 + * 2. 1 + * 3. 1 2 3 3 4 4 + * + * @param head + * @return + */ + public static ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode sail = head.next; + if (head.val == sail.val) { + while (sail!= null && head.val == sail.val) { + sail = sail.next; + } + head = deleteDuplicates(sail); + } else { + head.next = deleteDuplicates(sail); + } + + return head; + + } +} diff --git a/src/main/java/com/fancv/leetCode/LinkedList/ListNode.java b/src/main/java/com/fancv/leetCode/LinkedList/ListNode.java new file mode 100644 index 0000000..82cd275 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/LinkedList/ListNode.java @@ -0,0 +1,24 @@ +package com.fancv.leetCode.LinkedList; + +/** + * leetCode node + * + * @author hamish-wu + */ +public class ListNode { + + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } +} diff --git a/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java b/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java index bf1e632..4b514ed 100644 --- a/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java +++ b/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java @@ -117,19 +117,3 @@ static ListNode addTwoNumbers(ListNode l1, ListNode l2) { } -class ListNode { - int val; - ListNode next; - - ListNode() { - } - - ListNode(int val) { - this.val = val; - } - - ListNode(int val, ListNode next) { - this.val = val; - this.next = next; - } -} From 965ab56501b96caf6d27edb79aba3e4b2b76705c Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 25 Mar 2021 13:57:41 +0800 Subject: [PATCH 02/19] =?UTF-8?q?feature=20=EF=BC=9A=E4=B8=89=E4=B8=AA?= =?UTF-8?q?=E8=BF=9E=E7=BB=AD=E7=9A=84=E5=A5=87=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/mathematics/MyMathematics.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java index d815698..6e921c5 100644 --- a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java +++ b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java @@ -6,10 +6,10 @@ public class MyMathematics { public static void main(String[] args) { - System.out.println(Arrays.toString(findErrorNums(new int[]{1, 1}))); - System.out.println(Arrays.toString(findErrorNums(new int[]{1, 2, 2}))); - System.out.println(Arrays.toString(findErrorNums(new int[]{1, 2, 3, 5, 4, 5}))); - System.out.println(Arrays.toString(findErrorNums(new int[]{1, 2, 3, 2, 4, 5, 6, 7, 8, 9}))); + System.out.println(threeConsecutiveOdds(new int[]{1, 1})); + System.out.println(threeConsecutiveOdds(new int[]{1, 2, 2})); + System.out.println(threeConsecutiveOdds(new int[]{1, 3, 5, 4, 5})); + System.out.println(threeConsecutiveOdds(new int[]{0, 2, 3, 2, 4, 5, 7, 9})); } public static int[] findErrorNums(int[] nums) { @@ -27,4 +27,29 @@ public static int[] findErrorNums(int[] nums) { return r; } + /** + * 连续三个奇数 + * 实习生,进一步输出这个三个连续的奇数 + * @param arr + * @return + */ + public static boolean threeConsecutiveOdds(int[] arr) { + + int s = 0; + for (int i = 0; i < arr.length; i++) { + int temp = arr[i]; + if (temp % 2 == 1) { + s++; + if (s > 2) { + return true; + } + } else { + s = 0; + } + + + } + return false; + } + } From 1abdf44638b239de99d29c6e1d7bd706d68a243a Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 25 Mar 2021 14:25:21 +0800 Subject: [PATCH 03/19] =?UTF-8?q?feature=20=EF=BC=9A=E4=B8=91=E6=95=B0?= =?UTF-8?q?=E9=80=92=E5=BD=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/mathematics/MyMathematics.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java index 6e921c5..4504a6c 100644 --- a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java +++ b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java @@ -6,10 +6,10 @@ public class MyMathematics { public static void main(String[] args) { - System.out.println(threeConsecutiveOdds(new int[]{1, 1})); - System.out.println(threeConsecutiveOdds(new int[]{1, 2, 2})); - System.out.println(threeConsecutiveOdds(new int[]{1, 3, 5, 4, 5})); - System.out.println(threeConsecutiveOdds(new int[]{0, 2, 3, 2, 4, 5, 7, 9})); + System.out.println(isUgly(0)); + System.out.println(isUgly(2)); + System.out.println(isUgly(1024)); + System.out.println(isUgly(14)); } public static int[] findErrorNums(int[] nums) { @@ -30,6 +30,7 @@ public static int[] findErrorNums(int[] nums) { /** * 连续三个奇数 * 实习生,进一步输出这个三个连续的奇数 + * * @param arr * @return */ @@ -50,6 +51,52 @@ public static boolean threeConsecutiveOdds(int[] arr) { } return false; + + } + /** + * 丑数,数字 2 3 5 乘积 + * + * @param n + * @return + */ + public static boolean isUgly(int n) { + boolean r = false; + if(n==0){ + return r; + } + //1.偶数 + if (n % 2 == 0) { + n = n / 2; + if (n == 1) { + r = true; + } else { + return isUgly(n); + } + } + //2.奇数 3的倍数 + else if (n % 3 == 0) { + n = n / 3; + if (n == 1) { + r = true; + } else { + return isUgly(n); + } + + } else if (n % 5 == 0) { + n = n / 5; + if (n == 1) { + r = true; + } else { + return isUgly(n); + } + + } + if (n == 1) { + r = true; + } + return r; + + } } From a41ced76af8a0f5e4eb972b0391d927cc22d73ba Mon Sep 17 00:00:00 2001 From: andy Date: Fri, 26 Mar 2021 10:07:02 +0800 Subject: [PATCH 04/19] =?UTF-8?q?feature=20=EF=BC=9A=E6=89=93=E5=8D=A1?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LinkedList/deleteDuplicatesOne.java | 39 +++++++++ .../fancv/leetCode/mathematics/MathArray.java | 27 ++++++ .../leetCode/mathematics/MyMathematics.java | 87 +++++++++++++++++-- 3 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/fancv/leetCode/LinkedList/deleteDuplicatesOne.java create mode 100644 src/main/java/com/fancv/leetCode/mathematics/MathArray.java diff --git a/src/main/java/com/fancv/leetCode/LinkedList/deleteDuplicatesOne.java b/src/main/java/com/fancv/leetCode/LinkedList/deleteDuplicatesOne.java new file mode 100644 index 0000000..18e992c --- /dev/null +++ b/src/main/java/com/fancv/leetCode/LinkedList/deleteDuplicatesOne.java @@ -0,0 +1,39 @@ +package com.fancv.leetCode.LinkedList; + +public class deleteDuplicatesOne { + + public static void main(String[] args) { + ListNode a = new ListNode(4, null); + ListNode b = new ListNode(2, a); + ListNode c = new ListNode(2, b); + ListNode d = new ListNode(2, c); + ListNode e = new ListNode(1, d); + + ListNode result = deleteDuplicates(e); + System.out.println(result.val); + } + + + /** + * 删除链表中重复元素 回调 + * 1 1 2 3 3 + * + * @param head + * @return + */ + public static ListNode deleteDuplicates(ListNode head) { + if (head == null || head.next == null) { + return head; + } + ListNode sail = head.next; + if (head.val == sail.val) { + while (sail != null && head.val == sail.val) { + sail = sail.next; + } + } + head.next = deleteDuplicates(sail); + + return head; + } + +} diff --git a/src/main/java/com/fancv/leetCode/mathematics/MathArray.java b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java new file mode 100644 index 0000000..4c2f7b4 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java @@ -0,0 +1,27 @@ +package com.fancv.leetCode.mathematics; + +import java.util.Arrays; + +/** + * @author hamish-wu + */ +public class MathArray { + + + public static void main(String[] args) { + + } + + public int maximumGap(int[] nums) { + + //1.排序 + Arrays.sort(nums); + + //2.遍历元素,计算相邻元素差值 最大 + + + + return 0; + } + +} diff --git a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java index 4504a6c..9a73478 100644 --- a/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java +++ b/src/main/java/com/fancv/leetCode/mathematics/MyMathematics.java @@ -6,10 +6,11 @@ public class MyMathematics { public static void main(String[] args) { - System.out.println(isUgly(0)); - System.out.println(isUgly(2)); - System.out.println(isUgly(1024)); - System.out.println(isUgly(14)); + System.out.println(nthUglyNumber(400)); + System.out.println(nthUglyNumber(800)); + System.out.println(nthUglyNumber(1200)); + System.out.println(nthUglyNumber(1600)); + System.out.println(nthUglyNumber(200)); } public static int[] findErrorNums(int[] nums) { @@ -63,7 +64,7 @@ public static boolean threeConsecutiveOdds(int[] arr) { */ public static boolean isUgly(int n) { boolean r = false; - if(n==0){ + if (n == 0) { return r; } //1.偶数 @@ -97,6 +98,82 @@ else if (n % 3 == 0) { r = true; } return r; + } + + /** + * 丑数,数字 2 3 5 乘积 + * + * @param n + * @return + */ + public boolean isUgly2(int n) { + boolean r = false; + //1.偶数 + while (n % 2 == 0) { + n = n / 2; + } + //2.奇数 3的倍数 + while (n % 3 == 0) { + n = n / 3; + } + while (n % 5 == 0) { + n = n / 5; + } + if (n == 1) { + r = true; + } + return r; + } + + /** + * 第N 个丑数 笨办法 + * + * @param n + * @return + */ + public static int nthUglyNumber(int n) { + int i = 1; + int temp = 0; + if (n > 200) { + i = 16200; + temp = 199; + } else if (n > 400) { + i = 311040; + temp = 399; + } else if (n > 800) { + i = 12754584; + temp = 799; + } else if (n > 1200) { + i = 174960000; + temp = 799; + } else if (n > 1600) { + i = 1399680000; + temp = 1599; + } + for (; i < Integer.MAX_VALUE; i++) { + if (isUgly(i)) { + temp++; + } + if (temp == n) { + return i; + } + } + return 0; + } + + /** + * 第N 个丑数 三指针算法 + * + * @param n + * @return + */ + public static int nthUglyNumber2(int n) { + + + + + + return 0; } } From 83c607a4ce78aa7eb1cae5758eb2d11a59f48958 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 26 Mar 2021 22:23:32 +0800 Subject: [PATCH 05/19] =?UTF-8?q?fun=20=20=E8=BD=AC=E7=BD=AE=E7=9F=A9?= =?UTF-8?q?=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/matrix/TransposedMartix.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/main/java/com/fancv/leetCode/matrix/TransposedMartix.java diff --git a/src/main/java/com/fancv/leetCode/matrix/TransposedMartix.java b/src/main/java/com/fancv/leetCode/matrix/TransposedMartix.java new file mode 100644 index 0000000..8464bf6 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/matrix/TransposedMartix.java @@ -0,0 +1,28 @@ +package com.fancv.leetCode.matrix; + +public class TransposedMartix { + + public static void main(String[] args) { + int a[][] = {{1, 2, 3, 11}, {4, 5, 6, 12}, {7, 8, 9, 13}}; + transpose(a); + + } + + public static int[][] transpose(int[][] matrix) { + int width = matrix.length; + int temp = -1; + for (int[] s : matrix) { + temp = s.length > temp ? s.length : temp; + } + int b[][] = new int[temp][width]; + + for (int i = 0; i < matrix.length; i++) { + for (int j = 0; j < matrix[i].length; j++) { + + b[j][i] = matrix[i][j]; + } + + } + return b; + } +} From 47e4a6de5090480eb7c4b2dc3fdcd6403f44a45a Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 26 Mar 2021 23:54:35 +0800 Subject: [PATCH 06/19] =?UTF-8?q?fun=20=20=E9=81=BF=E5=85=8D=E6=B4=AA?= =?UTF-8?q?=E6=B0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fancv/leetCode/arrays/MyArrays.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/main/java/com/fancv/leetCode/arrays/MyArrays.java diff --git a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java new file mode 100644 index 0000000..6457454 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java @@ -0,0 +1,40 @@ +package com.fancv.leetCode.arrays; + +import java.util.HashMap; +import java.util.Map; + +public class MyArrays { + public static void main(String[] args) { + int[] rains = {1, 2, 0, 0, 2, 4}; + avoidFlood(rains); + } + + public static int[] avoidFlood(int[] rains) { + int ans[] = new int[rains.length]; + + int zero = 0; + Map r = new HashMap<>(); + for (int i = 0; i < rains.length; i++) { + if (rains[i] > 0) { + if (r.get(rains[i]) == null) { + r.put(rains[i], i); + } else { + int reained = r.get(rains[i]); + for (int j = 0; j < i; j++) { + if (ans[j] == 0) { + ans[j] = reained; + r.put(rains[i], i); + } + } + } + ans[i] = -1; + } else { + zero++; + ans[i] = 0; + } + } + + + return ans; + } +} From d6157312b2ff19e04aa2522821dfdf7f58051195 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 27 Mar 2021 15:59:18 +0800 Subject: [PATCH 07/19] =?UTF-8?q?feature=20=EF=BC=9A=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E6=B4=AA=E6=B0=B4DONE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fancv/leetCode/arrays/MyArrays.java | 37 ++++++----- .../fancv/leetCode/mathematics/MathArray.java | 64 ++++++++++++++++++- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java index 6457454..9ce4771 100644 --- a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java +++ b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java @@ -1,40 +1,47 @@ package com.fancv.leetCode.arrays; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import java.util.TreeSet; public class MyArrays { public static void main(String[] args) { - int[] rains = {1, 2, 0, 0, 2, 4}; - avoidFlood(rains); + int[] rains = {1, 0, 2, 0, 3, 0, 2, 0, 0, 0, 1, 2, 3}; + System.out.println(Arrays.toString(avoidFlood(rains))); } public static int[] avoidFlood(int[] rains) { int ans[] = new int[rains.length]; - - int zero = 0; Map r = new HashMap<>(); + //存储可以 rains[i] == 0 的索引位置(这里使用 TreeSet 是为了方便查找 适合 排空湖泊 的索引天数) + TreeSet set = new TreeSet<>(); + for (int i = 0; i < rains.length; i++) { if (rains[i] > 0) { - if (r.get(rains[i]) == null) { - r.put(rains[i], i); - } else { + if (r.containsKey(rains[i])) { + //查出第几天哪个池塘下过雨 int reained = r.get(rains[i]); - for (int j = 0; j < i; j++) { - if (ans[j] == 0) { - ans[j] = reained; - r.put(rains[i], i); - } + Integer zeroIdx = set.higher(reained); + if (zeroIdx == null) { + int a[] = {}; + return a; } + ans[zeroIdx] = rains[i]; + set.remove(zeroIdx); } + r.put(rains[i], i); ans[i] = -1; } else { - zero++; - ans[i] = 0; + set.add(i); } } - + for (int i = 0; i < ans.length; i++) { + if (ans[i] == 0) { + ans[i] = 1; + } + } return ans; } } diff --git a/src/main/java/com/fancv/leetCode/mathematics/MathArray.java b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java index 4c2f7b4..b363618 100644 --- a/src/main/java/com/fancv/leetCode/mathematics/MathArray.java +++ b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java @@ -10,6 +10,18 @@ public class MathArray { public static void main(String[] args) { + + int a[] = {1, 1, 1, 2, 3, 10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 7, 8, 9, 10, 11, 12, 13, 14}; + int b[] = {2, 1, 2, 2, 3, 1, 0, 0, 9, 5, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}; + int c[] = {1, 3, 2, 2, 10, 4, 2, 3, 5, 9}; + int d[] = {1, 3, 3, 10, 4, 2, 3, 5}; + + + System.out.println(findLengthOfShortestSubarray(a)); + System.out.println(findLengthOfShortestSubarray(b)); + System.out.println(findLengthOfShortestSubarray(c)); + System.out.println(findLengthOfShortestSubarray(d)); + } public int maximumGap(int[] nums) { @@ -20,8 +32,58 @@ public int maximumGap(int[] nums) { //2.遍历元素,计算相邻元素差值 最大 - return 0; } + /** + * [1,12,3,10,4,2,3,5] 非递减 + *

+ * 1.特殊情况 + * 2.循环遍历 i + * + * @param arr + * @return + */ + public static int findLengthOfShortestSubarray(int[] arr) { + int r = 0; + int len = arr.length; + int tail = -1; + //1.判断空 和length 为1 的情况 + if (arr == null || len == 1) { + return r; + } + // 1.循环遍历 + int temp = -1; + for (int i = 0; i < len; i++) { + //2.相等后移 + if (temp < 0) { + temp = arr[i]; + } + if (arr[i] == temp) { + continue; + } + int head = -1; + int sum = 0; + int min = -1; + for (int j = i + 1; j < len; j++) { + if (arr[j] < temp && arr[j] > min) { + if (head < 0) { + head = j; + } + sum++; + } + } + if (head < sum) { + r = r + head; + } else { + r = r + sum; + + } + + + } + + return r; + } + } From 517e430b9fbbf63f10be41b1101051d5cd420b9f Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 28 Mar 2021 15:09:51 +0800 Subject: [PATCH 08/19] =?UTF-8?q?fun=20=201535.=20=E6=89=BE=E5=87=BA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=B8=B8=E6=88=8F=E7=9A=84=E8=B5=A2=E5=AE=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fancv/leetCode/arrays/MyArrays.java | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java index 9ce4771..2347ca1 100644 --- a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java +++ b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java @@ -1,16 +1,23 @@ package com.fancv.leetCode.arrays; -import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.TreeSet; public class MyArrays { public static void main(String[] args) { - int[] rains = {1, 0, 2, 0, 3, 0, 2, 0, 0, 0, 1, 2, 3}; - System.out.println(Arrays.toString(avoidFlood(rains))); + int[] rains = {2, 1, 3, 5, 4, 6, 7}; + int[] b = {3, 2, 1}; + int[] c = { 1, 25, 35, 68, 66, 90}; + System.out.println(getWinner(c, 3000)); } + /** + * 避免洪水 + * + * @param rains + * @return + */ public static int[] avoidFlood(int[] rains) { int ans[] = new int[rains.length]; Map r = new HashMap<>(); @@ -44,4 +51,45 @@ public static int[] avoidFlood(int[] rains) { } return ans; } + + /** + * 获取获胜者 + *

+ * 1.假设 winner 从 index 0 开始 + * 2.比较之后 j>i+1 是否符合条件 + * 4.若比较过程中 j< arr.lenght-1 计算循环值,每次计算 + * 5.如果 循环一轮之后 无论k 多大 均返回 arr[i] 值 + * + * @param arr + * @param k + * @return + */ + public static int getWinner(int[] arr, int k) { + int winner = 0; + int tempk = 0; + for (int i = 0; i < arr.length; i++) { + winner = arr[i]; + for (int j = i + 1; j < 2 * arr.length; j++) { + if (j >= arr.length) { + j = j - arr.length; + } + if (i == j) { + continue; + } + if (arr[j] < winner) { + tempk++; + } else { + winner = arr[j]; + tempk = 1; + } + if (tempk == k || tempk >= arr.length - 1) { + return winner; + } + + } + + } + return winner; + + } } From 84f08c5e442045fa0a10695358c2a7bf5e4f4753 Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 28 Mar 2021 23:03:12 +0800 Subject: [PATCH 09/19] =?UTF-8?q?fun=20=20=E7=BE=8E=E4=B8=BD=E6=95=B0?= =?UTF-8?q?=E6=80=BB=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/fancv/stringLight/beautySum.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/fancv/stringLight/beautySum.java diff --git a/src/main/java/com/fancv/stringLight/beautySum.java b/src/main/java/com/fancv/stringLight/beautySum.java new file mode 100644 index 0000000..431b1b9 --- /dev/null +++ b/src/main/java/com/fancv/stringLight/beautySum.java @@ -0,0 +1,55 @@ +package com.fancv.stringLight; + +public class beautySum { + + public static void main(String[] args) { + String s = "aabcbaa"; + System.out.println(beautySum(s)); + } + + /** + * 1.计算出字符的所有可能子串 + * 2.计算子串的美丽数 + * 3.所有的美丽数求和 + * + * @param s + * @return + */ + public static int beautySum(String s) { + int sum = 0; + int len = s.length(); + + for (int i = 0; i < len - 2; i++) { + for (int j = i+3; j <= len; j++) { + String temp = s.substring(i, j); + sum = sum +beauty(temp); + } + } + return sum; + } + + /** + * 计算美丽数 + * + * @param s + * @return + */ + public static int beauty(String s) { + System.out.println(s); + int[] counts = new int[26]; + char[] marcher = s.toCharArray(); + for (char a : marcher) { + counts[a - 'a']++; + } + int max = Integer.MIN_VALUE; + int min = Integer.MAX_VALUE; + for (int count : counts) { + if (count > 0) { + max = Math.max(max, count); + min = Math.min(min, count); + } + } + return max-min; + + } +} From e1b957c6de965ce31caccb7e6bee28afb4d0c303 Mon Sep 17 00:00:00 2001 From: Andy Date: Sun, 28 Mar 2021 23:04:06 +0800 Subject: [PATCH 10/19] =?UTF-8?q?fun=20=20=E7=BE=8E=E4=B8=BD=E6=95=B0?= =?UTF-8?q?=E6=80=BB=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fancv/stringLight/beautySum.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/fancv/stringLight/beautySum.java b/src/main/java/com/fancv/stringLight/beautySum.java index 431b1b9..0c9b5ae 100644 --- a/src/main/java/com/fancv/stringLight/beautySum.java +++ b/src/main/java/com/fancv/stringLight/beautySum.java @@ -31,6 +31,8 @@ public static int beautySum(String s) { /** * 计算美丽数 * + * 使用 int[26] 表示字符串出现次数 + * * @param s * @return */ From 2494ea267d9aa5d4fba663aaa296e9cc10f135f2 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 29 Mar 2021 15:49:59 +0800 Subject: [PATCH 11/19] =?UTF-8?q?feature=20=EF=BC=9A=E4=BA=8C=E8=BF=9B?= =?UTF-8?q?=E5=88=B6=E7=BF=BB=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fancv/leetCode/mathematics/MathArray.java | 17 +++++++++++-- .../leetCode/mathematics/ReverseBits.java | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/fancv/leetCode/mathematics/ReverseBits.java diff --git a/src/main/java/com/fancv/leetCode/mathematics/MathArray.java b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java index b363618..aace844 100644 --- a/src/main/java/com/fancv/leetCode/mathematics/MathArray.java +++ b/src/main/java/com/fancv/leetCode/mathematics/MathArray.java @@ -14,13 +14,13 @@ public static void main(String[] args) { int a[] = {1, 1, 1, 2, 3, 10, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 7, 8, 9, 10, 11, 12, 13, 14}; int b[] = {2, 1, 2, 2, 3, 1, 0, 0, 9, 5, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 3}; int c[] = {1, 3, 2, 2, 10, 4, 2, 3, 5, 9}; - int d[] = {1, 3, 3, 10, 4, 2, 3, 5}; + int d[] = {1, 2, 3, 1,2,3}; System.out.println(findLengthOfShortestSubarray(a)); System.out.println(findLengthOfShortestSubarray(b)); System.out.println(findLengthOfShortestSubarray(c)); - System.out.println(findLengthOfShortestSubarray(d)); + System.out.println(containsNearbyDuplicate(d, 2)); } @@ -86,4 +86,17 @@ public static int findLengthOfShortestSubarray(int[] arr) { return r; } + public static boolean containsNearbyDuplicate(int[] nums, int k) { + for (int i = 0; i < nums.length; i++) { + int j = i+1; + for (; j - i <= k&&j Date: Mon, 29 Mar 2021 21:57:40 +0800 Subject: [PATCH 12/19] =?UTF-8?q?feature=20=EF=BC=9A=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E5=8F=B3=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/LinkedList/rotateRight.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/com/fancv/leetCode/LinkedList/rotateRight.java diff --git a/src/main/java/com/fancv/leetCode/LinkedList/rotateRight.java b/src/main/java/com/fancv/leetCode/LinkedList/rotateRight.java new file mode 100644 index 0000000..6ff7fe7 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/LinkedList/rotateRight.java @@ -0,0 +1,51 @@ +package com.fancv.leetCode.LinkedList; + +public class rotateRight { + + public static void main(String[] args) { + /* ListNode a = new ListNode(5, null); + ListNode b = new ListNode(4, a);*/ + ListNode c = new ListNode(3, null); + ListNode d = new ListNode(2, c); + ListNode e = new ListNode(1, d); + rotateRight(e, 200000000); + System.out.println(); + } + + /** + * + * + * @param head + * @param k + * @return + */ + public static ListNode rotateRight(ListNode head, int k) { + + + if (head == null || k == 0) { + return head; + } + ListNode sail = head; + int len = 1; + while (sail.next != null) { + len++; + sail = sail.next; + } + + ListNode tail = null; + //循环节点 + tail = sail; + sail.next = head; + sail = head; + //得到循环的次数 + int loop = len - (k % len); + + for (int i = 0; i < loop; i++) { + sail = sail.next; + tail = tail.next; + } + tail.next = null; + + return sail; + } +} From 7045130515df67a67a41b164f2c58381441d878d Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 30 Mar 2021 15:25:05 +0800 Subject: [PATCH 13/19] =?UTF-8?q?feature=20=EF=BC=9A=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E4=B8=A4=E4=B8=AA=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LinkedList/RemoveNLinkedList.java | 75 +++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java b/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java index 4b514ed..fef9232 100644 --- a/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java +++ b/src/main/java/com/fancv/leetCode/LinkedList/RemoveNLinkedList.java @@ -8,16 +8,16 @@ public class RemoveNLinkedList { public static void main(String[] args) { - ListNode a = new ListNode(9, null); - ListNode b = new ListNode(9, a); - ListNode c = new ListNode(9, null); - ListNode d = new ListNode(9, c); - ListNode e = new ListNode(9, d); + ListNode a = new ListNode(12, null); + ListNode b = new ListNode(5, null); + ListNode c = new ListNode(4, null); + ListNode d = new ListNode(3, c); + ListNode e = new ListNode(1, d); /*removeNthFromEnd(e, 5); System.out.println("结束");*/ - addTwoNumbers(e, b); + mergeTwoLists( b,e); } public static ListNode removeNthFromEnd(ListNode head, int n) { @@ -114,6 +114,69 @@ static ListNode addTwoNumbers(ListNode l1, ListNode l2) { } + /** + * 合并两个有序链表 + * + * @param l1 + * @param l2 + * @return + */ + public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { + + //头节点 + ListNode head1 = null; + //新节点尾部节点 + ListNode head2 = null; + //遍历节点 + ListNode sail = null; + /** + * 链表有空的情况 + */ + if (l1 == null && l2 != null) { + return l2; + } + if (l2 == null && l1 != null) { + return l1; + } + if (l1 == null && l2 == null) { + return l1; + } +/** + *选择l1遍历 + */ + + boolean tail = false; + if (l1.val >= l2.val) { + head1 = l2; + head2 = l1; + sail = l1; + tail = true; + } else { + head1 = l1; + head2 = l2; + sail = l2; + } + + while (head1 != null && head2 != null) { + if (head1.next != null && head1.next.val <= sail.val) { + head1 = head1.next; + } else { + sail = head1.next; + head1.next = head2; + head1 = head1.next; + head2 = sail; + } + } + + if (tail) { + return l2; + } else { + return l1; + } + + } + + } From bfa6ea4fb02ff39cce067474862e41df74bc6d03 Mon Sep 17 00:00:00 2001 From: andy Date: Wed, 31 Mar 2021 20:15:51 +0800 Subject: [PATCH 14/19] =?UTF-8?q?feature=20=EF=BC=9A=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AD=90=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fancv/leetCode/arrays/MyArrays.java | 66 +++++++++++++++++-- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java index 2347ca1..b8f9821 100644 --- a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java +++ b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java @@ -1,15 +1,13 @@ package com.fancv.leetCode.arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.TreeSet; +import java.util.*; public class MyArrays { public static void main(String[] args) { - int[] rains = {2, 1, 3, 5, 4, 6, 7}; - int[] b = {3, 2, 1}; - int[] c = { 1, 25, 35, 68, 66, 90}; - System.out.println(getWinner(c, 3000)); + int[] rains = {1, 1, 1, 2, 2, 3, 1, 4, 6, 7}; + int[] b = {-1, 1, 2, 2}; + int[] c = {1, 2, 1, 1, 2, 2, 3, 3, 3}; + System.out.println(subsetsWithDup(b)); } /** @@ -92,4 +90,58 @@ public static int getWinner(int[] arr, int k) { return winner; } + + /** + * 1.计算数组所有不重复的子集 + * 输入:nums = [1,1,1,1,2] + * 输出:[[],[1],[1,1],[1,1,1],[1,1,1,1],[1,1,1,1,2]] + * 输入:nums = [1,1,2,2] + * 输出:[[],[1],[1,1],[1,1,2],[1,1,2,2],[1,2][1,2,2][2,2][2]] + *

+ * 反思, + * 1.计算子集的算法不对,遗漏很多子集 + * 2.去重算法不对 + *

+ * nums=[-1,1,1,2] + * 子集 [-1] [-1,1],[-1,1,1][-1,1,1,2][-1,1][-1,1,2][1][1,1][1,1,2][1,2][2] + * + * @param nums + * @return + */ + public static List> subsetsWithDup(int[] nums) { + Arrays.sort(nums); + List> result = new ArrayList<>(); + int len = nums.length; + List list = new ArrayList<>(); + /** + * 1.外层循环 2的len 次方 + * C 0/4 + C 1/4 +C2/4+C3/4 +C4/4 + * 1 + 4+ 6+ 4+1=16 + * + */ + for (int i = 0; i < (1 << len); ++i) { + list.clear(); + /** + * 2.内层循环j 0 && (1 << (j - 1) & i) == 0 && nums[j] == nums[j - 1]) { + tag = false; + break; + } + list.add(nums[j]); + } + } + if (tag) { + result.add(new ArrayList<>(list)); + } + + } + return result; + + } + } From 35f992c359fe75cbd1f97f1f34d7dfbfb5dad88c Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 1 Apr 2021 13:45:38 +0800 Subject: [PATCH 15/19] =?UTF-8?q?feature=20=EF=BC=9A=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AD=90=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fancv/leetCode/arrays/MyArrays.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java index b8f9821..9b4fdbb 100644 --- a/src/main/java/com/fancv/leetCode/arrays/MyArrays.java +++ b/src/main/java/com/fancv/leetCode/arrays/MyArrays.java @@ -145,3 +145,24 @@ public static List> subsetsWithDup(int[] nums) { } } + +class Solution { + List> result = new ArrayList<>(); + public List> subsets(int[] nums) { + List temp = new ArrayList<>(); + int len = nums.length; + // 1 外部循环2的len 次方循环 + for (int loop = 0; loop < (1 << len); loop++) { + temp.clear(); + // 内部循环 len + for (int j = 0; j < len; j++) { + //3 位运算判断是否子集 + if ((loop & (1 << j)) != 0) { + temp.add(nums[j]); + } + } + result.add(new ArrayList<>(temp)); + } + return result; + } +} From af7d449ecee2daec021b436b32394c7904820f6e Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 1 Apr 2021 14:32:45 +0800 Subject: [PATCH 16/19] =?UTF-8?q?feature=20=EF=BC=9A1006.=20=E7=AC=A8?= =?UTF-8?q?=E9=98=B6=E4=B9=98=204=E4=B8=AA=E4=B8=80=E7=BB=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetCode/mathematics/FactorialClumsy.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/main/java/com/fancv/leetCode/mathematics/FactorialClumsy.java diff --git a/src/main/java/com/fancv/leetCode/mathematics/FactorialClumsy.java b/src/main/java/com/fancv/leetCode/mathematics/FactorialClumsy.java new file mode 100644 index 0000000..c37b8a3 --- /dev/null +++ b/src/main/java/com/fancv/leetCode/mathematics/FactorialClumsy.java @@ -0,0 +1,73 @@ +package com.fancv.leetCode.mathematics; + +/** + * @author hamish-wu + */ +public class FactorialClumsy { + + + public static void main(String[] args) { + + System.out.println(clumsy(4)); + } + + /** + * 通常,正整数 n 的阶乘是所有小于或等于 n 的正整数的乘积。例如,factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1。 + *

+ * 相反,我们设计了一个笨阶乘 clumsy:在整数的递减序列中,我们以一个固定顺序的操作符序列来依次替换原有的乘法操作符:乘法(*),除法(/),加法(+)和减法(-)。 + *

+ * 例如,clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1。然而,这些运算仍然使用通常的算术运算顺序:我们在任何加、减步骤之前执行所有的乘法和除法步骤,并且按从左到右处理乘法和除法步骤。 + *

+ * 另外,我们使用的除法是地板除法(floor division),所以 10 * 9 / 8 等于 11。这保证结果是一个整数。 + *

+ * 实现上面定义的笨函数:给定一个整数 N,它返回 N 的笨阶乘。 + *

+ *   + * 来源:力扣(LeetCode) + * 链接:https://leetcode-cn.com/problems/clumsy-factorial + * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 + * + * @param N + * @return + */ + public static int clumsy(int N) { + int result = 0; + boolean b = false; + for (int m = N; m > 0; m = m - 4) { + result = result + threeNum(m, b); + b = true; + } + + + return result; + } + + /** + * 四个数字一组计算 + * + * @param a + * @param b + * @return + */ + public static int threeNum(int a, boolean b) { + int temp = 0; + if (a > 3) { + temp = a * (a - 1) / (a - 2); + if (b) { + return (a - 3) - temp; + } else { + return temp + a - 3; + } + } else { + if (a == 1 || a == 2) { + temp = a; + } else { + temp = 6; + } + } + if (b) { + temp = -temp; + } + return temp; + } +} From c3fee911a60b94d0a827689855e260f53544163e Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 1 Apr 2021 14:34:19 +0800 Subject: [PATCH 17/19] Update readme.md --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 38340c9..95bb102 100644 --- a/readme.md +++ b/readme.md @@ -2,3 +2,4 @@ ### 20. 有效的括号 ### 2. 两数相加 +### 1006. 笨阶乘 From 273bfec899e7f4805314be4134f8d85eaf746749 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 10 Aug 2021 11:47:21 +0800 Subject: [PATCH 18/19] =?UTF-8?q?feature=20=EF=BC=9AJava=20Tree=20=20trave?= =?UTF-8?q?rsal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/fancv/tree/MyTree.java | 115 +++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/main/java/com/fancv/tree/MyTree.java diff --git a/src/main/java/com/fancv/tree/MyTree.java b/src/main/java/com/fancv/tree/MyTree.java new file mode 100644 index 0000000..e2ee231 --- /dev/null +++ b/src/main/java/com/fancv/tree/MyTree.java @@ -0,0 +1,115 @@ +package com.fancv.tree; + +import java.util.LinkedList; + +/** + * 基本的树 + */ +public class MyTree { + + + /** + * 最普通的树的构造器 + * + * 分析若list 中没有null + * 则会退化成一个链表,总是填充左侧节点 + * 所以需要平衡二叉树 + * + * + * @param list + * @return + */ + public static TreeNode createBinaryTree(LinkedList list) { + TreeNode node = null; + if (list == null || list.isEmpty()) { + return null; + } + Integer data = list.removeFirst(); + if (data != null) { + node = new TreeNode(data); + node.leftChild = createBinaryTree(list); + node.rightChild = createBinaryTree(list); + } + return node; + } + + /** + * 前序遍历 + * @param node + */ + public static void preOrderTraveral(TreeNode node) { + if (node == null) { + return; + } + System.out.print(node.data + " "); + preOrderTraveral(node.leftChild); + preOrderTraveral(node.rightChild); + } + + /** + * 中序遍历 + * @param node + */ + public static void inOrderTraveral(TreeNode node) { + if (node == null) { + return; + } + inOrderTraveral(node.leftChild); + System.out.print(node.data + " "); + inOrderTraveral(node.rightChild); + } + + /** + * 后续遍历 + * @param node + */ + public static void postOrderTraveral(TreeNode node) { + if (node == null) { + return; + } + postOrderTraveral(node.leftChild); + postOrderTraveral(node.rightChild); + System.out.print(node.data + " "); + } + + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + list.add(8); + list.add(9); + list.add(10); + list.add(11); + list.add(12); + + + TreeNode treeNode = createBinaryTree(list); + + + System.out.println("前序遍历:"); + preOrderTraveral(treeNode); + System.out.println("\n"+"中序遍历:"); + inOrderTraveral(treeNode); + System.out.println("\n"+"后序遍历:"); + postOrderTraveral(treeNode); + + } + + +} + + +class TreeNode { + public int data; + public TreeNode leftChild; + public TreeNode rightChild; + + public TreeNode(int data) { + this.data = data; + } +} \ No newline at end of file From 336f740cc60175f3174ca52161573fd26a542e53 Mon Sep 17 00:00:00 2001 From: Andy Date: Sat, 27 Aug 2022 20:39:41 +0800 Subject: [PATCH 19/19] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 95bb102..2040419 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# 算法demo +# 算法练习 ### 20. 有效的括号 ### 2. 两数相加