diff --git a/src/leetcode/lc315_countSmaller.java b/src/leetcode/lc315_countSmaller.java index 31f32b0..ce56d40 100644 --- a/src/leetcode/lc315_countSmaller.java +++ b/src/leetcode/lc315_countSmaller.java @@ -38,7 +38,7 @@ import java.util.List; * @DATE 2020/12/4 6:11 下午 * @Description */ -class Solution { +class lc315_countSmaller { public static List countSmaller(int[] nums) { if (nums.length < 1 || nums == null) { return null; @@ -89,7 +89,7 @@ class Main_315{ public static void main(String[] args){ int[] nums = new int[]{3,7,5,2,6,1}; - List list = Solution.countSmaller(nums); + List list = lc315_countSmaller.countSmaller(nums); System.out.println(list); } diff --git a/src/leetcode/lc92_reverseBetween.java b/src/leetcode/lc92_reverseBetween.java new file mode 100644 index 0000000..cbb289b --- /dev/null +++ b/src/leetcode/lc92_reverseBetween.java @@ -0,0 +1,101 @@ +package leetcode; + + +//反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 +// +// 说明: +//1 ≤ m ≤ n ≤ 链表长度。 +// +// 示例: +// +// 输入: 1->2->3->4->5->NULL, m = 2, n = 4 +//输出: 1->4->3->2->5->NULL +// Related Topics 链表 +// 👍 594 👎 0 + +/** + * @author Leo + * @ClassName lc92_reverseBetween + * @DATE 2020/12/7 2:29 下午 + * @Description + */ +//leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class lc92_reverseBetween { + public ListNode reverseBetween1(ListNode head, int m, int n) { + if (head==null||m == n){ + return head; + } + ListNode cur = head; + ListNode pre = null; + while (cur != null && m > 1) { + pre = cur; + cur = cur.next; + m--; + n--; + } + if (cur==null||cur.next == null){ + return head; + } + ListNode tail = cur; + ListNode temp = pre; + ListNode next = null; + while (cur != null && n > 0) { + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + n--; + } + if (temp != null) { + temp.next = pre; + }else{ + head = pre; + } + tail.next = cur; + return head; + } + + + public ListNode reverseBetween(ListNode head, int m, int n) { + if (head == null || m == n) { + return head; + } + ListNode pre = null; + ListNode cur = head; + while (cur != null && m > 1) { + pre = cur; + cur = cur.next; + m--; + n--; + } + ListNode preTemp = pre, next = null, tail = cur; + while (cur != null && n > 0) { + next = cur.next; + cur.next = preTemp; + preTemp = cur; + cur = next; + } + if (pre != null) { + pre.next = cur; + }else{ + + } + tail.next = next; + + return head; + } + +} +class ListNode { + int val; + ListNode next; + ListNode(int x) { val = x; } +} diff --git a/src/leo/class03_05/CountOfRangeSum.java b/src/leo/class03_05/CountOfRangeSum.java index e3a1429..1369ad6 100644 --- a/src/leo/class03_05/CountOfRangeSum.java +++ b/src/leo/class03_05/CountOfRangeSum.java @@ -362,10 +362,10 @@ class CountOfRangeSum5 { while (index <= r) { long max = sum[index] - lower; long min = sum[index] - upper; - while (windowR <= r && sum[windowR] <= max) { + while (windowR <= m && sum[windowR] <= max) { windowR++; } - while (windowL < r && sum[windowL] < min) { + while (windowL <=m && sum[windowL] < min) { windowL++; } index++; @@ -393,6 +393,69 @@ class CountOfRangeSum5 { } + +class CountOfRangeSum6{ + public static int countRangeSum(int[] arr, int lower, int upper) { + if (arr.length == 0 || arr == null) { + return 0; + } + long[] sum = new long[arr.length]; + sum[0] = arr[0]; + for (int i = 1; i < arr.length; i++) { + sum[i] = sum[i - 1] + arr[i]; + } + return process(sum, 0, arr.length - 1, lower, upper); + } + + public static int process(long[] arr, int l, int r, int lower, int upper) { + if (l > r) { + return 0; + } + if (l == r) { + return arr[l] >= lower && arr[l] <= upper ? 1 : 0; + } + int m = l + ((r - l) >> 1); + return process(arr, l, m, lower, upper) + process(arr, m + 1, r, lower, upper) + merge(arr, l, m, r, lower, upper); + } + + public static int merge(long[] arr,int l,int m,int r,int lower,int upper){ + int windowL = l; + int windowR = l; + int index = m + 1; + int res = 0; + while (index <= r) { + long max = arr[index] - lower; + long min = arr[index] - upper; + while (windowR<=m&&arr[windowR] <= max) { + windowR++; + } + while (windowL<=m&&arr[windowL] < min) { + windowL++; + } + index++; + res += windowR - windowL; + } + long[] help = new long[r - l + 1]; + int i = 0; + int p1 = l; + int p2 = m + 1; + while (p1 <= m && p2 <= r) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + while(p1<=m){ + help[i++] = arr[p1++]; + } + while(p2<=r){ + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + return res; + + } +} + class MainTest{ public static int countRangeSum(int[] nums, int lower, int upper) { @@ -422,7 +485,7 @@ class MainTest{ do { upper = (int) ((range * Math.random() + 1) - (range * Math.random() + 1)); } while (upper <= lower); - int sumCount = CountOfRangeSum4.countRangeSum(arr, lower, upper); + int sumCount = CountOfRangeSum5.countRangeSum(arr, lower, upper); int testSumCount = countRangeSum(copyArray, lower, upper); if (sumCount != testSumCount) { System.out.println("sumCount :" + sumCount+" testSumCount : "+testSumCount); diff --git a/src/leo/class03_05/QuickSort1.java b/src/leo/class03_05/QuickSort1.java index 4a2adc2..465ea85 100644 --- a/src/leo/class03_05/QuickSort1.java +++ b/src/leo/class03_05/QuickSort1.java @@ -1,6 +1,7 @@ package leo.class03_05; import leo.util.ArrayUtil; +import sun.jvm.hotspot.debugger.Page; import java.util.Arrays; import java.util.Stack; @@ -375,7 +376,7 @@ class QuickSort3_4{ } int[] equalArea = partition(arr, l, r); process(arr, l, equalArea[0] - 1); - process(arr, equalArea[0] + 1, r); + process(arr, equalArea[1] + 1, r); } private static int[] partition(int[] arr, int l, int r) { @@ -409,6 +410,56 @@ class QuickSort3_4{ } +class QuickSort3_5 { + + public static void quickSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + process(arr, 0, arr.length - 1); + } + + public static void process(int[] arr, int l, int r) { + + if (l >= r) { + return; + } + int[] equalsArea = partition(arr, l, r); + process(arr, l, equalsArea[0] - 1); + process(arr, equalsArea[1] + 1, r); + + } + + + private static int[] partition(int[] arr, int l, int r) { + int leftIndex = l - 1; + int rightIndex = r; + int i = l; + while (i < rightIndex) { + if (arr[i] == arr[r]) { + i++; + } else if (arr[i] > arr[r]) { + swap(arr, i, --rightIndex); + + } else if (arr[i] < arr[r]) { + swap(arr, i++, ++leftIndex); + } + } + swap(arr, r, rightIndex); + return new int[]{leftIndex + 1, rightIndex}; + } + + + public static void swap(int[] arr, int i, int j) { + if (i == j || arr[i] == arr[j]) { + return; + } + arr[i]= arr[i]^arr[j]; + arr[j]= arr[i]^arr[j]; + arr[i]= arr[i]^arr[j]; + } +} + class QuickSortUnRecursive{ private static class Op { diff --git a/src/leo/class04_06/HeapSort.java b/src/leo/class04_06/HeapSort.java index 9cfda81..430ea2c 100644 --- a/src/leo/class04_06/HeapSort.java +++ b/src/leo/class04_06/HeapSort.java @@ -1,6 +1,7 @@ package leo.class04_06; import leo.util.ArrayUtil; +import sun.jvm.hotspot.debugger.Page; import java.util.Arrays; @@ -66,6 +67,46 @@ public class HeapSort { } } +class HeapSort1{ + + public static void heapSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + for (int i = arr.length - 1; i >= 0; i--) { + heapify(arr, i, arr.length); + } + int length = arr.length; + while (length > 0) { + swap(arr, 0, --length); + heapify(arr, 0, length); + } + heapify(arr, 0, length); + } + + public static void heapify(int[] arr, int i, int heapSize) { + int left = i << 1 | 1; + while (left < heapSize) { + int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left; + largest = arr[largest] > arr[i] ? largest : i; + if (largest == i) { + return; + } + swap(arr, largest, i); + i = largest; + left = i << 1 | 1; + } + } + + public static void swap(int[] arr, int i, int j) { + if (i == j || arr[i] == arr[j]) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } +} @@ -83,7 +124,7 @@ class MainHeapSort { int[] arr = ArrayUtil.randomArray(sizeMax, range); int[] copyArray = ArrayUtil.copyArray(arr); Arrays.sort(copyArray); - HeapSort.heapSort(arr); + HeapSort1.heapSort(arr); if (!ArrayUtil.isEqual(arr, copyArray)) { ArrayUtil.printArr(arr); ArrayUtil.printArr(copyArray); diff --git a/src/leo/class04_06/MyMaxHeap.java b/src/leo/class04_06/MyMaxHeap.java index fcc9390..d837cc1 100644 --- a/src/leo/class04_06/MyMaxHeap.java +++ b/src/leo/class04_06/MyMaxHeap.java @@ -1,6 +1,8 @@ package leo.class04_06; import java.util.PriorityQueue; +import java.util.concurrent.ForkJoinPool; +import java.util.function.IntPredicate; /** * @author Leo @@ -298,6 +300,136 @@ class MyMaxHeap3 { } +class MyMaxHeap4{ + + int size; + int[] arr; + final int limit; + + public MyMaxHeap4(int limit) { + this.limit = limit; + this.size = 0; + arr = new int[limit]; + } + + public boolean isEmpty() { + return size == 0; + } + + public void push(int v) { + if (size==limit){ + throw new RuntimeException("heap is full"); + } + arr[size] = v; + heapInsert(arr, size++); + } + + public int pop() { + if (size == 0) { + throw new RuntimeException("heap is empty"); + } + int value = arr[0]; + swap(arr, 0, --size); + heapify(arr, 0, size); + return value; + } + + private void heapInsert(int[] arr, int i) { + while (arr[i] > arr[(i - 1) / 2]) { + swap(arr, i, (i - 1) / 2); + i = (i - 1) / 2; + } + + } + + private void heapify(int[] arr, int index, int heapSize) { + int left = index << 1 | 1; + while (left < heapSize) { + int largest = left+1arr[left]?left+1:left; + largest = arr[largest] > arr[index] ? largest : index; + if (largest == index) { + return; + } + swap(arr, largest, index); + index = largest; + left = index << 1 | 1; + } + } + public static void swap(int[] arr, int i, int j) { + if (i == j || arr[i] == arr[j]) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } + +} + + +class MyMaxHeap5{ + private int size; + private int[] arr; + private final int limit; + + public MyMaxHeap5(int limit) { + this.limit = limit; + this.arr = new int[limit]; + this.size = 0; + } + + public boolean isEmpty() { + return size == 0; + } + + public void push(int value) { + if (this.size == this.limit) { + throw new RuntimeException("heap is full"); + } + arr[size] = value; + heapInsert(arr, size++); + } + + public int pop() { + if (size == 0) { + throw new RuntimeException("heap is empty"); + } + int value = arr[0]; + swap(arr, 0, --size); + heapify(arr, 0, size); + return value; + } + + public void heapify(int[] arr, int i, int heapSize) { + int left = i << 1 | 1; + while (left < heapSize) { + int largest = left + 1 < heapSize && arr[left + 1] > arr[left] ? left + 1 : left; + largest = arr[largest] > arr[i] ? largest : i; + if (i == largest) { + return; + } + swap(arr, largest, i); + i = largest; + left = i << 1 | 1; + } + } + public void heapInsert(int[] arr, int index) { + while (arr[index] > arr[(index - 1) / 2]) { + swap(arr, index, (index - 1) / 2); + index = (index - 1) / 2; + } + } + + public void swap(int[] arr, int i, int j) { + if (i == j || arr[i] == arr[j]) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } +} + class Main{ @@ -307,7 +439,7 @@ class Main{ int range = 50; System.out.println("start!"); PriorityQueue queue = new PriorityQueue<>((a,b)-> {return b-a;}); - MyMaxHeap3 heap = new MyMaxHeap3(testTimes); + MyMaxHeap5 heap = new MyMaxHeap5(testTimes); for (int i = 0; i < testTimes; i++) { if (heap.isEmpty()) { int num = (int) ((range * Math.random() + 1) - (range * Math.random() + 1)); diff --git a/src/leo/class04_06/SortArrayDistanceLessK.java b/src/leo/class04_06/SortArrayDistanceLessK.java index 3e18f5c..50cd2ac 100644 --- a/src/leo/class04_06/SortArrayDistanceLessK.java +++ b/src/leo/class04_06/SortArrayDistanceLessK.java @@ -84,7 +84,32 @@ class SortArrayDistanceLessK2{ } +class SortArrayDistanceLessK3 { + public static void sortArrayDistanceLessK(int[] arr, int k) { + if (k == 0) { + return; + } + if (arr.length < 2 || arr == null) { + return; + } + PriorityQueue queue = new PriorityQueue<>(); + int i; + for (i = 0; i < Math.min(arr.length - 1, k - 1); i++) { + queue.add(arr[i]); + } + int j = 0; + for (; i < arr.length; i++, j++) { + queue.add(arr[i]); + arr[j] = queue.poll(); + } + while (!queue.isEmpty()) { + arr[j++] = queue.poll(); + } + + } + +} class MainK { @@ -101,7 +126,7 @@ class MainK { UpsetArray(arr, k); int[] copyArray = ArrayUtil.copyArray(arr); Arrays.sort(copyArray); - SortArrayDistanceLessK2.sortArrayDistanceLessK(arr,k); + SortArrayDistanceLessK3.sortArrayDistanceLessK(arr,k); if (!ArrayUtil.isEqual(arr, copyArray)) { ArrayUtil.printArr(arr); ArrayUtil.printArr(copyArray); diff --git a/src/leo/class06_09/CopyListWithRandom.java b/src/leo/class06_09/CopyListWithRandom.java index 855a78c..d23261f 100644 --- a/src/leo/class06_09/CopyListWithRandom.java +++ b/src/leo/class06_09/CopyListWithRandom.java @@ -79,6 +79,38 @@ public class CopyListWithRandom { return copyHead; } + public static Node copyRandomNode1(Node head) { + if (head == null) { + return null; + } + Node cur = head; + Node next; + while (cur != null) { + next = cur.next; + cur.next = new Node(cur.value); + cur.next.next = next; + cur = next; + } + Node copy; + cur = head; + while (cur != null) { + next = cur.next.next; + copy = cur.next; + copy.rand = cur.rand == null ? null : cur.rand.next; + cur = next; + } + cur = head; + Node copyHead = head.next; + while(cur!=null){ + next = cur.next.next; + copy = cur.next; + cur.next = next; + copy.next = next == null ? null : next.next; + cur = next; + } + return copyHead; + + } public static void main(String[] args){ @@ -90,7 +122,7 @@ public class CopyListWithRandom { for (int i = 0; i < testTime; i++) { Node node = randomNode(maxSize, range); Node headMap = copyRandomNodeByMap(node); - Node head = copyRandomNode(node); + Node head = copyRandomNode1(node); Node curNode = node; Node curHeadMap = headMap; Node curHead = head; diff --git a/src/leo/class06_09/FindFirstIntersectNode.java b/src/leo/class06_09/FindFirstIntersectNode.java index 1480716..f87f7fc 100644 --- a/src/leo/class06_09/FindFirstIntersectNode.java +++ b/src/leo/class06_09/FindFirstIntersectNode.java @@ -131,7 +131,119 @@ public class FindFirstIntersectNode { } +} + +class FindFirstIntersectNode1{ + public static Node getIntersectNode(Node head1, Node head2) { + if (head1 == null || head2 == null) { + return null; + } + Node loop1 = getLoop(head1); + Node loop2 = getLoop(head2); + if (loop1 != null && loop2 != null) { + return bothLoop(head1, loop1, head2, loop2); + } else if (loop1 == null && loop2 == null) { + return noLoop(head1, head2); + } + return null; + } + public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) { + Node cur1 = null; + Node cur2 = null; + if (loop1 == loop2) { + int n = 0; + cur1 = head1; + cur2 = head2; + while (cur1 != loop1) { + n++; + cur1 = cur1.next; + } + while (cur2 != loop2) { + n--; + cur2 = cur2.next; + } + cur1 = n > 0 ? head1 : head2; + cur2 = cur1 == head1 ? head2 : head1; + n = Math.abs(n); + while (n != 0) { + cur1 = cur1.next; + n--; + } + while (cur1 != cur2) { + cur1 = cur1.next; + cur2 = cur2.next; + } + + return cur1; + }else{ + cur1 = loop1.next; + while (cur1 != loop1) { + if (cur1 == loop2) { + return loop1; + } + cur1 = cur1.next; + } + return null; + } + } + + + public static Node noLoop(Node head1, Node head2) { + if (head1 == null || head2 == null) { + return null; + } + int n = 0; + Node cur1 = head1; + while (cur1 != null) { + n++; + cur1 = cur1.next; + } + Node cur2 = head2; + while (cur2 != null) { + n--; + cur2 = cur2.next; + } + if (cur1 != cur2) { + return null; + } + cur1 = n > 0 ? head1 : head2; + cur2 = cur1 == head1 ? head2 : head1; + n = Math.abs(n); + while (n != 0) { + n--; + cur1 = cur1.next; + } + while (cur1 != cur2) { + cur1 = cur1.next; + cur2 = cur2.next; + } + return cur1; + } + + public static Node getLoop(Node head) { + if (head == null || head.next == null || head.next.next == null) { + return null; + } + Node slow = head.next; + Node fast = head.next.next; + while (slow != fast) { + if (fast.next == null || fast.next.next == null) { + return null; + } + slow = slow.next; + fast = fast.next.next; + } + fast = head; + while (fast != slow) { + slow = slow.next; + fast = fast.next; + } + return slow; + } +} + +class FindFirstIntersectNode_Main{ public static void main(String[] args) { // 1->2->3->4->5->6->7->null Node head1 = new Node(1); @@ -147,7 +259,7 @@ public class FindFirstIntersectNode { head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next.next.next.next.next; // 8->6 - System.out.println(getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); // 1->2->3->4->5->6->7->4... head1 = new Node(1); @@ -164,18 +276,14 @@ public class FindFirstIntersectNode { head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next; // 8->2 - System.out.println(getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); // 0->9->8->6->4->5->6.. head2 = new Node(0); head2.next = new Node(9); head2.next.next = new Node(8); head2.next.next.next = head1.next.next.next.next.next; // 8->6 - System.out.println(getIntersectNode(head1, head2).value); + System.out.println(FindFirstIntersectNode1.getIntersectNode(head1, head2).value); } - - - - } diff --git a/src/leo/class06_09/IsPalindromeList.java b/src/leo/class06_09/IsPalindromeList.java index 90edbc4..68aea8a 100644 --- a/src/leo/class06_09/IsPalindromeList.java +++ b/src/leo/class06_09/IsPalindromeList.java @@ -364,6 +364,48 @@ public class IsPalindromeList { return verify; } + public static boolean isPalindromeList7(Node head) { + if (head == null || head.next == null) { + return true; + } + Node n1 = head; + Node n2 = head; + while (n2.next != null && n2.next.next != null) { + n1 = n1.next; + n2 = n2.next.next; + } + //n1 is mid + n2 = n1.next; + n1.next = null; + Node n3; + while (n2 != null) { + n3 = n2.next; + n2.next = n1; + n1 = n2; + n2 = n3; + } + n2 = head; + n3 = n1; + boolean verify = true; + while (n1 != null && n2 != null) { + if (n1.value != n2.value) { + verify = false; + break; + } + n1 = n1.next; + n2 = n2.next; + } + n2 = n3.next; + n3.next = null; + while (n2 != null) { + n1 = n2.next; + n2.next = n3; + n3 = n2; + n2 = n1; + } + return verify; + } + } @@ -382,13 +424,12 @@ class IsPalindromeListMain { Node originNode = generateRandomNode(maxSize, range); Node head = copyNode(originNode); b = IsPalindromeList.isPalindromeListByStack(head); - a = IsPalindromeList.isPalindromeList6(head); + a = IsPalindromeList.isPalindromeList7(head); if (!isEqualsNode(head, originNode)) { System.out.println("not equals node!"); } if (a != b) { - System.out.println(a); - System.out.println(b); + System.out.println(a+" "+ b); System.out.println("fuck!"); } } diff --git a/src/leo/class06_09/SmallerEqualBigger.java b/src/leo/class06_09/SmallerEqualBigger.java index c3a2054..d2af1a7 100644 --- a/src/leo/class06_09/SmallerEqualBigger.java +++ b/src/leo/class06_09/SmallerEqualBigger.java @@ -109,6 +109,55 @@ public class SmallerEqualBigger { return sH; } } + + + public static Node listPartition1(Node head, int target) { + if (head == null || head.next == null) { + return head; + } + Node sH = null; + Node sT = null; + Node bH = null; + Node bT = null; + Node cur = head; + while (cur != null) { + Node next = cur.next; + cur.next = null; + if (cur.value < target) { + if (sH == null) { + sH = cur; + sT = cur; + }else{ + sT.next = cur; + sT = cur; + } + } else if (cur.value == target) { + if (bH == null) { + bH = cur; + bT = cur; + }else{ + cur.next = bH; + bH = cur; + } + } else if (cur.value > target) { + if (bT == null) { + bH = cur; + bT = cur; + }else { + bT.next = cur; + bT = cur; + } + } + cur = next; + } + + if (sT == null) { + return bH; + }else { + sT.next = bH; + return sH; + } + } } class SmallerEqualBigger_Main{ @@ -126,12 +175,14 @@ class SmallerEqualBigger_Main{ int maxSize = 5; int range = 100; Node originNode = generateRandomNode(maxSize, range); + Node copyNode = copyNode(originNode); printLinkedList(originNode); - // head1 = listPartition1(head1, 4); int i = randomInt(range); System.out.println(i); - originNode = SmallerEqualBigger.listPartition(originNode, i); + copyNode = SmallerEqualBigger.NodePartitionByArray.nodePartitionByArray(copyNode, i); + originNode = SmallerEqualBigger.listPartition1(originNode, i); printLinkedList(originNode); + printLinkedList(copyNode); }