diff --git a/src/leetcode/lc315_countSmaller.java b/src/leetcode/lc315_countSmaller.java new file mode 100644 index 0000000..31f32b0 --- /dev/null +++ b/src/leetcode/lc315_countSmaller.java @@ -0,0 +1,97 @@ +package leetcode; +//给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 num +//s[i] 的元素的数量。 +// +// +// +// 示例: +// +// 输入:nums = [5,2,6,1] +//输出:[2,1,1,0] +//解释: +//5 的右侧有 2 个更小的元素 (2 和 1) +//2 的右侧仅有 1 个更小的元素 (1) +//6 的右侧有 1 个更小的元素 (1) +//1 的右侧有 0 个更小的元素 +// +// +// +// +// 提示: +// +// +// 0 <= nums.length <= 10^5 +// -10^4 <= nums[i] <= 10^4 +// +// Related Topics 排序 树状数组 线段树 二分查找 分治算法 +// 👍 499 👎 0 + + +import javax.print.attribute.standard.NumberUp; +import java.util.ArrayList; +import java.util.List; + +//leetcode submit region begin(Prohibit modification and deletion) +/** + * @author Leo + * @ClassName lc315_countSmaller + * @DATE 2020/12/4 6:11 下午 + * @Description + */ +class Solution { + public static List countSmaller(int[] nums) { + if (nums.length < 1 || nums == null) { + return null; + } + int[] res = new int[nums.length]; + process(nums, 0, nums.length - 1,res); + List list = new ArrayList(res.length); + for (int i = 0; i < res.length; i++) { + list.add(res[i]); + } + return list; + } + + public static void process(int[] arr, int l, int r, int[] res) { + if (l >= r) { + return; + } + int m = l + ((r - l) >> 1); + process(arr, l, m,res); + process(arr, m + 1, r,res); + merge(arr, l, m, r, res); + } + + private static void merge(int[] arr, int l, int m, int r, int[] res) { + int[] help = new int[r - l + 1]; + int p1 = m; + int p2 = r; + int i = help.length - 1; + while (p1 >= l && p2 > m) { + res[i] += arr[p1] > arr[p2] ? (p2 - m) : 0; + help[i--] = arr[p1] > arr[p2] ? arr[p1--] : arr[p2--]; + } + while (p1 >= l) { + help[i--] = arr[p1--]; + } + while (p2 > m) { + help[i--] = arr[p2--]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + } +} + +class Main_315{ + + + + public static void main(String[] args){ + int[] nums = new int[]{3,7,5,2,6,1}; + List list = Solution.countSmaller(nums); + System.out.println(list); + + } + +} diff --git a/src/leo/class03_04/BiggerThanRightTwice.java b/src/leo/class03_04/BiggerThanRightTwice.java index 44f0f92..cb36c26 100644 --- a/src/leo/class03_04/BiggerThanRightTwice.java +++ b/src/leo/class03_04/BiggerThanRightTwice.java @@ -199,6 +199,102 @@ class BiggerThanRightTwice3 { } } +class BiggerThanRightTwice4 { + + public static int biggerTwice(int[] arr) { + if (arr.length < 2 || arr == null) { + return 0; + } + return process(arr, 0, arr.length - 1); + } + + public static int process(int[] arr, int l, int r) { + if (l == r) { + return 0; + } + int m = l + ((r - l) >> 1); + return process(arr, l, m) + process(arr, m + 1, r) + merge(arr, l, m, r); + } + + private static int merge(int[] arr, int l, int m, int r) { + int res = 0; + int windowR = m + 1; + int i; + for (i = l; i <= m; i++) { + while (windowR <= r && arr[i] > (arr[windowR] << 1)) { + windowR++; + } + res += windowR - m - 1; + } + int p1= l; + int p2 = m + 1; + int[] help = new int[r - l + 1]; + i = 0; + 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 BiggerThanRightTwice5 { + public static int biggerTwice(int[] arr) { + if (arr.length < 2 || arr == null) { + return 0; + } + return process(arr, 0, arr.length - 1); + } + + public static int process(int[] arr, int l, int r) { + if (l == r) { + return 0; + } + int m = l + ((r - l) >> 1); + return process(arr, l, m) + process(arr, m + 1, r) + merge(arr, l, m, r); + } + + private static int merge(int[] arr, int l, int m, int r) { + int windowR = m+1; + int res = 0; + int i = l; + for (; i <= m; i++) { + while (windowR <= r && arr[i] > (arr[windowR] << 1)) { + windowR++; + } + res += windowR - m - 1; + } + int p1 = l; + int p2 = m + 1; + int[] help = new int[r - l + 1]; + i = 0; + 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 TestBiggerThanRightTwice { public static int biggerTwice(int[] arr) { @@ -224,7 +320,7 @@ class Main{ for (int i = 0; i < testTime; i++) { int[] arr = generateRandomArray(sizeMax, range); int[] copyArray = copyArray(arr); - int res = BiggerThanRightTwice3.biggerTwice(arr); + int res = BiggerThanRightTwice5.biggerTwice(arr); int res1 = TestBiggerThanRightTwice.biggerTwice(copyArray); if (res != res1) { System.out.println("res : " + res + " " + " res1: " + res1); diff --git a/src/leo/class07/NodeAncestorIntersection.java b/src/leo/class07/NodeAncestorIntersection.java new file mode 100644 index 0000000..a390484 --- /dev/null +++ b/src/leo/class07/NodeAncestorIntersection.java @@ -0,0 +1,11 @@ +package leo.class07; + +/** + * @author Leo + * @ClassName NodeAncestorIntersection + * @DATE 2020/12/6 9:58 上午 + * @Description + * 求X祖先节点交集 先序x之前的节点与后序x之后的几点交集就是x的祖先节点 + */ +public class NodeAncestorIntersection { +} diff --git a/src/leo/class07/TraversalBT.java b/src/leo/class07/TraversalBT.java new file mode 100644 index 0000000..5d672af --- /dev/null +++ b/src/leo/class07/TraversalBT.java @@ -0,0 +1,171 @@ +package leo.class07; + + +import java.util.Stack; + +/** + * @author Leo + * @ClassName RecursiveTraversalBT + * @DATE 2020/12/4 4:41 下午 + * @Description 递归遍历二叉树 + * 先序:头左右 + * 中序:左头右 + * 后续:左右头 + * + */ +public class TraversalBT { + + + + + + public static void main(String[] args){ + TreeNode head = new TreeNode(1); + head.left = new TreeNode(2); + head.right = new TreeNode(3); + head.left.left = new TreeNode(4); + head.left.right = new TreeNode(5); + head.right.left = new TreeNode(6); + head.right.right = new TreeNode(7); + + Recursive.pre(head); + System.out.println(); + UnRecursive.pre(head); + System.out.println("--------"); + Recursive.in(head); + System.out.println(); + UnRecursive.in(head); + System.out.println("---------"); + Recursive.pos(head); + System.out.println(); + + UnRecursive.pos(head); + System.out.println("---------"); + + } + +} + + +/** + * 递归序, + * 每个节点都会遍历三次 + */ +class Recursive{ + + public static void pre(TreeNode node) { + if (node == null) { + return; + } + System.out.print(node.value+" "); + pre(node.left); + pre(node.right); + } + + public static void in(TreeNode node) { + if (node == null) { + return; + } + in(node.left); + System.out.print(node.value+" "); + in(node.right); + } + + public static void pos(TreeNode node) { + if (node == null) { + return; + } + pos(node.left); + pos(node.right); + System.out.print(node.value+" "); + } + +} + + +class UnRecursive{ + + + /** + * 前序:头左右 + */ + public static void pre(TreeNode head) { + if (head == null) { + return; + } + System.out.print("pre-order: "); + Stack stack = new Stack<>(); + stack.add(head); + while (!stack.isEmpty()) { + head = stack.pop(); + System.out.print(head.value + " "); + if (head.right != null) { + stack.push(head.right); + } + if (head.left != null) { + stack.push(head.left); + } + } + System.out.println(); + } + + /** + * 中序:左头右 + */ + public static void in(TreeNode head) { + if (head == null) { + return; + } + System.out.print("in-order: "); + Stack stack = new Stack<>(); + TreeNode cur = head; + while (cur != null || !stack.isEmpty()) { + if (cur != null) { + stack.push(cur); + cur = cur.left; + }else { + cur = stack.pop(); + System.out.print(cur.value + " "); + cur = cur.right; + } + } + System.out.println(); + + } + + + + /** + * 左右头 + */ + public static void pos(TreeNode head){ + if (head == null) { + return; + } + System.out.print("pos-order: "); + Stack s1 = new Stack<>(); + Stack s2 = new Stack<>(); + TreeNode cur = head; + s1.push(cur); + while (!s1.isEmpty()) { + cur = s1.pop(); + s2.push(cur); + if (cur.left != null) { + s1.push(cur.left); + } + if (cur.right != null) { + s1.push(cur.right); + } + } + while (!s2.isEmpty()) { + System.out.print(s2.pop().value + " "); + } + System.out.println(); + + } + +} + + + + diff --git a/src/leo/class07/TreeNode.java b/src/leo/class07/TreeNode.java new file mode 100644 index 0000000..b8417e2 --- /dev/null +++ b/src/leo/class07/TreeNode.java @@ -0,0 +1,17 @@ +package leo.class07; + +/** + * @author Leo + * @ClassName TreeNode + * @DATE 2020/12/5 9:58 下午 + * @Description + */ +public class TreeNode { + int value; + TreeNode left; + TreeNode right; + + public TreeNode(int value) { + this.value = value; + } +}