diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/config.json" similarity index 67% rename from "data_source/exercises/\344\270\255\347\255\211/java/156.exercises/config.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/config.json" index 5b820033185edda219a4e6213583a457754d38e8..4a387aa0c659a1ba2f685c5db4d32cf10198c5aa 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/config.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/config.json" @@ -1,5 +1,5 @@ { - "node_id": "dailycode-723aa8aa68aa4900b87d54c943a6b8f3", + "node_id": "dailycode-66ca05246be44a6f8225b8b7b9a48b5a", "keywords": [], "children": [], "keywords_must": [], diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0d030b9f6124d9156199faa14e84611b962e1d30 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "672b66254337491fa4c259201dff5c7c", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..de52d97d03d774de4b3bfc6d0ad7c52a1aa1a7fd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/101.exercises/solution.md" @@ -0,0 +1,96 @@ +# 对称二叉树 + +

给定一个二叉树,检查它是否是镜像对称的。

+ +

 

+ +

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

+ +
    1
+   / \
+  2   2
+ / \ / \
+3  4 4  3
+
+ +

 

+ +

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

+ +
    1
+   / \
+  2   2
+   \   \
+   3    3
+
+ +

 

+ +

进阶:

+ +

你可以运用递归和迭代两种方法解决这个问题吗?

+ + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public boolean isSymmetric(TreeNode root) { + if (root == null) { + return true; + } + + return isSymmetric(root.left, root.right); + } + + public boolean isSymmetric(TreeNode n1, TreeNode n2) { + if (n1 == null || n2 == null) { + return n1 == n2; + } + + if (n1.val != n2.val) { + return false; + } + + return isSymmetric(n1.left, n2.right) && isSymmetric(n1.right, n2.left); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/config.json" similarity index 67% rename from "data_source/exercises/\344\270\255\347\255\211/java/161.exercises/config.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/config.json" index ab669edf23a4422e5a2051e29d2948b8b9eef0b1..aaa93dd7fda1d470fbb2bab118c3ce01d39fff95 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/config.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/config.json" @@ -1,5 +1,5 @@ { - "node_id": "dailycode-a49b8a8141784360b8a3e9f68e694db6", + "node_id": "dailycode-ca242207dcca46e98a855afe39ba7bc6", "keywords": [], "children": [], "keywords_must": [], diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2284a92adb8b8c980928a213dcd4475d530aad08 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "14ab4d85d03c4c1e9bae31166d22c95b", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d5766383fdda74d979389ba8a69b332ef2a7a20 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/104.exercises/solution.md" @@ -0,0 +1,88 @@ +# 二叉树的最大深度 + +

给定一个二叉树,找出其最大深度。

+ +

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

+ +

说明: 叶子节点是指没有子节点的节点。

+ +

示例:
+给定二叉树 [3,9,20,null,null,15,7]

+ +
    3
+   / \
+  9  20
+    /  \
+   15   7
+ +

返回它的最大深度 3 。

+ + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public int maxDepth(TreeNode root) { + + if (root == null) { + return 0; + } + + int deep = 1; + if (root.left == null && root.right == null) { + return 1; + } + + int leftDeep = 0; + if (root.left != null) { + + leftDeep = 1 + maxDepth(root.left); + } + + int rightDeep = 0; + if (root.right != null) { + rightDeep = 1 + maxDepth(root.right); + } + + return deep + leftDeep > rightDeep ? leftDeep : rightDeep; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/config.json" similarity index 67% rename from "data_source/exercises/\344\270\255\347\255\211/java/177.exercises/config.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/config.json" index c11e108442fd128eabc06add068ab52cc5d50651..7fff488f1c159db46829a513eaa9fadd2c8ca3fe 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/config.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/config.json" @@ -1,5 +1,5 @@ { - "node_id": "dailycode-371bbacdc9d94528911b28efdc64e849", + "node_id": "dailycode-c3fc112caa8346719353b5b53dd23b67", "keywords": [], "children": [], "keywords_must": [], diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a13fae224fcb516087f85660ecaa36e25a5d0893 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "42d0f88da8434410bbeb20c494e40359", + "author": "csdn.net", + "keywords": "树,二叉搜索树,数组,分治,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9656e855510746a2e2ef7a20c1ba61d15bcc2421 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/108.exercises/solution.md" @@ -0,0 +1,101 @@ +# 将有序数组转换为二叉搜索树 + +

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。

+ +

高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [-10,-3,0,5,9]
+输出:[0,-3,9,-10,null,5]
+解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:
+
+
+ +

示例 2:

+ +
+输入:nums = [1,3]
+输出:[3,1]
+解释:[1,3] 和 [3,1] 都是高度平衡二叉搜索树。
+
+ +

 

+ +

提示:

+ + + + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return traversal(nums, 0, nums.length - 1); + } + + public TreeNode traversal(int[] nums, int left, int right) { + if (left > right) + return null; + int mid = left + ((right - left) / 2); + TreeNode node = new TreeNode(nums[mid]); + node.left = traversal(nums, left, mid - 1); + node.right = traversal(nums, mid + 1, right); + return node; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/config.json" similarity index 67% rename from "data_source/exercises/\344\270\255\347\255\211/java/159.exercises/config.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/config.json" index e7efb558a8fdef6ff706e0d874d34a101e5da4f4..c1ca0425d8ba413dda180695712eb67bf12210fb 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/config.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/config.json" @@ -1,5 +1,5 @@ { - "node_id": "dailycode-e7d7681d60664ee98ceeb183ce7db2d5", + "node_id": "dailycode-e76dc351a33e4e8e956eebe31bd660e7", "keywords": [], "children": [], "keywords_must": [], diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.json" similarity index 70% rename from "data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.json" index 98022c6b829984428ce5d8711051bd9732a88db8..8c6d354d08375aad26b39170cc228d4e8201dc1c 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "707b8880504e45a495a7a26cacb3b636", + "exercise_id": "1c1d9f792cc84d2bb4bb1ef93281f4fc", "author": "csdn.net", "keywords": "树,深度优先搜索,二叉树" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ceca68b567883670d4a0d6a3cc241be802d5b90b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/110.exercises/solution.md" @@ -0,0 +1,102 @@ +# 平衡二叉树 + +

给定一个二叉树,判断它是否是高度平衡的二叉树。

+ +

本题中,一棵高度平衡二叉树定义为:

+ +
+

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。

+
+ +

 

+ +

示例 1:

+ +
+输入:root = [3,9,20,null,null,15,7]
+输出:true
+
+ +

示例 2:

+ +
+输入:root = [1,2,2,3,3,null,null,4,4]
+输出:false
+
+ +

示例 3:

+ +
+输入:root = []
+输出:true
+
+ +

 

+ +

提示:

+ + + + +## template + +```java +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +public class Solution { + public boolean isBalanced(TreeNode root) { + + if (root == null) { + return true; + } + + return (Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1) && isBalanced(root.left) + && isBalanced(root.right); + } + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..94a6776d0b8eca534da428d676b900fa4f573d55 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4ab0aa7bfc32444e99ac0fb88b7df7df", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..83ca3dd09dc28f1006681d293795ee607d7aa28f --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "eff5f836db954d70a569abed5ab33275", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..168561774d495e6881b45c89312825e911565987 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/111.exercises/solution.md" @@ -0,0 +1,100 @@ +# 二叉树的最小深度 + +

给定一个二叉树,找出其最小深度。

+ +

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

+ +

说明:叶子节点是指没有子节点的节点。

+ +

 

+ +

示例 1:

+ +
+输入:root = [3,9,20,null,null,15,7]
+输出:2
+
+ +

示例 2:

+ +
+输入:root = [2,null,3,null,4,null,5,null,6]
+输出:5
+
+ +

 

+ +

提示:

+ + + + +## template + +```java +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public int minDepth(TreeNode root) { + + if (root == null) { + return 0; + } + + if (root.left == null && root.right == null) { + return 1; + } + + int min_depth = Integer.MAX_VALUE; + if (root.left != null) { + int countLeft = 1; + countLeft += minDepth(root.left); + min_depth = Math.min(countLeft, min_depth); + } + if (root.right != null) { + int countRight = 1; + countRight += minDepth(root.right); + min_depth = Math.min(min_depth, countRight); + } + return min_depth; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f0c6ab5dba8ae75704aa01a0fa2b0cb6657c185e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-7ae1e736780545aebb14d68222a03202", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3574e3ae1c8246c31f9e5dcd819cfd8fcc3ba28e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "062f086ad7714ec0972b404dea1f2d5d", + "author": "csdn.net", + "keywords": "树,深度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..91a7a5c082db6f446fd095b43bd17d22a24304f6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/112.exercises/solution.md" @@ -0,0 +1,119 @@ +# 路径总和 + +

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum

+ +

叶子节点 是指没有子节点的节点。

+ +

 

+ +

示例 1:

+ +
+输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
+输出:true
+
+ +

示例 2:

+ +
+输入:root = [1,2,3], targetSum = 5
+输出:false
+
+ +

示例 3:

+ +
+输入:root = [1,2], targetSum = 0
+输出:false
+
+ +

 

+ +

提示:

+ + + + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + if (root == null) + return false; + return traversal(root, targetSum - root.val); + } + + public boolean traversal(TreeNode root, int count) { + if (root.left == null && root.right == null && count == 0) { + return true; + } + if (root.left == null && root.right == null) { + return false; + } + if (root.left != null) { + count -= root.left.val; + if (traversal(root.left, count)) + return true; + count += root.left.val; + } + if (root.right != null) { + count -= root.right.val; + if (traversal(root.right, count)) + return true; + count += root.right.val; + } + return false; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..456ecd44f06b467e165101bb977b0e13fa93b2bc --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-25153021b90e453bb325a5d3b8d8dc4c", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.json" similarity index 68% rename from "data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.json" index 93feb0f917aa40e4dcb25c046c6bd8ea9f109478..33180356d902beb1bdf704804e7b9426cf1e3469 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "7801defd13f04fc9bedabdebb93aec79", + "exercise_id": "f1d66b19312c4c78ace8697acb74a2c7", "author": "csdn.net", "keywords": "数组,动态规划" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c032f3a5cec8c022f6c5f2c5ff5faca3ad64af55 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/118.exercises/solution.md" @@ -0,0 +1,87 @@ +# 杨辉三角 + +

给定一个非负整数 numRows生成「杨辉三角」的前 numRows 行。

+ +

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

+ +

+ +

 

+ +

示例 1:

+ +
+输入: numRows = 5
+输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
+
+ +

示例 2:

+ +
+输入: numRows = 1
+输出: [[1]]
+
+ +

 

+ +

提示:

+ + + + +## template + +```java +class Solution { + public List> generate(int numRows) { + List> res = new ArrayList>(); + if (numRows == 0) { + return res; + } + for (int i = 0; i < numRows; ++i) { + List temp = new ArrayList(); + for (int j = 0; j <= i; ++j) { + temp.add(numberget(i, j)); + } + res.add(temp); + } + return res; + } + + public int numberget(int i, int j) { + if (j == 0 || i == j) { + return 1; + } else { + return (numberget(i - 1, j - 1) + numberget(i - 1, j)); + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..eaa705e7c15dd01f892ad0a7a2c6a326e79b2b44 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4ad6a6fb0c5d411480a1187f639e73eb", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..52bb5efaad579be96662bade14bfaf56a7f2d5cf --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "1db77f4044264ab9ac22c7ae33720199", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f7c1b9096c18f4e6cd88eae908e79707402e0b86 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/119.exercises/solution.md" @@ -0,0 +1,87 @@ +# 杨辉三角 II + +

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。

+ +

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

+ +

+ +

 

+ +

示例 1:

+ +
+输入: rowIndex = 3
+输出: [1,3,3,1]
+
+ +

示例 2:

+ +
+输入: rowIndex = 0
+输出: [1]
+
+ +

示例 3:

+ +
+输入: rowIndex = 1
+输出: [1,1]
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= rowIndex <= 33
  • +
+ +

 

+ +

进阶:

+ +

你可以优化你的算法到 O(rowIndex) 空间复杂度吗?

+ + +## template + +```java +class Solution { + public List getRow(int rowIndex) { + List row = new ArrayList(); + for (int i = 0; i < rowIndex + 1; i++) { + row.add(0, 1); + for (int j = 1; j < row.size() - 1; j++) + row.set(j, row.get(j) + row.get(j + 1)); + } + return row; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9f4c5b85eb2533f9903768e9684f756ad97d9307 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-f213e4fdf7844133abe7932c6a718d41", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..315bb7a41598147fedaa59001416741f637666af --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "ccc2aad185964504ae2ad5e26e6a3d5b", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4b3bd22cd60b8258526d9750bf381a07505812fa --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/121.exercises/solution.md" @@ -0,0 +1,78 @@ +# 买卖股票的最佳时机 + +

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

+ +

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

+ +

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0

+ +

 

+ +

示例 1:

+ +
+输入:[7,1,5,3,6,4]
+输出:5
+解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
+     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。
+
+ +

示例 2:

+ +
+输入:prices = [7,6,4,3,1]
+输出:0
+解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= prices.length <= 105
  • +
  • 0 <= prices[i] <= 104
  • +
+ + +## template + +```java +class Solution { + public int maxProfit(int[] prices) { + int temp = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + temp = temp + prices[i + 1] - prices[i]; + } + } + return temp; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..68aff022ec4dff45c369f61e73678a46403af9d6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-389f96534c054f91aa595194b11dfd60", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a5b91b1b7d3c2bd7e2cadb975128cf99925e27d2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "c5c580bc2d6e44b1915e9d0b7b297dea", + "author": "csdn.net", + "keywords": "贪心,数组,动态规划" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1b1a9e8e5bed9b79cfd0037b0cb1bb11b103c2fe --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/122.exercises/solution.md" @@ -0,0 +1,86 @@ +# 买卖股票的最佳时机 II + +

给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。

+ +

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

+ +

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

+ +

 

+ +

示例 1:

+ +
+输入: prices = [7,1,5,3,6,4]
+输出: 7
+解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
+
+ +

示例 2:

+ +
+输入: prices = [1,2,3,4,5]
+输出: 4
+解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
+     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
+
+ +

示例 3:

+ +
+输入: prices = [7,6,4,3,1]
+输出: 0
+解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= prices.length <= 3 * 104
  • +
  • 0 <= prices[i] <= 104
  • +
+ + +## template + +```java +class Solution { + public int maxProfit(int[] prices) { + int profit = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i] < prices[i + 1]) { + profit += prices[i + 1] - prices[i]; + } + } + return profit; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..774e6a0e266d3434c68d91df75478c3a74998f2c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-80a6b5a5b7574651b76f737299e2389a", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.json" similarity index 68% rename from "data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.json" index 375bf5789af912999a4b4ab9875703be56c41b55..d39a4f7a66f112e793fa5ffc73ceee1baf67e63d 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "68c60fe72f2b4e128e064e4b988fe209", + "exercise_id": "4494efbab4814171a54ce41adcb45eae", "author": "csdn.net", "keywords": "双指针,字符串" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a8f09e4933f435600755e62f009d050d97f3f5f0 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/125.exercises/solution.md" @@ -0,0 +1,87 @@ +# 验证回文串 + +

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写。

+ +

说明:本题中,我们将空字符串定义为有效的回文串。

+ +

 

+ +

示例 1:

+ +
+输入: "A man, a plan, a canal: Panama"
+输出: true
+解释:"amanaplanacanalpanama" 是回文串
+
+ +

示例 2:

+ +
+输入: "race a car"
+输出: false
+解释:"raceacar" 不是回文串
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 2 * 105
  • +
  • 字符串 s 由 ASCII 字符组成
  • +
+ + +## template + +```java +class Solution { + public boolean isPalindrome(String s) { + StringBuffer str = new StringBuffer(); + int len = s.length(); + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + if (Character.isLetterOrDigit(c)) { + str.append(Character.toLowerCase(c)); + } + } + int left = 0; + int right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8c46006195a6a8b9955eff1515d8039682b00c39 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-5a7acc6c506d47d689374ccaa3f71ea6", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..678892fdfcf9d64dadae26a4468c4eaf93d46ec8 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "ff6c003296da40eeaf2b8313f359ff96", + "author": "csdn.net", + "keywords": "位运算,数组" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a95e7d213e853ae00d0126f3d749fe63255358cd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/136.exercises/solution.md" @@ -0,0 +1,60 @@ +# 只出现一次的数字 + +

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

+ +

说明:

+ +

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

+ +

示例 1:

+ +
输入: [2,2,1]
+输出: 1
+
+ +

示例 2:

+ +
输入: [4,1,2,1,2]
+输出: 4
+ + +## template + +```java +class Solution { + public int singleNumber(int[] nums) { + int res = 0; + for (int num : nums) { + res ^= num; + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..b21be7d33965eb495ac29ec7c572b2e4dd1fda3b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-370c10ca633548ccb9204d042c41b60a", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.json" similarity index 69% rename from "data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.json" index 311c98cd2581ee447bdac4382056ffc28c8625c0..35d5cc8d44bb126054fca7090ceed419737860ac 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "c67f79a9c54d421d9a06e510234d7ae5", + "exercise_id": "a0cd256a454c4708ade034055b3c2d27", "author": "csdn.net", "keywords": "哈希表,链表,双指针" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..04feeba296420193fb2bf2e47cae370bff9f3386 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/141.exercises/solution.md" @@ -0,0 +1,110 @@ +# 环形链表 + +

给定一个链表,判断链表中是否有环。

+ +

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos-1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

+ +

如果链表中存在环,则返回 true 。 否则,返回 false

+ +

 

+ +

进阶:

+ +

你能用 O(1)(即,常量)内存解决此问题吗?

+ +

 

+ +

示例 1:

+ +

+ +
输入:head = [3,2,0,-4], pos = 1
+输出:true
+解释:链表中有一个环,其尾部连接到第二个节点。
+
+ +

示例 2:

+ +

+ +
输入:head = [1,2], pos = 0
+输出:true
+解释:链表中有一个环,其尾部连接到第一个节点。
+
+ +

示例 3:

+ +

+ +
输入:head = [1], pos = -1
+输出:false
+解释:链表中没有环。
+
+ +

 

+ +

提示:

+ +
    +
  • 链表中节点的数目范围是 [0, 104]
  • +
  • -105 <= Node.val <= 105
  • +
  • pos-1 或者链表中的一个 有效索引
  • +
+ + +## template + +```java + +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } +} + +public class Solution { + public boolean hasCycle(ListNode head) { + if (head == null || head.next == null) + return false; + ListNode fast = head.next; + ListNode slow = head; + while (fast != slow) { + if (fast.next == null || fast.next.next == null) + return false; + fast = fast.next.next; + slow = slow.next; + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c2ddb7c6fb74cc0f88445d65b6ba21e562c8dc84 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4db8147b18e341afbe0f3e2e0b07a9fe", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c586e0392a906f2e4a6156ac282f5b1682cf5e05 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "39babd61b271490cb6415284b6e05eb5", + "author": "csdn.net", + "keywords": "栈,树,深度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..80a37e8a782684a972f4a22b4a708287a3d8d4b9 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/144.exercises/solution.md" @@ -0,0 +1,120 @@ +# 二叉树的前序遍历 + +

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,null,2,3]
+输出:[1,2,3]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1]
+输出:[1]
+
+ +

示例 4:

+ +
+输入:root = [1,2]
+输出:[1,2]
+
+ +

示例 5:

+ +
+输入:root = [1,null,2]
+输出:[1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点数目在范围 [0, 100]
  • +
  • -100 <= Node.val <= 100
  • +
+ +

 

+ +

进阶:递归算法很简单,你可以通过迭代算法完成吗?

+ + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List preorderTraversal(TreeNode root) { + List resultList = new ArrayList<>(); + if (root == null) { + return resultList; + } + + helper(resultList, root); + + return resultList; + } + + public void helper(List resultList, TreeNode root) { + if (root == null) + return; + + resultList.add(root.val); + helper(resultList, root.left); + helper(resultList, root.right); + + } + +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..063d7636df614beef7f0022a2e17796824217f03 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-0705c77e457b4adc983a4780c85b7ada", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ce335e62cc2cbac7afde2efaa0a1a3e58b86f5e4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "8436b24b75714318a55242d1a0bc863a", + "author": "csdn.net", + "keywords": "栈,树,深度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ec4126a91f2b41b1b395b7c0d5453097bcf413e3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/145.exercises/solution.md" @@ -0,0 +1,84 @@ +# 二叉树的后序遍历 + +

给定一个二叉树,返回它的 后序 遍历。

+ +

示例:

+ +
输入: [1,null,2,3]  
+   1
+    \
+     2
+    /
+   3 
+
+输出: [3,2,1]
+ +

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

+ + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List postorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + Stack nodeStack = new Stack<>(); + TreeNode nodeTemp = root; + TreeNode preNode = null; + while (nodeTemp != null || !nodeStack.isEmpty()) { + while (nodeTemp != null) { + nodeStack.push(nodeTemp); + nodeTemp = nodeTemp.left; + } + nodeTemp = nodeStack.peek(); + if (nodeTemp.right == null || nodeTemp.right == preNode) { + nodeTemp = nodeStack.pop(); + list.add(nodeTemp.val); + preNode = nodeTemp; + nodeTemp = null; + } else { + nodeTemp = nodeTemp.right; + } + } + return list; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7852abea7e17231cb67d293ffd7cd41250235161 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-55d129895ce14cc981c8bcb22cf1afa0", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..60e9239faba3c45e56a2bde36f272d32a0a41b9e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "a8fcaf74a25746008bc8052396b6d03d", + "author": "csdn.net", + "keywords": "栈,设计" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a8fb8cbed9b247aa2e6bba7375be53da60959b5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/155.exercises/solution.md" @@ -0,0 +1,126 @@ +# 最小栈 + +

设计一个支持 pushpoptop 操作,并能在常数时间内检索到最小元素的栈。

+ +
    +
  • push(x) —— 将元素 x 推入栈中。
  • +
  • pop() —— 删除栈顶的元素。
  • +
  • top() —— 获取栈顶元素。
  • +
  • getMin() —— 检索栈中的最小元素。
  • +
+ +

 

+ +

示例:

+ +
输入:
+["MinStack","push","push","push","getMin","pop","top","getMin"]
+[[],[-2],[0],[-3],[],[],[],[]]
+
+输出:
+[null,null,null,null,-3,null,0,-2]
+
+解释:
+MinStack minStack = new MinStack();
+minStack.push(-2);
+minStack.push(0);
+minStack.push(-3);
+minStack.getMin();   --> 返回 -3.
+minStack.pop();
+minStack.top();      --> 返回 0.
+minStack.getMin();   --> 返回 -2.
+
+ +

 

+ +

提示:

+ +
    +
  • poptopgetMin 操作总是在 非空栈 上调用。
  • +
+ + +## template + +```java +class MinStack { + + Stack data_stack; + Stack min_stack; + + /** initialize your data structure here. */ + public MinStack() { + data_stack = new Stack(); + min_stack = new Stack(); + } + + public void push(int x) { + data_stack.push(x); + if (min_stack.isEmpty()) { + + min_stack.push(x); + } else { + if (x > min_stack.peek()) { + + x = min_stack.peek(); + } + min_stack.push(x); + } + + } + + public void pop() { + + data_stack.pop(); + min_stack.pop(); + + } + + public int top() { + + return data_stack.peek(); + + } + + public int getMin() { + + return min_stack.peek(); + + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(x); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..192151526c3860263ae949a7845e35a7b0e639e3 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-05d8225288564107b6082081bcb7efa8", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..43eb20b1579d3da408a0dfbdce15e36e5a8a4522 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "8521589a445c45d9ac56f1864496e2e9", + "author": "csdn.net", + "keywords": "数组,双指针,二分查找" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..25d289cd97f93ed2fb1b8e37d22892cf81bd9b29 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/167.exercises/solution.md" @@ -0,0 +1,99 @@ +# 两数之和 II + +

给定一个已按照 非递减顺序排列  的整数数组 numbers ,请你从数组中找出两个数满足相加之和等于目标数 target

+ +

函数应该以长度为 2 的整数数组的形式返回这两个数的下标值numbers 的下标 从 1 开始计数 ,所以答案数组应当满足 1 <= answer[0] < answer[1] <= numbers.length

+ +

你可以假设每个输入 只对应唯一的答案 ,而且你 不可以 重复使用相同的元素。

+  + +

示例 1:

+ +
+输入:numbers = [2,7,11,15], target = 9
+输出:[1,2]
+解释:2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
+
+ +

示例 2:

+ +
+输入:numbers = [2,3,4], target = 6
+输出:[1,3]
+
+ +

示例 3:

+ +
+输入:numbers = [-1,0], target = -1
+输出:[1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 2 <= numbers.length <= 3 * 104
  • +
  • -1000 <= numbers[i] <= 1000
  • +
  • numbers非递减顺序 排列
  • +
  • -1000 <= target <= 1000
  • +
  • 仅存在一个有效答案
  • +
+ + +## template + +```java +class Solution { + public int[] twoSum(int[] numbers, int target) { + int[] answer = new int[2]; + int n = numbers.length; + for (int i = 0; i < n - 1; i++) { + int t = target - numbers[i]; + answer[0] = i + 1; + for (int j = i + 1; j < n; j++) { + if (numbers[j] > t) { + break; + } + if (numbers[j] == t) { + answer[1] = j + 1; + break; + } + } + + if (answer[1] != 0) { + break; + } + } + return answer; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..243e19c92b884dbf9b9c06c786019b876387fdb1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-690b38df27ba49b48fabe919b665d389", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..76a434f2f248e6e5fda42d020707a54c351a1faa --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "38a29f7f99734ae9905da9059c67b34a", + "author": "csdn.net", + "keywords": "数学,字符串" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f1bfa83ab6ddda1852907254a0bf2c62276933e8 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/168.exercises/solution.md" @@ -0,0 +1,102 @@ +# Excel表列名称 + +

给你一个整数 columnNumber ,返回它在 Excel 表中相对应的列名称。

+ +

例如:

+ +
+A -> 1
+B -> 2
+C -> 3
+...
+Z -> 26
+AA -> 27
+AB -> 28 
+...
+
+ +

 

+ +

示例 1:

+ +
+输入:columnNumber = 1
+输出:"A"
+
+ +

示例 2:

+ +
+输入:columnNumber = 28
+输出:"AB"
+
+ +

示例 3:

+ +
+输入:columnNumber = 701
+输出:"ZY"
+
+ +

示例 4:

+ +
+输入:columnNumber = 2147483647
+输出:"FXSHRXW"
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= columnNumber <= 231 - 1
  • +
+ + +## template + +```java +class Solution { + public String convertToTitle(int n) { + if (n <= 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + while (n > 0) { + n--; + sb.append((char) (n % 26 + 'A')); + + n = n / 26; + } + return sb.reverse().toString(); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2e334cb3d5eabc7e0dcc04ac1763acd5c594daf7 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4d8dc4194a51436a8d2dcae69f41db85", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b2bc9340c985671249ba30a18fa8a7f095f7b900 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "13c7f88edeac4365a94866390f6ec8a4", + "author": "csdn.net", + "keywords": "数组,哈希表,分治,计数,排序" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..bc2c320022faba922c7afa55daf2d1a5b80fcb7a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/169.exercises/solution.md" @@ -0,0 +1,78 @@ +# 多数元素 + +

给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

+ +

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

+ +

 

+ +

示例 1:

+ +
+输入:[3,2,3]
+输出:3
+ +

示例 2:

+ +
+输入:[2,2,1,1,1,2,2]
+输出:2
+
+ +

 

+ +

进阶:

+ +
    +
  • 尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。
  • +
+ + +## template + +```java +class Solution { + public int majorityElement(int[] nums) { + int res = nums[0]; + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (res == nums[i]) { + count++; + } else { + count--; + } + if (count == 0) { + res = nums[i + 1]; + } + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..91bb915439676e22277e02d3c173c63cf4197674 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-fc3ec92515bf4a6092fc356967294cde", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4b8e62218eda292fc146c5303784c294249dce3b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d2fcc6a04d4f4c54bc47bd175dc76a8c", + "author": "csdn.net", + "keywords": "数学,字符串" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e12ec0a1147ddbae63ac8894a1b62628c39593f5 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/171.exercises/solution.md" @@ -0,0 +1,101 @@ +# Excel表列序号 + +

给你一个字符串 columnTitle ,表示 Excel 表格中的列名称。返回该列名称对应的列序号。

+ +

 

+ +

例如,

+ +
+    A -> 1
+    B -> 2
+    C -> 3
+    ...
+    Z -> 26
+    AA -> 27
+    AB -> 28 
+    ...
+
+ +

 

+ +

示例 1:

+ +
+输入: columnTitle = "A"
+输出: 1
+
+ +

示例 2:

+ +
+输入: columnTitle = "AB"
+输出: 28
+
+ +

示例 3:

+ +
+输入: columnTitle = "ZY"
+输出: 701
+ +

示例 4:

+ +
+输入: columnTitle = "FXSHRXW"
+输出: 2147483647
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= columnTitle.length <= 7
  • +
  • columnTitle 仅由大写英文组成
  • +
  • columnTitle 在范围 ["A", "FXSHRXW"]
  • +
+ + +## template + +```java +class Solution { + public int titleToNumber(String s) { + char[] charArray = s.toCharArray(); + int res = 0; + for (int i = 0; i < charArray.length; i++) { + res = res * 26 + (charArray[i] - 'A' + 1); + } + + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..45188075765e588431e925f0989d4187739dc21d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-0556176a0ee046e58601aa33957f0174", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.json" similarity index 50% rename from "data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.json" index c73547d7f999f568cfdce81ec7cfa1b886e9dacc..05f929a85065bdef5d6a41b7742cbace18092753 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "ffafdc01e857441b8967dca2e56add9a", + "exercise_id": "b44eca465b3142e686ce6c2abdde09b6", "author": "csdn.net", - "keywords": "shell" + "keywords": "数学" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..106b878f661e6a0e24b7899906f6dcc83d2fc12e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/172.exercises/solution.md" @@ -0,0 +1,86 @@ +# 阶乘后的零 + +

给定一个整数 n ,返回 n! 结果中尾随零的数量。

+ +

提示 n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1

+ +

 

+ +

示例 1:

+ +
+输入:n = 3
+输出:0
+解释:3! = 6 ,不含尾随 0
+
+ +

示例 2:

+ +
+输入:n = 5
+输出:1
+解释:5! = 120 ,有一个尾随 0
+
+ +

示例 3:

+ +
+输入:n = 0
+输出:0
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= n <= 104
  • +
+ +

 

+ +

进阶:你可以设计并实现对数时间复杂度的算法来解决此问题吗?

+ + +## template + +```java +class Solution { + public int trailingZeroes(int n) { + int count = 0; + while (n >= 5) { + + count += n / 5; + n /= 5; + } + return count; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cbcdaa3dbb856771434f4a65620591c07e7e18f1 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e8bccfcdf74e4031916b9771f8935ac4", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..442be3e05f9d43c7495434696afdd03705f6e688 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "192a41fa78f94fb4a61b2e637cc02185", + "author": "csdn.net", + "keywords": "位运算,分治" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0a4f4361f1b3eed389d7f735742b64e1139a5b54 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/190.exercises/solution.md" @@ -0,0 +1,86 @@ +# 颠倒二进制位 + +

颠倒给定的 32 位无符号整数的二进制位。

+ +

提示:

+ +
    +
  • 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
  • +
  • 在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在 示例 2 中,输入表示有符号整数 -3,输出表示有符号整数 -1073741825
  • +
+ +

 

+ +

示例 1:

+ +
+输入:n = 00000010100101000001111010011100
+输出:964176192 (00111001011110000010100101000000)
+解释:输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
+     因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000
+ +

示例 2:

+ +
+输入:n = 11111111111111111111111111111101
+输出:3221225471 (10111111111111111111111111111111)
+解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
+     因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111 。
+ +

 

+ +

提示:

+ +
    +
  • 输入是一个长度为 32 的二进制字符串
  • +
+ +

 

+ +

进阶: 如果多次调用这个函数,你将如何优化你的算法?

+ + +## template + +```java +public class Solution { + + public int reverseBits(int n) { + int m = 0; + for (int i = 0; i < 32; i++) { + m <<= 1; + m = m | (n & 1); + n >>= 1; + } + return m; + + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..51dcfbad2582983039dddfe0469636ebe2130807 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-18e0740c78224c0d83c1948419e53eeb", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.json" similarity index 66% rename from "data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.json" index eefd584978238c0d6cc6839061595a593e0821e7..b38de7e6cbf17a20796f4fcfd471b6f57c840f73 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "b3b497521b344e26bb6259fb5dc21337", + "exercise_id": "fa66dcd535544b94bad5b9e43593bea7", "author": "csdn.net", "keywords": "位运算" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..138c8617f764598da8d35ffe60561f8edb578fef --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/191.exercises/solution.md" @@ -0,0 +1,101 @@ +# 位1的个数 + +

编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量)。

+ +

 

+ +

提示:

+ +
    +
  • 请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
  • +
  • 在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 3 中,输入表示有符号整数 -3
  • +
+ +

 

+ +

示例 1:

+ +
+输入:00000000000000000000000000001011
+输出:3
+解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
+
+ +

示例 2:

+ +
+输入:00000000000000000000000010000000
+输出:1
+解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
+
+ +

示例 3:

+ +
+输入:11111111111111111111111111111101
+输出:31
+解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
+ +

 

+ +

提示:

+ +
    +
  • 输入必须是长度为 32二进制串
  • +
+ +
    +
+ +

 

+ +

进阶

+ +
    +
  • 如果多次调用这个函数,你将如何优化你的算法?
  • +
+ + +## template + +```java +public class Solution { + + public int hammingWeight(int n) { + int count = 0; + while (n != 0) { + if ((n & 1) == 1) + count++; + n >>>= 1; + } + return count; + + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..035308931be38c86cc8ada2cdc161943bc1a7858 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e43fac5f1ff84cd9a3c291c3c5786bab", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..53a4597ec96c145f8fd50ec870a74287bf9df65e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "f0242839ac7946848bc8875703215c11", + "author": "csdn.net", + "keywords": "哈希表,数学,双指针" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..852fc68e2f1b86ca3e15cf18df3afef7ae6d8003 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/202.exercises/solution.md" @@ -0,0 +1,97 @@ +# 快乐数 + +

编写一个算法来判断一个数 n 是不是快乐数。

+ +

「快乐数」定义为:

+ +
    +
  • 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
  • +
  • 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
  • +
  • 如果 可以变为  1,那么这个数就是快乐数。
  • +
+ +

如果 n 是快乐数就返回 true ;不是,则返回 false

+ +

 

+ +

示例 1:

+ +
+输入:19
+输出:true
+解释:
+12 + 92 = 82
+82 + 22 = 68
+62 + 82 = 100
+12 + 02 + 02 = 1
+
+ +

示例 2:

+ +
+输入:n = 2
+输出:false
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= n <= 231 - 1
  • +
+ + +## template + +```java +class Solution { + public boolean isHappy(int n) { + List list = new ArrayList(); + list.add(n); + while (n != 1) { + int temp = 0; + while (n != 0) { + temp += (n % 10) * (n % 10); + n = n / 10; + } + n = temp; + if (list.contains(n)) { + break; + } else { + list.add(n); + } + + } + + return n == 1; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cb4529a674ed0e7f2c3aef4c56c32e23cfd52b5e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4905d42cd7414ebbbd6461a66e7dff8e", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..377ece96cf767087189f964b840636e220f7315e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "f3ba2ff196b945a2bf27cfd550d67616", + "author": "csdn.net", + "keywords": "递归,链表" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1eff0ebcd8403af54742ea9cbde4e738b7fd8a73 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/203.exercises/solution.md" @@ -0,0 +1,93 @@ +# 移除链表元素 + +给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。 +

 

+ +

示例 1:

+ +
+输入:head = [1,2,6,3,4,5,6], val = 6
+输出:[1,2,3,4,5]
+
+ +

示例 2:

+ +
+输入:head = [], val = 1
+输出:[]
+
+ +

示例 3:

+ +
+输入:head = [7,7,7,7], val = 7
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • 列表中的节点数目在范围 [0, 104]
  • +
  • 1 <= Node.val <= 50
  • +
  • 0 <= val <= 50
  • +
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public ListNode removeElements(ListNode head, int val) { + ListNode top = new ListNode(0); + top.next = head; + ListNode pre = top; + ListNode temp = head; + while (temp != null) { + if (temp.val == val) + pre.next = temp.next; + else + pre = temp; + temp = temp.next; + } + return top.next; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0863d4c28e222e6b455353fca07fc5ab5a89a047 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-67f780794df54083bfce3bfb4d3b8c6e", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fced5131e7d4fa413923f6f2e9c5dbc8a26eed86 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "c45c0162aa7b45e99b61f124096791c7", + "author": "csdn.net", + "keywords": "数组,数学,枚举,数论" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a31da07bb16d32bf42e56dabc0b6ea717439ecac --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/204.exercises/solution.md" @@ -0,0 +1,82 @@ +# 计数质数 + +

统计所有小于非负整数 n 的质数的数量。

+ +

 

+ +

示例 1:

+ +
输入:n = 10
+输出:4
+解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。
+
+ +

示例 2:

+ +
输入:n = 0
+输出:0
+
+ +

示例 3:

+ +
输入:n = 1
+输出:0
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= n <= 5 * 106
  • +
+ + +## template + +```java +class Solution { + public int countPrimes(int n) { + int flag = 0; + if (n > 2) + flag = 1; + for (int i = 0; i < n; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + break; + } + if (j == i - 1) { + flag++; + } + } + } + return flag; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6526e11a1feaaf2b18ef1b28e8f4fcde9d24f583 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-b1cf23dc1744491bbef9e069eb161345", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..513c1cff8a0564e718e17f6e220e5c12dbc5c734 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d570871f659447039e0b18ea93e03324", + "author": "csdn.net", + "keywords": "哈希表,字符串" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d6099de5ef3412b7b997ace05cc4af05a90647fd --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/205.exercises/solution.md" @@ -0,0 +1,92 @@ +# 同构字符串 + +

给定两个字符串 和 t,判断它们是否是同构的。

+ +

如果 中的字符可以按某种映射关系替换得到 ,那么这两个字符串是同构的。

+ +

每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。

+ +

 

+ +

示例 1:

+ +
+输入:s = "egg", t = "add"
+输出:true
+
+ +

示例 2:

+ +
+输入:s = "foo", t = "bar"
+输出:false
+ +

示例 3:

+ +
+输入:s = "paper", t = "title"
+输出:true
+ +

 

+ +

提示:

+ +
    +
  • 可以假设 t 长度相同。
  • +
+ + +## template + +```java +class Solution { + public boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) { + return false; + } + Map somorphicMap = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + char key = s.charAt(i); + char value = t.charAt(i); + if (somorphicMap.get(key) != null) { + if (somorphicMap.get(key) != value) { + return false; + } + } else { + if (somorphicMap.containsValue(value)) { + return false; + } + somorphicMap.put(s.charAt(i), t.charAt(i)); + } + } + return true; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3048543f7ee52148163b1b6dc00f5fb3170dce37 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-88b30ea455b74aa386e4cd43616a5363", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e61ec27ccedf021d451692d9ed7a1be83a1be083 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "60a7ec12746543db858002c722581fd4", + "author": "csdn.net", + "keywords": "递归,链表" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a3dd03ecd035c389f445a52e54141ef0356668ae --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/206.exercises/solution.md" @@ -0,0 +1,99 @@ +# 反转链表 + +给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 +
+
+

 

+ +

示例 1:

+ +
+输入:head = [1,2,3,4,5]
+输出:[5,4,3,2,1]
+
+ +

示例 2:

+ +
+输入:head = [1,2]
+输出:[2,1]
+
+ +

示例 3:

+ +
+输入:head = []
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • 链表中节点的数目范围是 [0, 5000]
  • +
  • -5000 <= Node.val <= 5000
  • +
+ +

 

+ +

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

+
+
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public ListNode reverseList(ListNode head) { + ListNode pre = null; + while (head != null) { + + ListNode tmp = head.next; + + head.next = pre; + + pre = head; + + head = tmp; + } + return pre; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a00da2eca64310b1900ea5953e0fa0c2d61325af --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-0a04ec01ec274ee2807d5c61e9898068", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..517c425cdf308acb0109602db70b712ee57dd2f0 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "f54e05259acc4aff898ab42b96635ec3", + "author": "csdn.net", + "keywords": "数组,哈希表,排序" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c8f0ac610a9b2cca3734a3051f53db1082054d15 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/217.exercises/solution.md" @@ -0,0 +1,70 @@ +# 存在重复元素 + +

给定一个整数数组,判断是否存在重复元素。

+ +

如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false

+ +

 

+ +

示例 1:

+ +
+输入: [1,2,3,1]
+输出: true
+ +

示例 2:

+ +
+输入: [1,2,3,4]
+输出: false
+ +

示例 3:

+ +
+输入: [1,1,1,3,3,4,3,2,4,2]
+输出: true
+ + +## template + +```java +class Solution { + public boolean containsDuplicate(int[] nums) { + Map map = new HashMap(); + + for (int x : nums) { + if (map.containsKey(x)) + return true; + map.put(x, x); + } + return false; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9f6561722656369633fffca7e6ae1d9b97ec3a6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2e2b96cfe5c84efaa0aeb1c39db4930c", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2b7a2eead086e174aa4450a0389d4ce970a39f1c --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "141a6df1ad1340e3a97345ab5dff0b0b", + "author": "csdn.net", + "keywords": "数组,哈希表,滑动窗口" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0cbe36bb7167773f000617d1a54463029386d1e4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/219.exercises/solution.md" @@ -0,0 +1,76 @@ +# 存在重复元素 II + +

给定一个整数数组和一个整数 k,判断数组中是否存在两个不同的索引 i 和 j,使得 nums [i] = nums [j],并且 ij 的差的 绝对值 至多为 k

+ +

 

+ +

示例 1:

+ +
输入: nums = [1,2,3,1], k = 3
+输出: true
+ +

示例 2:

+ +
输入: nums = [1,0,1,1], k = 1
+输出: true
+ +

示例 3:

+ +
输入: nums = [1,2,3,1,2,3], k = 2
+输出: false
+ + +## template + +```java +public class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + int left = 0; + int right = -1; + HashMap hashMap = new HashMap<>(); + while (left < nums.length) { + if (right + 1 < nums.length && right - left < k) { + right++; + if (hashMap.containsKey(nums[right])) { + return true; + } else { + hashMap.put(nums[right], 1); + } + } else { + hashMap.put(nums[left], hashMap.get(nums[left]) - 1); + if (hashMap.get(nums[left]) == 0) { + hashMap.remove(nums[left]); + } + left++; + } + } + return false; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f46d3f6809a40116c83630845d170412481bfffe --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-7a8b56100ec947a9994098bde915dda2", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3cbad229a8279dad4add6833c47b8ad5020b5236 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "ca3fc13371004d43aa49fbf17207c9dd", + "author": "csdn.net", + "keywords": "栈,设计,队列" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..02768f37366bae9af01cde4ccbace967c43fdf49 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/225.exercises/solution.md" @@ -0,0 +1,131 @@ +# 用队列实现栈 + +

请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(pushtoppopempty)。

+ +

实现 MyStack 类:

+ +
    +
  • void push(int x) 将元素 x 压入栈顶。
  • +
  • int pop() 移除并返回栈顶元素。
  • +
  • int top() 返回栈顶元素。
  • +
  • boolean empty() 如果栈是空的,返回 true ;否则,返回 false
  • +
+ +

 

+ +

注意:

+ +
    +
  • 你只能使用队列的基本操作 —— 也就是 push to backpeek/pop from frontsize 和 is empty 这些操作。
  • +
  • 你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • +
+ +

 

+ +

示例:

+ +
+输入:
+["MyStack", "push", "push", "top", "pop", "empty"]
+[[], [1], [2], [], [], []]
+输出:
+[null, null, null, 2, 2, false]
+
+解释:
+MyStack myStack = new MyStack();
+myStack.push(1);
+myStack.push(2);
+myStack.top(); // 返回 2
+myStack.pop(); // 返回 2
+myStack.empty(); // 返回 False
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= x <= 9
  • +
  • 最多调用100pushpoptopempty
  • +
  • 每次调用 poptop 都保证栈不为空
  • +
+ +

 

+ +

进阶:你能否实现每种操作的均摊时间复杂度为 O(1) 的栈?换句话说,执行 n 个操作的总时间复杂度 O(n) ,尽管其中某个操作可能需要比其他操作更长的时间。你可以使用两个以上的队列。

+ + +## template + +```java +class MyStack { + + private Queue queue; + + /** Initialize your data structure here. */ + public MyStack() { + queue = new LinkedList<>(); + + } + + /** + * 在添加数据的时候进行翻转 + */ + /** Push element x onto stack. */ + public void push(int x) { + Queue temp = new LinkedList<>(); + + while (!queue.isEmpty()) { + + temp.add(queue.poll()); + } + + queue.add(x); + + while (!temp.isEmpty()) { + queue.add(temp.poll()); + } + } + + /** Removes the element on top of the stack and returns that element. */ + public int pop() { + return queue.poll(); + } + + /** Get the top element. */ + public int top() { + return queue.peek(); + } + + /** Returns whether the stack is empty. */ + public boolean empty() { + return queue.isEmpty(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c0a2d7b6ba0c58776c8f336ad6620b92044588d2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-639960f477904199a831b9e77d63e9cb", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..49331fc9c37f4a3a34cbe2f0030bb7a06457006e --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "aa9c6b3d9953414b90d6af680d782643", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5018d0699ec3ab89fa5253c474a66c559459741a --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/226.exercises/solution.md" @@ -0,0 +1,93 @@ +# 翻转二叉树 + +

翻转一棵二叉树。

+ +

示例:

+ +

输入:

+ +
     4
+   /   \
+  2     7
+ / \   / \
+1   3 6   9
+ +

输出:

+ +
     4
+   /   \
+  7     2
+ / \   / \
+9   6 3   1
+ +

备注:
+这个问题是受到 Max Howell 原问题 启发的 :

+ +
谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + + +class Solution { + + public TreeNode invertTree(TreeNode root) { + + if (root == null) + return null; + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + TreeNode rightTree = node.right; + node.right = node.left; + node.left = rightTree; + if (node.left != null) { + queue.offer(node.left); + } + if (node.right != null) { + queue.offer(node.right); + } + } + return root; + } + +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8b4a8615cdfcaf337a034318c72e21f564362eba --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-81e0284ea0144410823aac209004514d", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.json" similarity index 65% rename from "data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.json" rename to "data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.json" index fff4a3f8d022bfe4a14034f8ca667430e4457250..7ccee980e3cb2fa918d89420e22f7275d98ee67f 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.json" +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "288e98a516fc41c3865670c261f06f02", + "exercise_id": "6b017afad3d94b10adabd5623ab8c0b2", "author": "csdn.net", "keywords": "数组" } \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..cac3170d2be979f090b6f257c295b0599c8cacfa --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/228.exercises/solution.md" @@ -0,0 +1,127 @@ +# 汇总区间 + +

给定一个无重复元素的有序整数数组 nums

+ +

返回 恰好覆盖数组中所有数字最小有序 区间范围列表。也就是说,nums 的每个元素都恰好被某个区间范围所覆盖,并且不存在属于某个范围但不属于 nums 的数字 x

+ +

列表中的每个区间范围 [a,b] 应该按如下格式输出:

+ +
    +
  • "a->b" ,如果 a != b
  • +
  • "a" ,如果 a == b
  • +
+ +

 

+ +

示例 1:

+ +
+输入:nums = [0,1,2,4,5,7]
+输出:["0->2","4->5","7"]
+解释:区间范围是:
+[0,2] --> "0->2"
+[4,5] --> "4->5"
+[7,7] --> "7"
+
+ +

示例 2:

+ +
+输入:nums = [0,2,3,4,6,8,9]
+输出:["0","2->4","6","8->9"]
+解释:区间范围是:
+[0,0] --> "0"
+[2,4] --> "2->4"
+[6,6] --> "6"
+[8,9] --> "8->9"
+
+ +

示例 3:

+ +
+输入:nums = []
+输出:[]
+
+ +

示例 4:

+ +
+输入:nums = [-1]
+输出:["-1"]
+
+ +

示例 5:

+ +
+输入:nums = [0]
+输出:["0"]
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 20
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • nums 中的所有值都 互不相同
  • +
  • nums 按升序排列
  • +
+ + +## template + +```java + +class Solution { + public List summaryRanges(int[] nums) { + List list = new ArrayList<>(); + int pre = 0; + int next = 0; + for (int i = 0; i < nums.length; i++) { + if (i + 1 < nums.length && nums[i + 1] - nums[i] == 1) { + next = i + 1; + } else { + if (next < i) + next = i; + if (pre != next) { + list.add(nums[pre] + "->" + nums[next]); + pre = i + 1; + } + if (pre == next) { + list.add(nums[pre] + ""); + pre = i + 1; + } + } + } + return list; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..fdfd49007f99675711d292d8d76bf58d38a2d10b --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2018213882f045499ab5816da6ace07b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d1c7e5c44e40703c30e276d584d64b594ce8bd67 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "5995f80cc5334d03b2909c0cf87e6483", + "author": "csdn.net", + "keywords": "位运算,递归,数学" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f6e36dcc6772d5ecc3bc8c8de9076600564e4a70 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/231.exercises/solution.md" @@ -0,0 +1,105 @@ +# 2 的幂 + +

给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false

+ +

如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。

+ +

 

+ +

示例 1:

+ +
+输入:n = 1
+输出:true
+解释:20 = 1
+
+ +

示例 2:

+ +
+输入:n = 16
+输出:true
+解释:24 = 16
+
+ +

示例 3:

+ +
+输入:n = 3
+输出:false
+
+ +

示例 4:

+ +
+输入:n = 4
+输出:true
+
+ +

示例 5:

+ +
+输入:n = 5
+输出:false
+
+ +

 

+ +

提示:

+ +
    +
  • -231 <= n <= 231 - 1
  • +
+ +

 

+ +

进阶:你能够不使用循环/递归解决此问题吗?

+ + +## template + +```java +class Solution { + public boolean isPowerOfTwo(int n) { + if (n <= 0) + return false; + return countBit(n) == 1; + } + + public int countBit(int num) { + int count = 0; + while (num != 0) { + count += (num & 1); + num >>= 1; + } + return count; + + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..67ad65529dd5b443cc4fb59e7be300b92170836d --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-cba24ee1b0204e13b21c169db5661dfc", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..2a702ee81dfd90679b2e0ce7b30cb6bda19c9219 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d0c31ea817924ac0b91f30e846b3e1ad", + "author": "csdn.net", + "keywords": "栈,设计,队列" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1acfb954062e6cd604200da8ef393f478158bef8 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/232.exercises/solution.md" @@ -0,0 +1,140 @@ +# 用栈实现队列 + +

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(pushpoppeekempty):

+ +

实现 MyQueue 类:

+ +
    +
  • void push(int x) 将元素 x 推到队列的末尾
  • +
  • int pop() 从队列的开头移除并返回元素
  • +
  • int peek() 返回队列开头的元素
  • +
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false
  • +
+ +

 

+ +

说明:

+ +
    +
  • 你只能使用标准的栈操作 —— 也就是只有 push to toppeek/pop from topsize, 和 is empty 操作是合法的。
  • +
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • +
+ +

 

+ +

进阶:

+ +
    +
  • 你能否实现每个操作均摊时间复杂度为 O(1) 的队列?换句话说,执行 n 个操作的总时间复杂度为 O(n) ,即使其中一个操作可能花费较长时间。
  • +
+ +

 

+ +

示例:

+ +
+输入:
+["MyQueue", "push", "push", "peek", "pop", "empty"]
+[[], [1], [2], [], [], []]
+输出:
+[null, null, null, 1, 1, false]
+
+解释:
+MyQueue myQueue = new MyQueue();
+myQueue.push(1); // queue is: [1]
+myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
+myQueue.peek(); // return 1
+myQueue.pop(); // return 1, queue is [2]
+myQueue.empty(); // return false
+
+ +
    +
+ +

 

+ +

提示:

+ +
    +
  • 1 <= x <= 9
  • +
  • 最多调用 100pushpoppeekempty
  • +
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)
  • +
+ + +## template + +```java +class MyQueue { + Stack s1; + Stack s2; + + /** Initialize your data structure here. */ + public MyQueue() { + s1 = new Stack(); + s2 = new Stack(); + } + + /** Push element x to the back of queue. */ + public void push(int x) { + while (!s1.empty()) + s2.push(s1.pop()); + s1.push(x); + while (!s2.empty()) + s1.push(s2.pop()); + return; + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + return s1.pop(); + } + + /** Get the front element. */ + public int peek() { + int ret = s1.pop(); + s1.push(ret); + return ret; + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return s1.empty(); + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cf93a5372e4c047b6833356047960a16efb85900 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-1e57611d292041248ccf8cd0e980fb60", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c912b26d11adaa2fa83424101e93b350760e50f6 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "1ab6992ae398412384eaf71d75649036", + "author": "csdn.net", + "keywords": "栈,递归,链表,双指针" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..07965ad56314eb852036ede7bd78ada4496d5376 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/234.exercises/solution.md" @@ -0,0 +1,109 @@ +# 回文链表 + +

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false

+ +

 

+ +

示例 1:

+ +
+输入:head = [1,2,2,1]
+输出:true
+
+ +

示例 2:

+ +
+输入:head = [1,2]
+输出:false
+
+ +

 

+ +

提示:

+ +
    +
  • 链表中节点数目在范围[1, 105]
  • +
  • 0 <= Node.val <= 9
  • +
+ +

 

+ +

进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?

+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } + +} + +class Solution { + public boolean isPalindrome(ListNode head) { + + if (head == null || head.next == null) { + return true; + } + + ListNode slow = head; + ListNode quick = head; + + while (quick != null && quick.next != null) { + quick = quick.next.next; + slow = slow.next; + } + ListNode pre = null; + ListNode p = slow; + + while (p != null) { + ListNode temp = p.next; + p.next = pre; + pre = p; + p = temp; + } + + while (pre != null) { + if (pre.val == head.val) { + pre = pre.next; + head = head.next; + } else { + return false; + } + } + return true; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/config.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..25e774e58c3ab3f66dd5cea8ea0e8ca66b247dc4 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-81b41e5e728344faab09c3337e91e4c9", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.json" "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..265cb18a9eb2493842891c5cb41fd4baee71b3d2 --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "96e11cec69864ccc9304320780b26f50", + "author": "csdn.net", + "keywords": "树,深度优先搜索,二叉搜索树,二叉树" +} \ No newline at end of file diff --git "a/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.md" "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9be2cb38cc4a643df42b04403c5e61ff5e79c2fe --- /dev/null +++ "b/data/1.dailycode\345\210\235\351\230\266/2.java/235.exercises/solution.md" @@ -0,0 +1,88 @@ +# 二叉搜索树的最近公共祖先 + +

给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。

+ +

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

+ +

例如,给定如下二叉搜索树:  root = [6,2,8,0,4,7,9,null,null,3,5]

+ +

+ +

 

+ +

示例 1:

+ +
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
+输出: 6 
+解释: 节点 2 和节点 8 的最近公共祖先是 6。
+
+ +

示例 2:

+ +
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
+输出: 2
+解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
+ +

 

+ +

说明:

+ +
    +
  • 所有节点的值都是唯一的。
  • +
  • p、q 为不同节点且均存在于给定的二叉搜索树中。
  • +
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + + if (p.val < root.val && q.val < root.val) + return lowestCommonAncestor(root.left, p, q); + else if (p.val > root.val && q.val > root.val) + return lowestCommonAncestor(root.right, p, q); + else + return root; + } + +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2302786d4143e3c91b6e46740058f3910951de51 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-a2ccc65d66f7450e8405eefda34c259d", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4f6de0e30d172b5a7ffc79553d552bb8863fc1c6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "682f41a955f04da9bb0feb3d063f8b3e", + "author": "csdn.net", + "keywords": "树,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..377fafadce6a9f292642329990ec6ee0725c1ba9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/102.exercises/solution.md" @@ -0,0 +1,95 @@ +# 二叉树的层序遍历 + +

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。

+ +

 

+ +

示例:
+二叉树:[3,9,20,null,null,15,7],

+ +
+    3
+   / \
+  9  20
+    /  \
+   15   7
+
+ +

返回其层序遍历结果:

+ +
+[
+  [3],
+  [9,20],
+  [15,7]
+]
+
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> levelOrder(TreeNode root) { + List> l = new ArrayList<>(); + Queue q = new LinkedList(); + if (root != null) { + q.add(root); + } + while (!q.isEmpty()) { + List l2 = new ArrayList<>(); + int number = q.size(); + while (number > 0) { + TreeNode t = q.poll(); + l2.add(t.val); + if (t.left != null) { + q.add(t.left); + } + if (t.right != null) { + q.add(t.right); + } + number--; + } + l.add(l2); + } + return l; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d461e7e5fc43d9d010414fba7a8e82f4991ab636 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-a10ff4a447fb49fca996a9c4c95b4991", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..80898dfcdc92c53cb4e152763ed7bf2bc6f29943 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "622d6183d44744c68b49f96bb9c87452", + "author": "csdn.net", + "keywords": "树,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ae68b131b350321d2495f1e68cab40caacec7e7a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/103.exercises/solution.md" @@ -0,0 +1,113 @@ +# 二叉树的锯齿形层序遍历 + +

给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

+ +

例如:
+给定二叉树 [3,9,20,null,null,15,7],

+ +
+    3
+   / \
+  9  20
+    /  \
+   15   7
+
+ +

返回锯齿形层序遍历如下:

+ +
+[
+  [3],
+  [20,9],
+  [15,7]
+]
+
+ + +## template + +```java + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> zigzagLevelOrder(TreeNode root) { + List> list = new LinkedList<>(); + if (root == null) { + return list; + } + Stack stack1 = new Stack<>(); + stack1.push(root); + + boolean postive = true; + + while (!stack1.isEmpty()) { + Stack stack2 = new Stack<>(); + List subList = new LinkedList<>(); + while (!stack1.isEmpty()) { + TreeNode current = stack1.pop(); + subList.add(current.val); + if (postive) { + if (current.left != null) { + stack2.push(current.left); + } + if (current.right != null) { + stack2.push(current.right); + } + } else { + if (current.right != null) { + stack2.push(current.right); + } + if (current.left != null) { + stack2.push(current.left); + } + } + + } + postive = !postive; + + stack1 = stack2; + list.add(subList); + } + + return list; + + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5c1011582651b2a11892fd03836ef751d0bf483c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-86fd35376fd74dbdb95ce149ea10f1a3", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1c98c2b08888ca4d9c2afa697a56090cf9a08cdd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "8fa4959ae71f49b8bc134a8a20849f36", + "author": "csdn.net", + "keywords": "树,数组,哈希表,分治,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..4b44f326fce12330006c7cfdfc4d2425c78b6ff8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/105.exercises/solution.md" @@ -0,0 +1,112 @@ +# 从前序与中序遍历序列构造二叉树 + +

给定一棵树的前序遍历 preorder 与中序遍历  inorder。请构造二叉树并返回其根节点。

+ +

 

+ +

示例 1:

+ +
+Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
+Output: [3,9,20,null,null,15,7]
+
+ +

示例 2:

+ +
+Input: preorder = [-1], inorder = [-1]
+Output: [-1]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= preorder.length <= 3000
  • +
  • inorder.length == preorder.length
  • +
  • -3000 <= preorder[i], inorder[i] <= 3000
  • +
  • preorder 和 inorder 均无重复元素
  • +
  • inorder 均出现在 preorder
  • +
  • preorder 保证为二叉树的前序遍历序列
  • +
  • inorder 保证为二叉树的中序遍历序列
  • +
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +public class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + if (preorder.length != inorder.length) + return null; + if (preorder.length == 0) + return null; + if (preorder.length == 1) + return new TreeNode(preorder[0]); + return buildTree(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); + } + + private TreeNode buildTree(int[] preorder, int[] inorder, int prei, int prej, int ini, int inj) { + if (prei > prej || ini > inj || prei < 0 || prej >= preorder.length || ini < 0 || inj >= inorder.length) + return null; + if (prej - prei < 0) + return null; + if (prei == prej) + return new TreeNode(preorder[prei]); + TreeNode root = new TreeNode(preorder[prei]); + int inFlag = 0; + for (int i = ini; i <= inj; i++) { + if (inorder[i] == root.val) { + inFlag = i; + break; + } + } + int num_left = inFlag - ini; + int num_right = inj - inFlag; + root.left = buildTree(preorder, inorder, prei + 1, prei + num_left, ini, inFlag - 1); + root.right = buildTree(preorder, inorder, prej - num_right + 1, prej, inFlag + 1, inj); + + return root; + + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..344237072792d14145e9f20da84a121d65575a13 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-9cf2f60a9b974c86bf61a0412336ca0f", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0bf20d41e40e5946820904ed3086b91aadea4430 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "12c18413a51d4dc0aaff19498b606d12", + "author": "csdn.net", + "keywords": "树,数组,哈希表,分治,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..37636406ce32941e660370bdc34ededff609e111 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/106.exercises/solution.md" @@ -0,0 +1,89 @@ +# 从中序与后序遍历序列构造二叉树 + +

根据一棵树的中序遍历与后序遍历构造二叉树。

+ +

注意:
+你可以假设树中没有重复的元素。

+ +

例如,给出

+ +
中序遍历 inorder = [9,3,15,20,7]
+后序遍历 postorder = [9,15,7,20,3]
+ +

返回如下的二叉树:

+ +
    3
+   / \
+  9  20
+    /  \
+   15   7
+
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public TreeNode buildTree(int[] inorder, int[] postorder) { + return helper(inorder, postorder, postorder.length - 1, 0, inorder.length - 1); + } + + public TreeNode helper(int[] inorder, int[] postorder, int postEnd, int inStart, int inEnd) { + if (inStart > inEnd) { + return null; + } + + int currentVal = postorder[postEnd]; + TreeNode current = new TreeNode(currentVal); + + int inIndex = 0; + for (int i = inStart; i <= inEnd; i++) { + if (inorder[i] == currentVal) { + inIndex = i; + } + } + TreeNode left = helper(inorder, postorder, postEnd - (inEnd - inIndex) - 1, inStart, inIndex - 1); + TreeNode right = helper(inorder, postorder, postEnd - 1, inIndex + 1, inEnd); + current.left = left; + current.right = right; + return current; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ad5090a0ced51d0dcb65a5fc31d739edc024018c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-7bc90c68c2e040abaf544ce5034b98be", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0fd5e27564e3f6aae1e7cd395ca8d6acfba2dd27 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "c243d27f11ae40178101f3a631183c6e", + "author": "csdn.net", + "keywords": "树,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9177280110d7d9bde39ab26e3482e60f61c6f417 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/107.exercises/solution.md" @@ -0,0 +1,92 @@ +# 二叉树的层序遍历 II + +

给定一个二叉树,返回其节点值自底向上的层序遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

+ +

例如:
+给定二叉树 [3,9,20,null,null,15,7],

+ +
+    3
+   / \
+  9  20
+    /  \
+   15   7
+
+ +

返回其自底向上的层序遍历为:

+ +
+[
+  [15,7],
+  [9,20],
+  [3]
+]
+
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> levelOrderBottom(TreeNode root) { + List> list1 = new ArrayList<>(); + if (root == null) + return list1; + + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + List list2 = new ArrayList<>(); + int count = queue.size(); + for (int i = 0; i < count; i++) { + TreeNode node = queue.poll(); + list2.add(node.val); + if (node.left != null) + queue.add(node.left); + if (node.right != null) + queue.add(node.right); + } + list1.add(0, list2); + } + return list1; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2ef87a32c417d4b78a3267ce8639d1bfb1699c91 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-fda1069c25854dcf91f2ec44d9a5447c", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..74262cf3fc997c6926e24e1f6ddb492dcf8135da --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "4f2c0c8d00ef4f23ae5d13ea794ea680", + "author": "csdn.net", + "keywords": "树,二叉搜索树,链表,分治,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e68848dac3a9085f69ef76f20aea199f4b8a4df2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/109.exercises/solution.md" @@ -0,0 +1,117 @@ +# 有序链表转换二叉搜索树 + +

给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。

+ +

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

+ +

示例:

+ +
给定的有序链表: [-10, -3, 0, 5, 9],
+
+一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:
+
+      0
+     / \
+   -3   9
+   /   /
+ -10  5
+
+ + +## template + +```java + +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; + } +} + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public TreeNode sortedListToBST(ListNode head) { + if (head == null) + return null; + return helper(head, null); + } + + private TreeNode helper(ListNode start, ListNode end) { + if (start == end) + return null; + + ListNode slow = start; + ListNode fast = start; + + while (fast != end && fast.next != end) { + + slow = slow.next; + + fast = fast.next.next; + } + + TreeNode root = new TreeNode(slow.val); + + root.left = helper(start, slow); + + root.right = helper(slow.next, end); + return root; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a4d23201ae4435d2e07a319f69faec6491a699b5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-7d17892d955e4c4199329919b6f5f2fa", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a42d367c507390d65063ad0e9e06927369b4f623 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "b7b39f6a4e2c422e9ea675fdb15c7443", + "author": "csdn.net", + "keywords": "树,深度优先搜索,回溯,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f6ae8e888a02f8cec21b94d75b34274913d8f54b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/113.exercises/solution.md" @@ -0,0 +1,101 @@ +# 路径总和 II + +

给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

+ +

叶子节点 是指没有子节点的节点。

+ +
+
+

 

+ +

示例 1:

+ +
+输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
+输出:[[5,4,11,2],[5,8,4,5]]
+
+ +

示例 2:

+ +
+输入:root = [1,2,3], targetSum = 5
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1,2], targetSum = 0
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点总数在范围 [0, 5000]
  • +
  • -1000 <= Node.val <= 1000
  • +
  • -1000 <= targetSum <= 1000
  • +
+
+
+ + +## template + +```java +class Solution { + public List> pathSum(TreeNode root, int sum) { + if (root == null) + return new ArrayList<>(); + List> ans = new ArrayList<>(); + if (root.val == sum && root.left == null && root.right == null) { + List arr = new ArrayList<>(); + arr.add(root.val); + ans.add(arr); + return ans; + } + List> left = pathSum(root.left, sum - root.val); + List> right = pathSum(root.right, sum - root.val); + for (List list : left) { + + list.add(0, root.val); + ans.add(list); + } + for (List list : right) { + list.add(0, root.val); + ans.add(list); + } + return ans; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c789dcbb60384dbd359d1dc7a19a11a9d896fff2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-10270be411614245ba9f185a69eab18b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..907c6ab52d96eb48503fdb5ca2736761a52565a8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e45a86e778a94466bf15522cb8b48d6d", + "author": "csdn.net", + "keywords": "栈,树,深度优先搜索,链表,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f97441f2119876d32710b6810587965c8ae9f71f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/114.exercises/solution.md" @@ -0,0 +1,103 @@ +# 二叉树展开为链表 + +

给你二叉树的根结点 root ,请你将它展开为一个单链表:

+ +
    +
  • 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null
  • +
  • 展开后的单链表应该与二叉树 先序遍历 顺序相同。
  • +
+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,5,3,4,null,6]
+输出:[1,null,2,null,3,null,4,null,5,null,6]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [0]
+输出:[0]
+
+ +

 

+ +

提示:

+ +
    +
  • 树中结点数在范围 [0, 2000]
  • +
  • -100 <= Node.val <= 100
  • +
+ +

 

+ +

进阶:你可以使用原地算法(O(1) 额外空间)展开这棵树吗?

+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public void flatten(TreeNode root) { + while (root != null) { + if (root.left == null) + root = root.right; + else { + TreeNode pre = root.left; + while (pre.right != null) + pre = pre.right; + pre.right = root.right; + root.right = root.left; + root.left = null; + root = root.right; + } + } + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..97059d062ab4dd60740949c9583ccc01ec1efc4a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-56528700cf0d47b194088d0962257d31", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..07293e55d18d0fecc838c608850110f317b675f4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "6f1eb2d1c9aa4dc6a97e7c8076573729", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8f0423e45fe2c459787b108002e36f10f805adba --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/116.exercises/solution.md" @@ -0,0 +1,112 @@ +# 填充每个节点的下一个右侧节点指针 + +

给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

+ +
+struct Node {
+  int val;
+  Node *left;
+  Node *right;
+  Node *next;
+}
+ +

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

+ +

初始状态下,所有 next 指针都被设置为 NULL

+ +

 

+ +

进阶:

+ +
    +
  • 你只能使用常量级额外空间。
  • +
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
  • +
+ +

 

+ +

示例:

+ +

+ +
+输入:root = [1,2,3,4,5,6,7]
+输出:[1,#,2,3,#,4,5,6,7,#]
+解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化的输出按层序遍历排列,同一层节点由 next 指针连接,'#' 标志着每一层的结束。
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点的数量少于 4096
  • +
  • -1000 <= node.val <= 1000
  • +
+ + +## template + +```java +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node connect(Node root) { + if (root == null) + return root; + if (root.left != null) + root.left.next = root.right; + if (root.next != null && root.right != null) { + root.right.next = root.next.left; + } + connect(root.left); + connect(root.right); + return root; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6179799daf1734216fc410ca80f9387b6a354678 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-05b0757a4c1946b5add1f0050cdd4d24", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e6cec3dc8466b4dd9e041f82c535441f29da2158 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "07b8ec4db87d47f6a9e1d8c0748271bb", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ae2d602a85fa8379a20af2eb32e445d4183b73e0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/117.exercises/solution.md" @@ -0,0 +1,137 @@ +# 填充每个节点的下一个右侧节点指针 II + +

给定一个二叉树

+ +
+struct Node {
+  int val;
+  Node *left;
+  Node *right;
+  Node *next;
+}
+ +

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

+ +

初始状态下,所有 next 指针都被设置为 NULL

+ +

 

+ +

进阶:

+ +
    +
  • 你只能使用常量级额外空间。
  • +
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
  • +
+ +

 

+ +

示例:

+ +

+ +
+输入:root = [1,2,3,4,5,null,7]
+输出:[1,#,2,3,#,4,5,7,#]
+解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。
+ +

 

+ +

提示:

+ +
    +
  • 树中的节点数小于 6000
  • +
  • -100 <= node.val <= 100
  • +
+ +

 

+ +
    +
+ + +## template + +```java +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node connect(Node root) { + if (root == null || (root.left == null && root.right == null)) { + return root; + } + if (root.left != null && root.right != null) { + root.left.next = root.right; + root.next = getrightnext(root); + } + if (root.left != null) { + root.left.next = getrightnext(root); + } + if (root.right != null) { + root.right.next = getrightnext(root); + } + connect(root.right); + connect(root.left); + return root; + + } + + public static Node getrightnext(Node root) { + while (root.next != null) { + if (root.left != null) { + return root.left; + } + if (root.right != null) { + return root.right; + } + root = root.next; + } + return null; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7f3826afbe5ce965b7f48e74fadc3d12c5529f43 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2e52e1b28e9f4a199d310a38005794e8", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..fb6ecdea2e5afa8a4d3fe3c2d70d8eed8b39d345 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "636abfea1a2b4607859af3c78cb96e84", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e4b5cc0530cc587a5725d4a1afe68264b89b4129 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/120.exercises/solution.md" @@ -0,0 +1,106 @@ +# 三角形最小路径和 + +

给定一个三角形 triangle ,找出自顶向下的最小路径和。

+ +

每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 ii + 1

+ +

 

+ +

示例 1:

+ +
+输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
+输出:11
+解释:如下面简图所示:
+   2
+  3 4
+ 6 5 7
+4 1 8 3
+自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
+
+ +

示例 2:

+ +
+输入:triangle = [[-10]]
+输出:-10
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= triangle.length <= 200
  • +
  • triangle[0].length == 1
  • +
  • triangle[i].length == triangle[i - 1].length + 1
  • +
  • -104 <= triangle[i][j] <= 104
  • +
+ +

 

+ +

进阶:

+ +
    +
  • 你可以只使用 O(n) 的额外空间(n 为三角形的总行数)来解决这个问题吗?
  • +
+ + +## template + +```java +class Solution { + public int minimumTotal(List> triangle) { + int[] dp = new int[triangle.size()]; + int ans = 0; + dp[0] = triangle.get(0).get(0); + int temp, prev = 0, cur; + for (int i = 1; i < triangle.size(); i++) { + for (int j = 0; j < triangle.get(i).size(); j++) { + temp = triangle.get(i).get(j); + cur = dp[j]; + if (j == 0) + dp[j] = cur + temp; + else if (j == triangle.get(i).size() - 1) + dp[j] = prev + temp; + else + dp[j] = Math.min(prev, cur) + temp; + prev = cur; + } + } + + for (int i = 1; i < dp.length; i++) { + if (dp[ans] > dp[i]) + ans = i; + } + return dp[ans]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9644d38266609996805b1e2c4ca931bbd786132 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-5eeb1630d37b40339d3ae6808d1b0d24", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6c43e906ca40ae3f4313b7a6d8a69f6bc04f3439 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "17afc370460f46c0adbaab615712518f", + "author": "csdn.net", + "keywords": "并查集,数组,哈希表" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..167477f2b98e4b4c61d23148f9def83b9f5fdd7a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/128.exercises/solution.md" @@ -0,0 +1,90 @@ +# 最长连续序列 + +

给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。

+ +

请你设计并实现时间复杂度为 O(n) 的算法解决此问题。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [100,4,200,1,3,2]
+输出:4
+解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
+ +

示例 2:

+ +
+输入:nums = [0,3,7,2,5,8,4,6,0,1]
+输出:9
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= nums.length <= 105
  • +
  • -109 <= nums[i] <= 109
  • +
+ + +## template + +```java +class Solution { + public int longestConsecutive(int[] nums) { + Set num_set = new HashSet(); + for (int num : nums) { + num_set.add(num); + } + + int longestStreak = 0; + + for (int num : num_set) { + if (!num_set.contains(num - 1)) { + int currentNum = num; + int currentStreak = 1; + + while (num_set.contains(currentNum + 1)) { + currentNum += 1; + currentStreak += 1; + } + + longestStreak = Math.max(longestStreak, currentStreak); + } + } + + return longestStreak; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9ec61dbfb4c9182925b580de44538f6055068b65 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-56e4d8032c32409fae1d148bf85536b3", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..43403977dcd24d422dbd8a56b92db53163e89d15 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "4ae056aaec24487aa6c7c5664dcd9106", + "author": "csdn.net", + "keywords": "树,深度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c65cee1f9ebf0ecee59e48a90af8489ecfd75f9a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/129.exercises/solution.md" @@ -0,0 +1,130 @@ +# 求根节点到叶节点数字之和 + +给你一个二叉树的根节点 root ,树中每个节点都存放有一个 09 之间的数字。 +
+
+

每条从根节点到叶节点的路径都代表一个数字:

+ +
    +
  • 例如,从根节点到叶节点的路径 1 -> 2 -> 3 表示数字 123
  • +
+ +

计算从根节点到叶节点生成的 所有数字之和

+ +

叶节点 是指没有子节点的节点。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,3]
+输出:25
+解释:
+从根到叶子节点路径 1->2 代表数字 12
+从根到叶子节点路径 1->3 代表数字 13
+因此,数字总和 = 12 + 13 = 25
+ +

示例 2:

+ +
+输入:root = [4,9,0,5,1]
+输出:1026
+解释:
+从根到叶子节点路径 4->9->5 代表数字 495
+从根到叶子节点路径 4->9->1 代表数字 491
+从根到叶子节点路径 4->0 代表数字 40
+因此,数字总和 = 495 + 491 + 40 = 1026
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点的数目在范围 [1, 1000]
  • +
  • 0 <= Node.val <= 9
  • +
  • 树的深度不超过 10
  • +
+
+
+ + +## template + +```java + +public class Solution { + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } + + StringBuffer tempPath = new StringBuffer(); + int sum = 0; + + public void dfs(TreeNode root) { + char c = (char) (root.val + '0'); + tempPath.append(c); + if (root.left == null && root.right == null) { + String s = tempPath.toString(); + int num = Integer.valueOf(s); + sum += num; + } + if (root.left != null) { + dfs(root.left); + tempPath.delete(tempPath.length() - 1, tempPath.length()); + } + if (root.right != null) { + dfs(root.right); + tempPath.delete(tempPath.length() - 1, tempPath.length()); + } + } + + public int sumNumbers(TreeNode root) { + dfs(root); + return sum; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..64b066bb67c005ad364ddc1b9f028e39bad5afc9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-fabff8ca1cb14ce0a4847b03b29ff3d2", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..80b688e0c8960bd1983a2604564cea4deefc9616 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "be34956c33dd4d54bbed87721b778ad6", + "author": "csdn.net", + "keywords": "深度优先搜索,广度优先搜索,并查集,数组,矩阵" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3efc558a4928c66d723ce22fd0b1447c9b7a959e --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/130.exercises/solution.md" @@ -0,0 +1,118 @@ +# 被围绕的区域 + +给你一个 m x n 的矩阵 board ,由若干字符 'X''O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O''X' 填充。 +
+
+

 

+ +

示例 1:

+ +
+输入:board = [["X","X","X","X"],["X","O","O","X"],["X","X","O","X"],["X","O","X","X"]]
+输出:[["X","X","X","X"],["X","X","X","X"],["X","X","X","X"],["X","O","X","X"]]
+解释:被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
+
+ +

示例 2:

+ +
+输入:board = [["X"]]
+输出:[["X"]]
+
+ +

 

+ +

提示:

+ +
    +
  • m == board.length
  • +
  • n == board[i].length
  • +
  • 1 <= m, n <= 200
  • +
  • board[i][j]'X''O'
  • +
+
+
+ + +## template + +```java + +class Solution { + int[] dx = { 1, -1, 0, 0 }; + int[] dy = { 0, 0, 1, -1 }; + + public void solve(char[][] board) { + int n = board.length; + if (n == 0) { + return; + } + int m = board[0].length; + Queue queue = new LinkedList(); + for (int i = 0; i < n; i++) { + if (board[i][0] == 'O') { + queue.offer(new int[] { i, 0 }); + } + if (board[i][m - 1] == 'O') { + queue.offer(new int[] { i, m - 1 }); + } + } + for (int i = 1; i < m - 1; i++) { + if (board[0][i] == 'O') { + queue.offer(new int[] { 0, i }); + } + if (board[n - 1][i] == 'O') { + queue.offer(new int[] { n - 1, i }); + } + } + while (!queue.isEmpty()) { + int[] cell = queue.poll(); + int x = cell[0], y = cell[1]; + board[x][y] = 'A'; + for (int i = 0; i < 4; i++) { + int mx = x + dx[i], my = y + dy[i]; + if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') { + continue; + } + queue.offer(new int[] { mx, my }); + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (board[i][j] == 'A') { + board[i][j] = 'O'; + } else if (board[i][j] == 'O') { + board[i][j] = 'X'; + } + } + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ee63205b635cbbdff1913df47bf07f043903577d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-1e93b38f039b4b4f8c8fbeda5538fcb8", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f4cb208c1cce290d08454bdca5647797d7f67388 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d946d51d15644bc6b5147e846d0a12d4", + "author": "csdn.net", + "keywords": "字符串,动态规划,回溯" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8da1848153c4415ecb1d153d767f1c005c7796f3 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/131.exercises/solution.md" @@ -0,0 +1,98 @@ +# 分割回文串 + +

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

+ +

回文串 是正着读和反着读都一样的字符串。

+ +

 

+ +

示例 1:

+ +
+输入:s = "aab"
+输出:[["a","a","b"],["aa","b"]]
+
+ +

示例 2:

+ +
+输入:s = "a"
+输出:[["a"]]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 16
  • +
  • s 仅由小写英文字母组成
  • +
+ + +## template + +```java + +class Solution { + public List> partition(String s) { + boolean[][] dp = new boolean[s.length()][s.length()]; + for (int len = 1; len <= s.length(); len++) { + for (int i = 0; i <= s.length() - len; i++) { + if (len == 1) + dp[i][i] = true; + else if (s.charAt(i) == s.charAt(i + len - 1) && (len == 2 || dp[i + 1][i + len - 2])) { + dp[i][i + len - 1] = true; + } + } + } + return solution(s, 0, dp); + } + + public List> solution(String s, int start, boolean[][] dp) { + ArrayList> list = new ArrayList<>(); + if (start == s.length()) { + List li = new ArrayList<>(); + list.add(li); + return list; + } + for (int i = start; i < s.length(); i++) { + if (dp[start][i]) { + String first = s.substring(start, i + 1); + for (List li : solution(s, i + 1, dp)) { + li.add(0, first); + list.add(li); + } + } + } + return list; + } + +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c4605d34f653a8748d6a425c7b5b0b86af934c84 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-6a47f1314c534935805a689264427699", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..abed8476019c7b01c7e97f3abbdbbd98906d0b05 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "b3d073ab49ef4e6aae7f03cefcb13959", + "author": "csdn.net", + "keywords": "深度优先搜索,广度优先搜索,图,哈希表" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b38073a29f06896f6db2a4ecfb99a04197f300d5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/133.exercises/solution.md" @@ -0,0 +1,149 @@ +# 克隆图 + +

给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。

+ +

图中的每个节点都包含它的值 valint) 和其邻居的列表(list[Node])。

+ +
class Node {
+    public int val;
+    public List<Node> neighbors;
+}
+ +

 

+ +

测试用例格式:

+ +

简单起见,每个节点的值都和它的索引相同。例如,第一个节点值为 1(val = 1),第二个节点值为 2(val = 2),以此类推。该图在测试用例中使用邻接列表表示。

+ +

邻接列表 是用于表示有限图的无序列表的集合。每个列表都描述了图中节点的邻居集。

+ +

给定节点将始终是图中的第一个节点(值为 1)。你必须将 给定节点的拷贝 作为对克隆图的引用返回。

+ +

 

+ +

示例 1:

+ +

+ +
输入:adjList = [[2,4],[1,3],[2,4],[1,3]]
+输出:[[2,4],[1,3],[2,4],[1,3]]
+解释:
+图中有 4 个节点。
+节点 1 的值是 1,它有两个邻居:节点 2 和 4 。
+节点 2 的值是 2,它有两个邻居:节点 1 和 3 。
+节点 3 的值是 3,它有两个邻居:节点 2 和 4 。
+节点 4 的值是 4,它有两个邻居:节点 1 和 3 。
+
+ +

示例 2:

+ +

+ +
输入:adjList = [[]]
+输出:[[]]
+解释:输入包含一个空列表。该图仅仅只有一个值为 1 的节点,它没有任何邻居。
+
+ +

示例 3:

+ +
输入:adjList = []
+输出:[]
+解释:这个图是空的,它不含任何节点。
+
+ +

示例 4:

+ +

+ +
输入:adjList = [[2],[1]]
+输出:[[2],[1]]
+ +

 

+ +

提示:

+ +
    +
  1. 节点数不超过 100 。
  2. +
  3. 每个节点值 Node.val 都是唯一的,1 <= Node.val <= 100
  4. +
  5. 无向图是一个简单图,这意味着图中没有重复的边,也没有自环。
  6. +
  7. 由于图是无向的,如果节点 p 是节点 q 的邻居,那么节点 q 也必须是节点 p 的邻居。
  8. +
  9. 图是连通图,你可以从给定节点访问到所有节点。
  10. +
+ + +## template + +```java + +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node cloneGraph(Node node) { + HashMap visited = new HashMap<>(); + return DFS(node, visited); + } + + public Node DFS(Node node, HashMap visited) { + if (node == null) { + return node; + } + + if (visited.containsKey(node)) { + return visited.get(node); + } + + Node clone = new Node(node.val, new ArrayList<>()); + visited.put(node, clone); + + for (Node n : node.neighbors) { + clone.neighbors.add(DFS(n, visited)); + } + return clone; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2e340a415fc8d243038252c28a293f99bc766dec --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e2c15bb17a9a4bcca4e80e3765e168b0", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..27e3a6c5b313ae5a3f4441526c950f814bca87f9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "650fb194bc8b4e1fad675188558ba5fe", + "author": "csdn.net", + "keywords": "贪心,数组" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6bd72a60b21b270187f7f501689091cdc3bba610 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/134.exercises/solution.md" @@ -0,0 +1,99 @@ +# 加油站 + +

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

+ +

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

+ +

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

+ +

说明: 

+ +
    +
  • 如果题目有解,该答案即为唯一答案。
  • +
  • 输入数组均为非空数组,且长度相同。
  • +
  • 输入数组中的元素均为非负数。
  • +
+ +

示例 1:

+ +
输入: 
+gas  = [1,2,3,4,5]
+cost = [3,4,5,1,2]
+
+输出: 3
+
+解释:
+从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
+开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
+开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
+开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
+开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
+开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
+因此,3 可为起始索引。
+ +

示例 2:

+ +
输入: 
+gas  = [2,3,4]
+cost = [3,4,3]
+
+输出: -1
+
+解释:
+你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
+我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
+开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
+开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
+你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
+因此,无论怎样,你都不可能绕环路行驶一周。
+ + +## template + +```java +class Solution { + public int canCompleteCircuit(int[] gas, int[] cost) { + int n = gas.length; + int sum = 0, cur = 0, start = 0; + for (int i = 0; i < n; i++) { + sum = sum + gas[i] - cost[i]; + if (cur < 0) { + start = i; + cur = gas[i] - cost[i]; + } else + cur = cur + gas[i] - cost[i]; + } + if (sum < 0) + return -1; + return start; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..dd5c7a6c171460bc28239899853cec5d14a910c0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e63bc430999940118df0d565da770675", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..3c77f7412c7c25e8ff838d999d42add15aeec283 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "45585dd9d6054a9cb9b039029175d81b", + "author": "csdn.net", + "keywords": "位运算,数组" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5f5a4a67f4b82074c8b9ca3be06ee8fa7878ab62 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/137.exercises/solution.md" @@ -0,0 +1,87 @@ +# 只出现一次的数字 II + +

给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [2,2,3,2]
+输出:3
+
+ +

示例 2:

+ +
+输入:nums = [0,1,0,1,0,1,99]
+输出:99
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 3 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • nums 中,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次
  • +
+ +

 

+ +

进阶:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

+ + +## template + +```java +class Solution { + public int singleNumber(int[] nums) { + + int ret = 0; + for (int i = 0; i < 32; ++i) { + int bitnums = 0; + + int bit = 1 << i; + for (int num : nums) { + + if ((num & bit) != 0) + bitnums++; + } + + if (bitnums % 3 != 0) + ret |= bit; + } + return ret; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..62ac7f1ce97980614dde0fc5c25e9948cbab11dd --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-55e51a28f29444599868b299e1d57cbe", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d8917b22c977172fbd0ac9bd3ca40ab52df22a9f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "5392710266504c69ac6bb4002bfd7407", + "author": "csdn.net", + "keywords": "哈希表,链表" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..eda011dcc6a96b6aca7c72be21ada4850de7149f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/138.exercises/solution.md" @@ -0,0 +1,138 @@ +# 复制带随机指针的链表 + +

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

+ +

构造这个链表的 深拷贝。 深拷贝应该正好由 n全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点

+ +

例如,如果原链表中有 XY 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 xy ,同样有 x.random --> y

+ +

返回复制链表的头节点。

+ +

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

+ +
    +
  • val:一个表示 Node.val 的整数。
  • +
  • random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
  • +
+ +

你的代码 接受原链表的头节点 head 作为传入参数。

+ +

 

+ +

示例 1:

+ +

+ +
+输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
+输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
+
+ +

示例 2:

+ +

+ +
+输入:head = [[1,1],[2,1]]
+输出:[[1,1],[2,1]]
+
+ +

示例 3:

+ +

+ +
+输入:head = [[3,null],[3,0],[3,null]]
+输出:[[3,null],[3,0],[3,null]]
+
+ +

示例 4:

+ +
+输入:head = []
+输出:[]
+解释:给定的链表为空(空指针),因此返回 null。
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= n <= 1000
  • +
  • -10000 <= Node.val <= 10000
  • +
  • Node.random 为空(null)或指向链表中的节点。
  • +
+ + +## template + +```java +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node copyRandomList(Node head) { + if (head == null) + return null; + Map map = new HashMap(); + Node p = head; + while (p != null) { + map.put(p, new Node(p.val)); + p = p.next; + } + p = head; + while (p != null) { + map.get(p).next = map.get(p.next); + map.get(p).random = map.get(p.random); + p = p.next; + } + return map.get(head); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..095d111f480607cd74c73e06aad11620a86bb1fb --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-c37b78b0a00a4f119f109bf0144e62f7", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4721fa158da59afb911e2fd44d57779089490eea --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "0e368bf69c094190966ba3515e36d864", + "author": "csdn.net", + "keywords": "字典树,记忆化搜索,哈希表,字符串,动态规划" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c6cca1ed668c2771a634715407a5b1262d32b31c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/139.exercises/solution.md" @@ -0,0 +1,98 @@ +# 单词拆分 + +

给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

+ +

说明:

+ +
    +
  • 拆分时可以重复使用字典中的单词。
  • +
  • 你可以假设字典中没有重复的单词。
  • +
+ +

示例 1:

+ +
输入: s = "leetcode", wordDict = ["leet", "code"]
+输出: true
+解释: 返回 true 因为 "leetcode" 可以被拆分成 "leet code"。
+
+ +

示例 2:

+ +
输入: s = "applepenapple", wordDict = ["apple", "pen"]
+输出: true
+解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。
+     注意你可以重复使用字典中的单词。
+
+ +

示例 3:

+ +
输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
+输出: false
+
+ + +## template + +```java + +public class Solution { + public boolean wordBreak(String s, List wordDict) { + int length = s.length(); + int size = wordDict.size(); + if (length == 0) { + return true; + } + if (size == 0) { + return false; + } + + boolean[] dp = new boolean[length]; + for (int i = 0; i < length; i++) { + for (int j = 0; j < size; j++) { + String now = wordDict.get(j); + if (i < now.length()) { + String test = s.substring(0, i + 1); + if (now.equals(test)) { + dp[i] = true; + break; + } + } + if (i >= now.length()) { + String test = s.substring(i - now.length() + 1, i + 1); + if (dp[i - now.length()] && test.equals(now)) { + dp[i] = true; + break; + } + } + } + } + return dp[length - 1]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..de489f72979d21eeaae04bedeca0ee0c28494115 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e756d9b2ce9148c2b2069859152653e2", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1d2a861143edce923636dcfb5aebbefc666b86f5 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e122bca4653a4a05a64157c9d3afb931", + "author": "csdn.net", + "keywords": "哈希表,链表,双指针" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..40cd6228811f66cc6996647459b65a7980ed9941 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/142.exercises/solution.md" @@ -0,0 +1,124 @@ +# 环形链表 II + +

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

+ +

为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos-1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。

+ +

说明:不允许修改给定的链表。

+ +

进阶:

+ +
    +
  • 你是否可以使用 O(1) 空间解决此题?
  • +
+ +

 

+ +

示例 1:

+ +

+ +
+输入:head = [3,2,0,-4], pos = 1
+输出:返回索引为 1 的链表节点
+解释:链表中有一个环,其尾部连接到第二个节点。
+
+ +

示例 2:

+ +

+ +
+输入:head = [1,2], pos = 0
+输出:返回索引为 0 的链表节点
+解释:链表中有一个环,其尾部连接到第一个节点。
+
+ +

示例 3:

+ +

+ +
+输入:head = [1], pos = -1
+输出:返回 null
+解释:链表中没有环。
+
+ +

 

+ +

提示:

+ +
    +
  • 链表中节点的数目范围在范围 [0, 104]
  • +
  • -105 <= Node.val <= 105
  • +
  • pos 的值为 -1 或者链表中的一个有效索引
  • +
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +public class Solution { + public ListNode detectCycle(ListNode head) { + if (head == null || head.next == null) { + return null; + } + ListNode slow = head; + ListNode fast = head; + + while (true) { + if (fast == null || fast.next == null) { + return null; + } + fast = fast.next.next; + slow = slow.next; + if (fast == slow) { + break; + } + } + slow = head; + while (slow != fast) { + slow = slow.next; + fast = fast.next; + + } + return fast; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..5202e9d07fe2b5f09d1d487a06a92a74e08e5458 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-87f2181c6ec246618b56ff14516d7c29", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c6e8611f740ccd45195a81048266ffb79d350227 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "197a8b12e097460ab3a266818f16699c", + "author": "csdn.net", + "keywords": "栈,递归,链表,双指针" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7bbd3f3dd948a0c43b3e960358fddb7b83f1cba7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/143.exercises/solution.md" @@ -0,0 +1,109 @@ +# 重排链表 + +

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

+ +

 L→ L→ … → Ln-1 → L
+请将其重新排列后变为:

+ +

L→ L→ L→ Ln-1 → L→ Ln-2 → …

+ +

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

+ +

 

+ +

示例 1:

+ +

+ +
+输入: head = [1,2,3,4]
+输出: [1,4,2,3]
+ +

示例 2:

+ +

+ +
+输入: head = [1,2,3,4,5]
+输出: [1,5,2,4,3]
+ +

 

+ +

提示:

+ +
    +
  • 链表的长度范围为 [1, 5 * 104]
  • +
  • 1 <= node.val <= 1000
  • +
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public void reorderList(ListNode head) { + if (head == null) { + return; + } + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + + ListNode cur = slow.next, pre = null, next = null; + slow.next = null; + while (cur != null) { + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + + ListNode p1 = head, p2 = pre; + while (p1 != null && p2 != null) { + next = p2.next; + p2.next = p1.next; + p1.next = p2; + p1 = p2.next; + p2 = next; + } + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e7f23254863300e423719e8d6cb9a5ef142039b0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4603d97fa8f342b7921dfdeb6b1563fc", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b6ca70c2830cc9cefc55506b22586caa2da2c32f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "fcd911bde6db4e1f8ba109f6e3dd6a7e", + "author": "csdn.net", + "keywords": "设计,哈希表,链表,双向链表" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..767313204815063b0d066e059f4e257bad488da0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/146.exercises/solution.md" @@ -0,0 +1,149 @@ +# LRU 缓存机制 + +
运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制
+ +
+
+

实现 LRUCache 类:

+ +
    +
  • LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
  • +
  • int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1
  • +
  • void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
  • +
+ +

 

+
+
+ +

进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?

+ +

 

+ +

示例:

+ +
+输入
+["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
+[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
+输出
+[null, null, null, 1, null, -1, null, -1, 3, 4]
+
+解释
+LRUCache lRUCache = new LRUCache(2);
+lRUCache.put(1, 1); // 缓存是 {1=1}
+lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
+lRUCache.get(1);    // 返回 1
+lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
+lRUCache.get(2);    // 返回 -1 (未找到)
+lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
+lRUCache.get(1);    // 返回 -1 (未找到)
+lRUCache.get(3);    // 返回 3
+lRUCache.get(4);    // 返回 4
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= capacity <= 3000
  • +
  • 0 <= key <= 10000
  • +
  • 0 <= value <= 105
  • +
  • 最多调用 2 * 105getput
  • +
+ + +## template + +```java + +class LRUCache { + + class Node { + Node prev, next; + int key, value; + + Node(int _key, int _value) { + key = _key; + value = _value; + } + } + + Node head = new Node(0, 0), tail = new Node(0, 0); + Map map = new HashMap<>(); + int max_len; + + public LRUCache(int capacity) { + max_len = capacity; + + head.next = tail; + tail.prev = head; + } + + public int get(int key) { + if (map.containsKey(key)) { + Node node = map.get(key); + remove(node); + add(node); + return node.value; + } else { + return -1; + } + } + + public void put(int key, int value) { + if (map.containsKey(key)) { + remove(map.get(key)); + } + + if (map.size() == max_len) { + remove(head.next); + } + add(new Node(key, value)); + } + + private void remove(Node node) { + map.remove(node.key); + + node.prev.next = node.next; + node.next.prev = node.prev; + } + + private void add(Node node) { + map.put(node.key, node); + Node pre_tail = tail.prev; + node.next = tail; + tail.prev = node; + pre_tail.next = node; + node.prev = pre_tail; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6dcee4a0bce722ef1472970d1382f76f78a59700 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2681d3787791429a92a1122759a4e9ba", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..417884f67f06a7afdb58207c59e4b971b44c3fc9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "6267d9fe114f48b890c6ed28dc63da05", + "author": "csdn.net", + "keywords": "链表,排序" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a1c627ff7de937377bff7d76d084e6ddef01ad4f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/147.exercises/solution.md" @@ -0,0 +1,101 @@ +# 对链表进行插入排序 + +

对链表进行插入排序。

+ +


+插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。
+每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。

+ +

 

+ +

插入排序算法:

+ +
    +
  1. 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
  2. +
  3. 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
  4. +
  5. 重复直到所有输入数据插入完为止。
  6. +
+ +

 

+ +

示例 1:

+ +
输入: 4->2->1->3
+输出: 1->2->3->4
+
+ +

示例 2:

+ +
输入: -1->5->3->4->0
+输出: -1->0->3->4->5
+
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + + +class Solution { + public ListNode insertionSortList(ListNode head) { + if (head == null) + return head; + ListNode res = new ListNode(head.val); + ListNode left = head.next; + while ((left != null)) { + ListNode cur = left; + left = left.next; + + if (cur.val <= res.val) { + cur.next = res; + res = cur; + continue; + } + + ListNode p = res; + ListNode last = p; + while (p != null && p.val < cur.val) { + last = p; + p = p.next; + } + last.next = cur; + last.next.next = p; + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..9a06fba03f32980581a8b4b723a7a91dbe6d3fc9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-60835a48fea84f49a675dd21417d061b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..223d7fba75119f7ebc35a44c157c01f8eb71b263 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "71419e1d7bc74b23958e1b78923ad9a1", + "author": "csdn.net", + "keywords": "链表,双指针,分治,排序,归并排序" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..bb862a6a5095775fd7248d243c91d9a3050b1ed6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/148.exercises/solution.md" @@ -0,0 +1,142 @@ +# 排序链表 + +

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表

+ +

进阶:

+ +
    +
  • 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序吗?
  • +
+ +

 

+ +

示例 1:

+ +
+输入:head = [4,2,1,3]
+输出:[1,2,3,4]
+
+ +

示例 2:

+ +
+输入:head = [-1,5,3,4,0]
+输出:[-1,0,3,4,5]
+
+ +

示例 3:

+ +
+输入:head = []
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • 链表中节点的数目在范围 [0, 5 * 104] 内
  • +
  • -105 <= Node.val <= 105
  • +
+ + +## template + +```java +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public ListNode sortList(ListNode head) { + + if (head == null) + return head; + + return mergeSort(head); + } + + public ListNode mergeSort(ListNode head) { + + if (head.next == null) + return head; + + ListNode p1 = head; + ListNode p2 = head.next; + + if (p2 != null && p2.next != null) { + p1 = p1.next; + p2 = p2.next.next; + } + ListNode left = head; + ListNode right = p1.next; + p1.next = null; + + left = mergeSort(left); + right = mergeSort(right); + + return merge(left, right); + } + + public ListNode merge(ListNode left, ListNode right) { + + ListNode head = null; + + if (left.val < right.val) { + head = left; + left = left.next; + } else { + head = right; + right = right.next; + } + ListNode tmp = head; + + while (left != null && right != null) { + if (left.val < right.val) { + tmp.next = left; + left = left.next; + } else { + tmp.next = right; + right = right.next; + } + tmp = tmp.next; + } + + tmp.next = left != null ? left : right; + return head; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..884f56d024aca2567b4531c9f75b2251aa51bc0d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e7e86a4d8b7944b38101f90b945020e3", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..20a26248cf7f6a11a2929e2bfd47595194856e69 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "30c3029500da406d97c9f663305f3a22", + "author": "csdn.net", + "keywords": "栈,数组,数学" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7339944e932643ecbd7116953be7be7c08af7f3d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/150.exercises/solution.md" @@ -0,0 +1,127 @@ +# 逆波兰表达式求值 + +

根据 逆波兰表示法,求表达式的值。

+ +

有效的算符包括 +-*/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。

+ +

 

+ +

说明:

+ +
    +
  • 整数除法只保留整数部分。
  • +
  • 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
  • +
+ +

 

+ +

示例 1:

+ +
+输入:tokens = ["2","1","+","3","*"]
+输出:9
+解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
+
+ +

示例 2:

+ +
+输入:tokens = ["4","13","5","/","+"]
+输出:6
+解释:该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6
+
+ +

示例 3:

+ +
+输入:tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
+输出:22
+解释:
+该算式转化为常见的中缀算术表达式为:
+  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
+= ((10 * (6 / (12 * -11))) + 17) + 5
+= ((10 * (6 / -132)) + 17) + 5
+= ((10 * 0) + 17) + 5
+= (0 + 17) + 5
+= 17 + 5
+= 22
+ +

 

+ +

提示:

+ +
    +
  • 1 <= tokens.length <= 104
  • +
  • tokens[i] 要么是一个算符("+""-""*""/"),要么是一个在范围 [-200, 200] 内的整数
  • +
+ +

 

+ +

逆波兰表达式:

+ +

逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。

+ +
    +
  • 平常使用的算式则是一种中缀表达式,如 ( 1 + 2 ) * ( 3 + 4 )
  • +
  • 该算式的逆波兰表达式写法为 ( ( 1 2 + ) ( 3 4 + ) * )
  • +
+ +

逆波兰表达式主要有以下两个优点:

+ +
    +
  • 去掉括号后表达式无歧义,上式即便写成 1 2 + 3 4 + * 也可以依据次序计算出正确结果。
  • +
  • 适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中。
  • +
+ + +## template + +```java +class Solution { + public int evalRPN(String[] tokens) { + Stack stack = new Stack<>(); + for (String s : tokens) { + if (s.equals("+")) { + stack.push(stack.pop() + stack.pop()); + } else if (s.equals("-")) { + stack.push(-stack.pop() + stack.pop()); + } else if (s.equals("*")) { + stack.push(stack.pop() * stack.pop()); + } else if (s.equals("/")) { + int num1 = stack.pop(); + stack.push(stack.pop() / num1); + } else { + stack.push(Integer.parseInt(s)); + } + } + return stack.pop(); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ba33b9f1067a4bd12ed3c65c2f52fe077439b58b --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4a9e2b04c13c495fa9c9b868ecea654b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.json" similarity index 68% rename from "data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.json" rename to "data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.json" index f57c210e7619e509198b3e0849ac3374c8ac1647..d984e14dc4c470ae953982427aaf358941d34c2d 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.json" +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "5a059b5a61b448158043f249c46f61ae", + "exercise_id": "2a8b52e0c9894ceea4d669185da8bafa", "author": "csdn.net", "keywords": "双指针,字符串" } \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ff96068b2f4239b6a7ce387f86db7dad8ab17848 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/151.exercises/solution.md" @@ -0,0 +1,167 @@ +# 翻转字符串里的单词 + +

给你一个字符串 s ,逐个翻转字符串中的所有 单词

+ +

单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。

+ +

请你返回一个翻转 s 中单词顺序并用单个空格相连的字符串。

+ +

说明:

+ +
    +
  • 输入字符串 s 可以在前面、后面或者单词间包含多余的空格。
  • +
  • 翻转后单词间应当仅用一个空格分隔。
  • +
  • 翻转后的字符串中不应包含额外的空格。
  • +
+ +

 

+ +

示例 1:

+ +
+输入:s = "the sky is blue"
+输出:"blue is sky the"
+
+ +

示例 2:

+ +
+输入:s = "  hello world  "
+输出:"world hello"
+解释:输入字符串可以在前面或者后面包含多余的空格,但是翻转后的字符不能包括。
+
+ +

示例 3:

+ +
+输入:s = "a good   example"
+输出:"example good a"
+解释:如果两个单词间有多余的空格,将翻转后单词间的空格减少到只含一个。
+
+ +

示例 4:

+ +
+输入:s = "  Bob    Loves  Alice   "
+输出:"Alice Loves Bob"
+
+ +

示例 5:

+ +
+输入:s = "Alice does not even like bob"
+输出:"bob like even not does Alice"
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 104
  • +
  • s 包含英文大小写字母、数字和空格 ' '
  • +
  • s至少存在一个 单词
  • +
+ +
    +
+ +

 

+ +

进阶:

+ +
    +
  • 请尝试使用 O(1) 额外空间复杂度的原地解法。
  • +
+ + +## template + +```java +class Solution { + public StringBuilder trimSpaces(String s) { + int left = 0, right = s.length() - 1; + + while (left <= right && s.charAt(left) == ' ') + ++left; + + while (left <= right && s.charAt(right) == ' ') + --right; + + StringBuilder sb = new StringBuilder(); + while (left <= right) { + char c = s.charAt(left); + + if (c != ' ') + sb.append(c); + else if (sb.charAt(sb.length() - 1) != ' ') + sb.append(c); + + ++left; + } + return sb; + } + + public void reverse(StringBuilder sb, int left, int right) { + while (left < right) { + char tmp = sb.charAt(left); + sb.setCharAt(left++, sb.charAt(right)); + sb.setCharAt(right--, tmp); + } + } + + public void reverseEachWord(StringBuilder sb) { + int n = sb.length(); + int start = 0, end = 0; + + while (start < n) { + + while (end < n && sb.charAt(end) != ' ') + ++end; + + reverse(sb, start, end - 1); + + start = end + 1; + ++end; + } + } + + public String reverseWords(String s) { + StringBuilder sb = trimSpaces(s); + + reverse(sb, 0, sb.length() - 1); + + reverseEachWord(sb); + + return sb.toString(); + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..afbb4f9afa3a2a9d9960959694035342d3ec6c82 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-400b5243e2ff479bb8227fe50ff587ec", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4b5bb7dadea8880633b6d447c7d9ec82a737a6a0 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "65950a04beae457f82920300e5028918", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..3182a5a6aa427dc0dc0d5ae9e12762dbce45bfe2 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/152.exercises/solution.md" @@ -0,0 +1,68 @@ +# 乘积最大子数组 + +

给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。

+ +

 

+ +

示例 1:

+ +
输入: [2,3,-2,4]
+输出: 6
+解释: 子数组 [2,3] 有最大乘积 6。
+
+ +

示例 2:

+ +
输入: [-2,0,-1]
+输出: 0
+解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
+ + +## template + +```java + +class Solution { + public int maxProduct(int[] nums) { + int max = Integer.MIN_VALUE, imax = 1, imin = 1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0) { + int tmp = imax; + imax = imin; + imin = tmp; + } + imax = Math.max(imax * nums[i], nums[i]); + imin = Math.min(imin * nums[i], nums[i]); + + max = Math.max(max, imax); + } + return max; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d5dc518bf41823443d264c15d1858a091d446a4d --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-c4642447f1a1419ea730cc38c0d1a1e5", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ca494072f6b430f5ad666a51d017ae0902188848 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "27c7eb432aa541659f91e42739ce47c5", + "author": "csdn.net", + "keywords": "数组,二分查找" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..cbe9dce6a6c8676d0db413439ae0117e0107d15a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/153.exercises/solution.md" @@ -0,0 +1,97 @@ +# 寻找旋转排序数组中的最小值 + +已知一个长度为 n 的数组,预先按照升序排列,经由 1n旋转 后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到: +
    +
  • 若旋转 4 次,则可以得到 [4,5,6,7,0,1,2]
  • +
  • 若旋转 7 次,则可以得到 [0,1,2,4,5,6,7]
  • +
+ +

注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]]

+ +

给你一个元素值 互不相同 的数组 nums ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素

+ +

 

+ +

示例 1:

+ +
+输入:nums = [3,4,5,1,2]
+输出:1
+解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
+
+ +

示例 2:

+ +
+输入:nums = [4,5,6,7,0,1,2]
+输出:0
+解释:原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。
+
+ +

示例 3:

+ +
+输入:nums = [11,13,15,17]
+输出:11
+解释:原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
+
+ +

 

+ +

提示:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 5000
  • +
  • -5000 <= nums[i] <= 5000
  • +
  • nums 中的所有整数 互不相同
  • +
  • nums 原来是一个升序排序的数组,并进行了 1n 次旋转
  • +
+ + +## template + +```java + +class Solution { + public int findMin(int[] nums) { + int low = 0, high = nums.length - 1, mid = 0; + while (low <= high) { + mid = (low + high) / 2; + if (nums[mid] > nums[high]) + low = mid + 1; + else if (nums[mid] < nums[high]) + high = mid; + else + high--; + } + return nums[low]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0cbb762eead74b294dc560e7c063ecc928d3a62f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-9b414557020445448be1be09432d9887", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0d48a11b2da2dd19f6e316b0b44f4ffff3ff8cbf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "777e3a0d404c492096839996e9bd0388", + "author": "csdn.net", + "keywords": "数组,二分查找" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1a84306bb29f7c9433361c5f1ea28f7276f102f6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/162.exercises/solution.md" @@ -0,0 +1,86 @@ +# 寻找峰值 + +

峰值元素是指其值严格大于左右相邻值的元素。

+ +

给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。

+ +

你可以假设 nums[-1] = nums[n] = -∞

+ +

你必须实现时间复杂度为 O(log n) 的算法来解决此问题。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,2,3,1]
+输出:2
+解释:3 是峰值元素,你的函数应该返回其索引 2。
+ +

示例 2:

+ +
+输入:nums = [1,2,1,3,5,6,4]
+输出:1 或 5 
+解释:你的函数可以返回索引 1,其峰值元素为 2;
+     或者返回索引 5, 其峰值元素为 6。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 1000
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • 对于所有有效的 i 都有 nums[i] != nums[i + 1]
  • +
+ + +## template + +```java + +class Solution { + public int findPeakElement(int[] nums) { + if (nums.length == 1) + return 0; + int len = nums.length; + int low = 0, mid = 0, high = nums.length - 1; + while (low < high) { + mid = (low + high) / 2; + if (nums[mid] > nums[mid + 1]) + high = mid; + else + low = mid + 1; + } + return low; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..283724587246c524a0d58954a009c22c774bebbe --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2eef2fa76d214f628309d07d3ec77a21", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..8804c2a3e1858ca2c54a61b6d687c5227015cbf9 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "244c65fcb46345da8a6f79f4a63934ef", + "author": "csdn.net", + "keywords": "双指针,字符串" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..e146b43b4e859b12a10a0e24bc108430cd28f4bf --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/165.exercises/solution.md" @@ -0,0 +1,117 @@ +# 比较版本号 + +

给你两个版本号 version1version2 ,请你比较它们。

+ +

版本号由一个或多个修订号组成,各修订号由一个 '.' 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.330.1 都是有效的版本号。

+ +

比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 010 < 1

+ +

返回规则如下:

+ +
    +
  • 如果 version1 version2 返回 1
  • +
  • 如果 version1 version2 返回 -1
  • +
  • 除此之外返回 0
  • +
+ +

 

+ +

示例 1:

+ +
+输入:version1 = "1.01", version2 = "1.001"
+输出:0
+解释:忽略前导零,"01" 和 "001" 都表示相同的整数 "1"
+
+ +

示例 2:

+ +
+输入:version1 = "1.0", version2 = "1.0.0"
+输出:0
+解释:version1 没有指定下标为 2 的修订号,即视为 "0"
+
+ +

示例 3:

+ +
+输入:version1 = "0.1", version2 = "1.1"
+输出:-1
+解释:version1 中下标为 0 的修订号是 "0",version2 中下标为 0 的修订号是 "1" 。0 < 1,所以 version1 < version2
+
+ +

示例 4:

+ +
+输入:version1 = "1.0.1", version2 = "1"
+输出:1
+
+ +

示例 5:

+ +
+输入:version1 = "7.5.2.4", version2 = "7.5.3"
+输出:-1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= version1.length, version2.length <= 500
  • +
  • version1version2 仅包含数字和 '.'
  • +
  • version1version2 都是 有效版本号
  • +
  • version1version2 的所有修订号都可以存储在 32 位整数
  • +
+ + +## template + +```java + +class Solution { + public int compareVersion(String version1, String version2) { + String[] a1 = version1.split("\\."); + String[] a2 = version2.split("\\."); + + for (int n = 0; n < Math.max(a1.length, a2.length); n++) { + int i = (n < a1.length ? Integer.valueOf(a1[n]) : 0); + int j = (n < a2.length ? Integer.valueOf(a2[n]) : 0); + if (i < j) + return -1; + else if (i > j) + return 1; + } + return 0; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..399cb56d0410b8990e4f93c7323e1e61f04a492a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-61c201bf3acc4f73aec3e3637c1149cb", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..645f3f8caf375eeaa64ee1c3994d51ba9825b84c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "1ff02b73fbc84fdca3e87f23ac478ce1", + "author": "csdn.net", + "keywords": "哈希表,数学,字符串" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d9403f59deca559addc9a906fc9842aa2e00f21 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/166.exercises/solution.md" @@ -0,0 +1,117 @@ +# 分数到小数 + +

给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以 字符串形式返回小数

+ +

如果小数部分为循环小数,则将循环的部分括在括号内。

+ +

如果存在多个答案,只需返回 任意一个

+ +

对于所有给定的输入,保证 答案字符串的长度小于 104

+ +

 

+ +

示例 1:

+ +
+输入:numerator = 1, denominator = 2
+输出:"0.5"
+
+ +

示例 2:

+ +
+输入:numerator = 2, denominator = 1
+输出:"2"
+
+ +

示例 3:

+ +
+输入:numerator = 2, denominator = 3
+输出:"0.(6)"
+
+ +

示例 4:

+ +
+输入:numerator = 4, denominator = 333
+输出:"0.(012)"
+
+ +

示例 5:

+ +
+输入:numerator = 1, denominator = 5
+输出:"0.2"
+
+ +

 

+ +

提示:

+ +
    +
  • -231 <= numerator, denominator <= 231 - 1
  • +
  • denominator != 0
  • +
+ + +## template + +```java + +class Solution { + public String fractionToDecimal(int numerator, int denominator) { + if (numerator == 0) + return "0"; + StringBuilder str = new StringBuilder(); + if (numerator < 0 ^ denominator < 0) + str.append('-'); + long dividend = Math.abs(Long.valueOf(numerator)); + long divisor = Math.abs(Long.valueOf(denominator)); + str.append(String.valueOf(dividend / divisor)); + long remainter = dividend % divisor; + if (remainter == 0) + return str.toString(); + str.append('.'); + Map map = new HashMap<>(); + while (remainter != 0) { + if (map.containsKey(remainter)) { + str.insert(map.get(remainter), "("); + str.append(")"); + break; + } + map.put(remainter, str.length()); + remainter *= 10; + str.append(String.valueOf(remainter / divisor)); + remainter %= divisor; + } + return str.toString(); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6c3f684c28ec8194e2707b6bcc859378192d6eb8 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-37c3b26089ae4634bfe836b73a515303", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6fefcf7dcf7a365d7c975896ef664632e499cbc7 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e18c19c6ef0d4268a8154c9b3e1a2f1a", + "author": "csdn.net", + "keywords": "栈,树,设计,二叉搜索树,二叉树,迭代器" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..807e519acef3ad1bb5df9bdc79413605c12f6baa --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/173.exercises/solution.md" @@ -0,0 +1,134 @@ +# 二叉搜索树迭代器 + +实现一个二叉搜索树迭代器类BSTIterator ,表示一个按中序遍历二叉搜索树(BST)的迭代器: +
+
+
    +
  • BSTIterator(TreeNode root) 初始化 BSTIterator 类的一个对象。BST 的根节点 root 会作为构造函数的一部分给出。指针应初始化为一个不存在于 BST 中的数字,且该数字小于 BST 中的任何元素。
  • +
  • boolean hasNext() 如果向指针右侧遍历存在数字,则返回 true ;否则返回 false
  • +
  • int next()将指针向右移动,然后返回指针处的数字。
  • +
+ +

注意,指针初始化为一个不存在于 BST 中的数字,所以对 next() 的首次调用将返回 BST 中的最小元素。

+
+
+ +

你可以假设 next() 调用总是有效的,也就是说,当调用 next() 时,BST 的中序遍历中至少存在一个下一个数字。

+ +

 

+ +

示例:

+ +
+输入
+["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
+[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
+输出
+[null, 3, 7, true, 9, true, 15, true, 20, false]
+
+解释
+BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
+bSTIterator.next();    // 返回 3
+bSTIterator.next();    // 返回 7
+bSTIterator.hasNext(); // 返回 True
+bSTIterator.next();    // 返回 9
+bSTIterator.hasNext(); // 返回 True
+bSTIterator.next();    // 返回 15
+bSTIterator.hasNext(); // 返回 True
+bSTIterator.next();    // 返回 20
+bSTIterator.hasNext(); // 返回 False
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点的数目在范围 [1, 105]
  • +
  • 0 <= Node.val <= 106
  • +
  • 最多调用 105hasNextnext 操作
  • +
+ +

 

+ +

进阶:

+ +
    +
  • 你可以设计一个满足下述条件的解决方案吗?next()hasNext() 操作均摊时间复杂度为 O(1) ,并使用 O(h) 内存。其中 h 是树的高度。
  • +
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class BSTIterator { + LinkedList stack = new LinkedList<>(); + + public BSTIterator(TreeNode root) { + TreeNode cur = root; + while (cur != null) { + stack.push(cur); + cur = cur.left; + } + } + + public int next() { + TreeNode n = stack.pop(); + TreeNode cur = n.right; + while (cur != null) { + stack.push(cur); + cur = cur.left; + } + return n.val; + } + + public boolean hasNext() { + return !stack.isEmpty(); + } +} + +/** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8fbaa6a3891a492d95fc93748c429c4eaf0be7f6 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-7b60fae19a444e0aaa8fa1f415290cc3", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..f8f049e90de497bbe2dea328a2d0f19cdc373c84 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "694715c7a1484e80b939cbaed3256af1", + "author": "csdn.net", + "keywords": "贪心,字符串,排序" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f562d2d23b4befacdd84ecfa9d5c9d63d0eab245 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/179.exercises/solution.md" @@ -0,0 +1,108 @@ +# 最大数 + +

给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。

+ +

注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。

+ +

 

+ +

示例 1:

+ +
+输入nums = [10,2]
+输出:"210"
+ +

示例 2:

+ +
+输入nums = [3,30,34,5,9]
+输出:"9534330"
+
+ +

示例 3:

+ +
+输入nums = [1]
+输出:"1"
+
+ +

示例 4:

+ +
+输入nums = [10]
+输出:"10"
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 109
  • +
+ + +## template + +```java +class Solution { + public String largestNumber(int[] nums) { + + String[] str = new String[nums.length]; + + for (int i = 0; i < nums.length; i++) + str[i] = String.valueOf(nums[i]); + + Arrays.parallelSort(str); + + for (int i = 1; i < str.length; i++) + for (int j = 0; j < i; j++) { + if (str[i].length() > str[j].length() && str[i].substring(0, str[j].length()).equals(str[j])) { + StringBuilder str1 = new StringBuilder(); + StringBuilder str2 = new StringBuilder(); + str1.append(str[i] + str[j]); + str2.append(str[j] + str[i]); + if (str2.toString().compareTo(str1.toString()) > 0) { + String tmp = str[i]; + str[i] = str[j]; + str[j] = tmp; + } + } + } + StringBuilder ans = new StringBuilder(); + + for (int i = str.length - 1; i >= 0; i--) + ans.append(str[i]); + + return ans.charAt(0) == '0' ? "0" : ans.toString(); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..394515871e09dbe31f3588d61c28a402ecd2bcdc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-fff53c38bfc141e6b4a9cf7f77159674", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..79b4c79547a19735d32c3aec44fe3968c4411b53 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "84d39ae3a62d48309d5c5dda130a901f", + "author": "csdn.net", + "keywords": "位运算,哈希表,字符串,滑动窗口,哈希函数,滚动哈希" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..aebf1f37ff7d3ab440e2277b494967251ee3e1ac --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/187.exercises/solution.md" @@ -0,0 +1,76 @@ +# 重复的DNA序列 + +

所有 DNA 都由一系列缩写为 'A''C''G''T' 的核苷酸组成,例如:"ACGAATTCCG"。在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助。

+ +

编写一个函数来找出所有目标子串,目标子串的长度为 10,且在 DNA 字符串 s 中出现次数超过一次。

+ +

 

+ +

示例 1:

+ +
+输入:s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
+输出:["AAAAACCCCC","CCCCCAAAAA"]
+
+ +

示例 2:

+ +
+输入:s = "AAAAAAAAAAAAA"
+输出:["AAAAAAAAAA"]
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= s.length <= 105
  • +
  • s[i]'A''C''G''T'
  • +
+ + +## template + +```java +class Solution { + public List findRepeatedDnaSequences(String s) { + Set set = new HashSet<>(); + Set help = new HashSet<>(); + for (int i = 0; i <= s.length() - 10; i++) { + String cur = s.substring(i, i + 10); + if (!set.add(cur)) + help.add(cur); + } + return new ArrayList(help); + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..7fee72a2c798e790c9c22a1b4c0511be3c38e114 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-9cf44bed13e34e3f885b69595f169dfd", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..db6a037746e8e44cf57db8923285e55d841e2a4c --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "dcba827a84eb45ef889091d28e2b99f8", + "author": "csdn.net", + "keywords": "数组,数学,双指针" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c7ceee910177fc7abcd97752b49e592519022f69 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/189.exercises/solution.md" @@ -0,0 +1,107 @@ +# 旋转数组 + +

给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。

+ +

 

+ +

进阶:

+ +
    +
  • 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
  • +
  • 你可以使用空间复杂度为 O(1) 的 原地 算法解决这个问题吗?
  • +
+ +

 

+ +

示例 1:

+ +
+输入: nums = [1,2,3,4,5,6,7], k = 3
+输出: [5,6,7,1,2,3,4]
+解释:
+向右旋转 1 步: [7,1,2,3,4,5,6]
+向右旋转 2 步: [6,7,1,2,3,4,5]
+向右旋转 3 步: [5,6,7,1,2,3,4]
+
+ +

示例 2:

+ +
+输入:nums = [-1,-100,3,99], k = 2
+输出:[3,99,-1,-100]
+解释: 
+向右旋转 1 步: [99,-1,-100,3]
+向右旋转 2 步: [3,99,-1,-100]
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 2 * 104
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • 0 <= k <= 105
  • +
+ +
    +
+ + +## template + +```java + +class Solution { + public void rotate(int[] nums, int k) { + if (k > nums.length) { + k = k % nums.length; + } + int mark = nums.length - k; + int mark2 = (nums.length + mark) / 2; + for (int i = 0; i < (mark + 1) / 2; i++) { + swap(nums, i, mark - 1 - i); + } + + for (int j = mark; j < mark2; j++) { + swap(nums, j, nums.length + mark - 1 - j); + } + + for (int index = 0; index < nums.length / 2; index++) { + swap(nums, index, nums.length - 1 - index); + } + + } + + public static void swap(int[] nums, int a, int b) { + int temp = nums[a]; + nums[a] = nums[b]; + nums[b] = temp; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..013384023ce005014e1dcdd1d9a5464b635b09cc --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-51e09058a3234838854b95921b2bb5ba", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c9cf1ce798bfbeb5e03b46c9e8b635913355f1b4 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "815e7d0c8bcb417a8ee8f6b8a80c72ee", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..b9eee115a00e74b68db40f510ad4172922dcd099 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/198.exercises/solution.md" @@ -0,0 +1,83 @@ +# 打家劫舍 + +

你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警

+ +

给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。

+ +

 

+ +

示例 1:

+ +
+输入:[1,2,3,1]
+输出:4
+解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
+     偷窃到的最高金额 = 1 + 3 = 4 。
+ +

示例 2:

+ +
+输入:[2,7,9,3,1]
+输出:12
+解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
+     偷窃到的最高金额 = 2 + 9 + 1 = 12 。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 100
  • +
  • 0 <= nums[i] <= 400
  • +
+ + +## template + +```java +class Solution { + public int rob(int[] nums) { + if (nums.length == 0) + return 0; + if (nums.length == 1) + return nums[0]; + int result[] = new int[nums.length]; + result[0] = nums[0]; + result[1] = Math.max(nums[0], nums[1]); + for (int i = 2; i < result.length; i++) { + result[i] = Math.max(result[i - 1], result[i - 2] + nums[i]); + } + + return result[result.length - 1]; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..12b5def884a98023fb52fd3f763ff0c7b143a068 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-aafb162733204b54ad1659371e1b8f4a", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d7d3417f9ab136e84b2560ff9cbae26d55945937 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "b68c4b0a4f974ab4b38ec8d3a8eb9c77", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,二叉树" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..0f1f99f0005c3f00123078903285b2c78b1962ae --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/199.exercises/solution.md" @@ -0,0 +1,101 @@ +# 二叉树的右视图 + +

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

+ +

 

+ +

示例 1:

+ +

+ +
+输入: [1,2,3,null,5,null,4]
+输出: [1,3,4]
+
+ +

示例 2:

+ +
+输入: [1,null,3]
+输出: [1,3]
+
+ +

示例 3:

+ +
+输入: []
+输出: []
+
+ +

 

+ +

提示:

+ +
    +
  • 二叉树的节点个数的范围是 [0,100]
  • +
  • -100 <= Node.val <= 100 
  • +
+ + +## template + +```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List rightSideView(TreeNode root) { + List list = new ArrayList(); + if (root == null) + return list; + + list.add(root.val); + List list1 = rightSideView(root.right); + List list2 = rightSideView(root.left); + + list.addAll(list1); + + if (list1.size() < list2.size()) { + for (int i = list1.size(); i < list2.size(); i++) { + list.add(list2.get(i)); + } + } + + return list; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/config.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..aae0ff8eb74183df7ec29ab197a7212000b4021f --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-805cf116376a4e7bbb4b28835ecb711d", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.json" "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7c1069ec7ce763c026da4621274a1841de632554 --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e7be0db8bb5844eba148a6e28f727ae3", + "author": "csdn.net", + "keywords": "深度优先搜索,广度优先搜索,并查集,数组,矩阵" +} \ No newline at end of file diff --git "a/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.md" "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8fda15e7537ff93184c5afec2e61789bd11f074a --- /dev/null +++ "b/data/2.dailycode\344\270\255\351\230\266/2.java/200.exercises/solution.md" @@ -0,0 +1,103 @@ +# 岛屿数量 + +

给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。

+ +

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

+ +

此外,你可以假设该网格的四条边均被水包围。

+ +

 

+ +

示例 1:

+ +
+输入:grid = [
+  ["1","1","1","1","0"],
+  ["1","1","0","1","0"],
+  ["1","1","0","0","0"],
+  ["0","0","0","0","0"]
+]
+输出:1
+
+ +

示例 2:

+ +
+输入:grid = [
+  ["1","1","0","0","0"],
+  ["1","1","0","0","0"],
+  ["0","0","1","0","0"],
+  ["0","0","0","1","1"]
+]
+输出:3
+
+ +

 

+ +

提示:

+ +
    +
  • m == grid.length
  • +
  • n == grid[i].length
  • +
  • 1 <= m, n <= 300
  • +
  • grid[i][j] 的值为 '0''1'
  • +
+ + +## template + +```java + +class Solution { + public int numIslands(char[][] grid) { + int islandNum = 0; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == '1') { + infect(grid, i, j); + islandNum++; + } + } + } + return islandNum; + } + + public void infect(char[][] grid, int i, int j) { + if (i < 0 || i >= grid.length || + j < 0 || j >= grid[0].length || grid[i][j] != '1') { + return; + } + grid[i][j] = '2'; + infect(grid, i + 1, j); + infect(grid, i - 1, j); + infect(grid, i, j + 1); + infect(grid, i, j - 1); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..06c2ceaaf87bc97e3773db8af970ae075889d830 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-b827a10b7bfb4e49a353f144ec097c9a", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..eeda894e5e115140bf64460f78bc581180e0036a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "c73ef4e33d884238a7f8e9d704408d9e", + "author": "csdn.net", + "keywords": "字符串,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..931f1d2e6cbd2114b2839fa7e13edc6e14960103 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/115.exercises/solution.md" @@ -0,0 +1,96 @@ +# 不同的子序列 + +

给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。

+ +

字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,"ACE" 是 "ABCDE" 的一个子序列,而 "AEC" 不是)

+ +

题目数据保证答案符合 32 位带符号整数范围。

+ +

 

+ +

示例 1:

+ +
+输入:s = "rabbbit", t = "rabbit"
+输出3
+解释:
+如下图所示, 有 3 种可以从 s 中得到 "rabbit" 的方案。
+rabbbit
+rabbbit
+rabbbit
+ +

示例 2:

+ +
+输入:s = "babgbag", t = "bag"
+输出5
+解释:
+如下图所示, 有 5 种可以从 s 中得到 "bag" 的方案。 
+babgbag
+babgbag
+babgbag
+babgbag
+babgbag
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= s.length, t.length <= 1000
  • +
  • st 由英文字母组成
  • +
+ + +## template + +```java +class Solution { + public int numDistinct(String s, String t) { + int n1 = s.length(); + int n2 = t.length(); + int[][] dp = new int[n1 + 1][n2 + 1]; + for (int i = 0; i <= n1; i++) { + dp[i][0] = 1; + } + for (int i = 1; i <= n1; i++) { + for (int j = 1; j <= n2; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + return dp[n1][n2]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cefc48004f3970b1cfa58d3a328f4c423164e56b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-18505f78aea44c34928d351e76a21f6b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..de19e3e1c95d3373a0ce7784c35235786aeaa91f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "a26e1f31344140fbab1774f76a47193e", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..32164c153cc964410dce342f6a43c25923f70f70 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/123.exercises/solution.md" @@ -0,0 +1,122 @@ +# 买卖股票的最佳时机 III + +

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

+ +

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

+ +

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

+ +

 

+ +

示例 1:

+ +
+输入:prices = [3,3,5,0,0,3,1,4]
+输出:6
+解释:在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
+     随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。
+ +

示例 2:

+ +
+输入:prices = [1,2,3,4,5]
+输出:4
+解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。   
+     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。   
+     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
+
+ +

示例 3:

+ +
+输入:prices = [7,6,4,3,1] 
+输出:0 
+解释:在这个情况下, 没有交易完成, 所以最大利润为 0。
+ +

示例 4:

+ +
+输入:prices = [1]
+输出:0
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= prices.length <= 105
  • +
  • 0 <= prices[i] <= 105
  • +
+ + +## template + +```java +public class Solution { + public int maxProfit(int[] prices) { + int result = 0; + if (prices.length == 0) { + return result; + } + int firstDealSell; + int secondDealSell; + for (secondDealSell = prices.length - 1; secondDealSell > 0; secondDealSell--) { + if (prices[secondDealSell - 1] < prices[secondDealSell]) { + break; + } + } + for (firstDealSell = 1; firstDealSell < prices.length; firstDealSell++) { + while (firstDealSell + 1 < prices.length && prices[firstDealSell + 1] >= prices[firstDealSell]) { + firstDealSell++; + } + int result1 = maxProfit(prices, 0, firstDealSell); + int result2 = maxProfit(prices, firstDealSell + 1, secondDealSell); + if (result1 + result2 > result) { + result = result1 + result2; + } + } + return result; + } + + private int maxProfit(int[] prices, int left, int right) { + int result = 0; + if (right - left < 1) { + return result; + } + int minPrice = prices[left]; + for (int i = left + 1; i <= right; i++) { + result = Math.max(result, prices[i] - minPrice); + minPrice = Math.min(minPrice, prices[i]); + } + return result; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..989d8b3a2bab27af0b5dc9af2503d1c4c71e6852 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-dbcf2b90100c4e939ebf7dac9ce64387", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..202bc1eefec4f7cb6b7baee5924883b4d498538f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "9d331fa3fb014da08b32050ccef6cb57", + "author": "csdn.net", + "keywords": "树,深度优先搜索,动态规划,二叉树" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..24727689f7bc9a16655053faf8f4e467f4efbb89 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/124.exercises/solution.md" @@ -0,0 +1,88 @@ +# 二叉树中的最大路径和 + +

路径 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 至多出现一次 。该路径 至少包含一个 节点,且不一定经过根节点。

+ +

路径和 是路径中各节点值的总和。

+ +

给你一个二叉树的根节点 root ,返回其 最大路径和

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,3]
+输出:6
+解释:最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6
+ +

示例 2:

+ +
+输入:root = [-10,9,20,null,null,15,7]
+输出:42
+解释:最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
+
+ +

 

+ +

提示:

+ +
    +
  • 树中节点数目范围是 [1, 3 * 104]
  • +
  • -1000 <= Node.val <= 1000
  • +
+ + +## template + +```java +class Solution { + int maxSum = Integer.MIN_VALUE; + + public int maxPathSum(TreeNode root) { + maxGain(root); + return maxSum; + } + + public int maxGain(TreeNode node) { + if (node == null) { + return 0; + } + + int leftGain = Math.max(maxGain(node.left), 0); + int rightGain = Math.max(maxGain(node.right), 0); + + int priceNewpath = node.val + leftGain + rightGain; + + maxSum = Math.max(maxSum, priceNewpath); + + return node.val + Math.max(leftGain, rightGain); + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1063f56a4aa15d232f015718fcca7f4ba0fff21d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-ceb30d29ac1c45d2862eb2224b876a65", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..57343b1d512ae3e4e7585952beda8e87f62ec9fb --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "3716f183d0af4ef89d973d535bc4bcb8", + "author": "csdn.net", + "keywords": "广度优先搜索,哈希表,字符串,回溯" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5e9df658fda74e08e816bdc84ce01e08c09fb451 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/126.exercises/solution.md" @@ -0,0 +1,173 @@ +# 单词接龙 II + +

按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:

+ +
+
+
    +
  • 每对相邻的单词之间仅有单个字母不同。
  • +
  • 转换过程中的每个单词 si1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。
  • +
  • sk == endWord
  • +
+ +

给你两个单词 beginWordendWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWordendWord最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 [beginWord, s1, s2, ..., sk] 的形式返回。

+ +

 

+ +

示例 1:

+ +
+输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
+输出:[["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
+解释:存在 2 种最短的转换序列:
+"hit" -> "hot" -> "dot" -> "dog" -> "cog"
+"hit" -> "hot" -> "lot" -> "log" -> "cog"
+
+ +

示例 2:

+ +
+输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
+输出:[]
+解释:endWord "cog" 不在字典 wordList 中,所以不存在符合要求的转换序列。
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= beginWord.length <= 7
  • +
  • endWord.length == beginWord.length
  • +
  • 1 <= wordList.length <= 5000
  • +
  • wordList[i].length == beginWord.length
  • +
  • beginWordendWordwordList[i] 由小写英文字母组成
  • +
  • beginWord != endWord
  • +
  • wordList 中的所有单词 互不相同
  • +
+
+
+ + +## template + +```java +class Solution { + public List> findLadders(String beginWord, String endWord, List wordList) { + + List> res = new ArrayList<>(); + if (wordList == null) + return res; + + Set dicts = new HashSet<>(wordList); + if (!dicts.contains(endWord)) + return res; + if (dicts.contains(beginWord)) + dicts.remove(beginWord); + + Set endList = new HashSet<>(), + beginList = new HashSet<>(); + + Map> map = new HashMap<>(); + beginList.add(beginWord); + endList.add(endWord); + bfs(map, beginList, endList, beginWord, endWord, dicts, false); + + List subList = new ArrayList<>(); + subList.add(beginWord); + dfs(map, res, subList, beginWord, endWord); + return res; + } + + void dfs(Map> map, + List> result, List subList, + String beginWord, String endWord) { + if (beginWord.equals(endWord)) { + result.add(new ArrayList<>(subList)); + return; + } + if (!map.containsKey(beginWord)) { + return; + } + for (String word : map.get(beginWord)) { + subList.add(word); + dfs(map, result, subList, word, endWord); + subList.remove(subList.size() - 1); + } + } + + void bfs(Map> map, Set beginList, Set endList, String beginWord, + String endWord, Set wordList, boolean reverse) { + if (beginList.size() == 0) + return; + wordList.removeAll(beginList); + boolean finish = false; + Set temp = new HashSet<>(); + for (String str : beginList) { + char[] charr = str.toCharArray(); + for (int chI = 0; chI < charr.length; chI++) { + char old = charr[chI]; + for (char ch = 'a'; ch <= 'z'; ch++) { + if (ch == old) + continue; + charr[chI] = ch; + String newstr = new String(charr); + if (!wordList.contains(newstr)) { + continue; + } + + if (endList.contains(newstr)) { + finish = true; + } else { + temp.add(newstr); + } + + String key = reverse ? newstr : str; + String value = reverse ? str : newstr; + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(value); + + } + charr[chI] = old; + } + } + if (!finish) { + if (temp.size() > endList.size()) { + bfs(map, endList, temp, beginWord, endWord, wordList, !reverse); + } else { + bfs(map, temp, endList, beginWord, endWord, wordList, reverse); + } + } + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..cd3d024f9cdd675ea0a2cf7baa3b8ae94933d5d3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e089934307ca4166a86555f671b153c9", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9c67506155a6c17fecd5fa74e52618dd9b3571d6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "868233572dc84c47b8588aa085e46b65", + "author": "csdn.net", + "keywords": "广度优先搜索,哈希表,字符串" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..1714c4726473bc99d9d6a2dbcddb35a050377010 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/127.exercises/solution.md" @@ -0,0 +1,176 @@ +# 单词接龙 + +

字典 wordList 中从单词 beginWord endWord转换序列 是一个按下述规格形成的序列:

+ +
    +
  • 序列中第一个单词是 beginWord
  • +
  • 序列中最后一个单词是 endWord
  • +
  • 每次转换只能改变一个字母。
  • +
  • 转换过程中的中间单词必须是字典 wordList 中的单词。
  • +
+ +

给你两个单词 beginWord endWord 和一个字典 wordList ,找到从 beginWord 到 endWord最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0。

+  + +

示例 1:

+ +
+输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
+输出:5
+解释:一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog", 返回它的长度 5。
+
+ +

示例 2:

+ +
+输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
+输出:0
+解释:endWord "cog" 不在字典中,所以无法进行转换。
+ +

 

+ +

提示:

+ +
    +
  • 1 <= beginWord.length <= 10
  • +
  • endWord.length == beginWord.length
  • +
  • 1 <= wordList.length <= 5000
  • +
  • wordList[i].length == beginWord.length
  • +
  • beginWordendWordwordList[i] 由小写英文字母组成
  • +
  • beginWord != endWord
  • +
  • wordList 中的所有字符串 互不相同
  • +
+ + +## template + +```java +public class Solution { + + HashMap> mapPositive = new HashMap>(); + HashMap> mapNegative = new HashMap>(); + HashMap mapVisit = new HashMap(); + + int result = Integer.MAX_VALUE; + + public int ladderLength(String beginWord, String endWord, List wordList) { + + for (int i = 0; i < wordList.size(); i++) { + addWordToMap(wordList.get(i)); + } + addWordToMap(beginWord); + mapVisit.put(beginWord, true); + findEnd(beginWord, endWord, 1); + if (result == Integer.MAX_VALUE) { + return 0; + } + return result; + } + + private void addWordToMap(String word) { + int length = word.length(); + if (length == 0) { + return; + } + char[] chars = word.toCharArray(); + for (int i = 0; i < length; i++) { + char now = chars[i]; + chars[i] = '*'; + String regex = new String(chars); + List list; + + if (mapPositive.containsKey(word)) { + list = mapPositive.get(word); + } else { + list = new ArrayList(); + mapPositive.put(word, list); + } + list.add(regex); + + if (mapNegative.containsKey(regex)) { + list = mapNegative.get(regex); + } else { + list = new ArrayList(); + mapNegative.put(regex, list); + } + list.add(word); + + chars[i] = now; + } + mapVisit.put(word, false); + } + + private void findEnd(String nowWord, String endWord, int nowStep) { + if (endWord.equals(nowWord)) { + if (nowStep < result) { + result = nowStep; + } + return; + } + List candidates = getCandidates(nowWord); + if (candidates == null || candidates.size() == 0) { + return; + } + for (int i = 0; i < candidates.size(); i++) { + String now = candidates.get(i); + mapVisit.put(now, true); + findEnd(now, endWord, nowStep + 1); + mapVisit.put(now, false); + } + } + + private List getCandidates(String word) { + List list = mapPositive.get(word); + if (list == null) { + return new ArrayList(); + } + Set set = new HashSet(); + for (int i = 0; i < list.size(); i++) { + String regex = list.get(i); + List regexList = mapNegative.get(regex); + if (regexList == null) { + continue; + } + for (int j = 0; j < regexList.size(); j++) { + String now = regexList.get(j); + if (mapVisit.get(now) == false) { + set.add(now); + } + } + } + List candidates = new ArrayList(); + for (String string : set) { + candidates.add(string); + } + return candidates; + } + +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0bd370dea04210444a5df286452528a33ab670a7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-ed5845b6ef1643d194e963750bc1cbcb", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d5cff9add01a1ebec1dcf1b6254c7a8b72080e79 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e337ab12306f468292112386f586615f", + "author": "csdn.net", + "keywords": "字符串,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..34b6238a9eb88b1fb0a0c4e32769ce397c4e2bf3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/132.exercises/solution.md" @@ -0,0 +1,103 @@ +# 分割回文串 II + +

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文。

+ +

返回符合要求的 最少分割次数

+ +
+
+

 

+ +

示例 1:

+ +
+输入:s = "aab"
+输出:1
+解释:只需一次分割就可将 s 分割成 ["aa","b"] 这样两个回文子串。
+
+ +

示例 2:

+ +
+输入:s = "a"
+输出:0
+
+ +

示例 3:

+ +
+输入:s = "ab"
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 2000
  • +
  • s 仅由小写英文字母组成
  • +
+
+
+ + +## template + +```java +class Solution { + public int minCut(String s) { + int n = s.length(); + boolean[][] g = new boolean[n][n]; + for (int i = 0; i < n; i++) { + Arrays.fill(g[i], true); + } + for (int i = n - 1; i >= 0; i--) { + for (int j = i + 1; j < n; j++) { + g[i][j] = (s.charAt(i) == s.charAt(j) && g[i + 1][j - 1]); + } + } + int[] f = new int[n]; + Arrays.fill(f, Integer.MAX_VALUE); + for (int i = 0; i < n; i++) { + if (g[0][i]) { + f[i] = 0; + } + for (int j = 0; j < i; j++) { + if (g[j + 1][i]) { + f[i] = Math.min(f[i], f[j] + 1); + } + } + } + return f[n - 1]; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..117a49df2e393c22d8724540c86194c3a587ccca --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-f21a163908cd48438eb653c2a944013b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7fffb04de8977967a915f2fdab825a1ab811a1fa --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d9550848c8644b4c8aec6795c0e05f32", + "author": "csdn.net", + "keywords": "贪心,数组" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..03f6f20d00682244e1e200d84e707b2d23366d9d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/135.exercises/solution.md" @@ -0,0 +1,96 @@ +# 分发糖果 + +

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

+ +

你需要按照以下要求,帮助老师给这些孩子分发糖果:

+ +
    +
  • 每个孩子至少分配到 1 个糖果。
  • +
  • 评分更高的孩子必须比他两侧的邻位孩子获得更多的糖果。
  • +
+ +

那么这样下来,老师至少需要准备多少颗糖果呢?

+ +

 

+ +

示例 1:

+ +
+输入:[1,0,2]
+输出:5
+解释:你可以分别给这三个孩子分发 2、1、2 颗糖果。
+
+ +

示例 2:

+ +
+输入:[1,2,2]
+输出:4
+解释:你可以分别给这三个孩子分发 1、2、1 颗糖果。
+     第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
+ + +## template + +```java +class Solution { + public int candy(int[] ratings) { + int len = ratings.length; + int[] left = new int[len]; + int[] right = new int[len]; + + left[0] = 1; + right[len - 1] = 1; + + for (int i = 1; i < len; i++) { + if (ratings[i] > ratings[i - 1]) { + left[i] = left[i - 1] + 1; + } else { + left[i] = 1; + } + } + + for (int i = len - 2; i >= 0; i--) { + if (ratings[i] > ratings[i + 1]) { + right[i] = right[i + 1] + 1; + } else { + right[i] = 1; + } + } + + int res = 0; + for (int i = 0; i < len; i++) { + res += Math.max(left[i], right[i]); + } + + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..31d92b348b1fbadcdf04b62c2be5a3d5486f5a84 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-20dd73b360dd44a3996b60964b566e60", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e7987869ee047738b291b48d4bad5853eb1fd9b7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "58347641730142b0b6d4a50156cdf7fc", + "author": "csdn.net", + "keywords": "字典树,记忆化搜索,哈希表,字符串,动态规划,回溯" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a82f1a19c361f3bc8c1480d4bf81a7cf5cb3848b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/140.exercises/solution.md" @@ -0,0 +1,125 @@ +# 单词拆分 II + +

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

+ +

说明:

+ +
    +
  • 分隔时可以重复使用字典中的单词。
  • +
  • 你可以假设字典中没有重复的单词。
  • +
+ +

示例 1:

+ +
输入:
+s = "catsanddog"
+wordDict = ["cat", "cats", "and", "sand", "dog"]
+输出:
+[
+  "cats and dog",
+  "cat sand dog"
+]
+
+ +

示例 2:

+ +
输入:
+s = "pineapplepenapple"
+wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
+输出:
+[
+  "pine apple pen apple",
+  "pineapple pen apple",
+  "pine applepen apple"
+]
+解释: 注意你可以重复使用字典中的单词。
+
+ +

示例 3:

+ +
输入:
+s = "catsandog"
+wordDict = ["cats", "dog", "sand", "and", "cat"]
+输出:
+[]
+
+ + +## template + +```java +class Solution { + public List wordBreak(String s, List wordDict) { + + List res = new ArrayList<>(); + int max = 0, min = Integer.MAX_VALUE; + Set set = new HashSet<>(); + for (String word : wordDict) { + set.add(word); + max = Integer.max(max, word.length()); + min = Integer.min(min, word.length()); + } + + boolean f[] = new boolean[s.length() + 1]; + f[0] = true; + for (int i = 1; i < s.length() + 1; i++) { + for (int j = Math.max(i - max, 0); j <= i - min; j++) { + if (f[j] && set.contains(s.substring(j, i))) { + f[i] = true; + break; + } + } + } + if (f[s.length()]) { + dfs(s, res, new StringBuilder(), set, 0, max, min); + } + return res; + } + + private void dfs(String s, List res, StringBuilder sb, Set set, int index, int max, int min) { + if (index == s.length()) { + sb.deleteCharAt(sb.length() - 1); + res.add(sb.toString()); + return; + } + String str; + int size; + for (int i = index + min; i <= s.length() && i <= index + max; i++) { + if (set.contains(str = s.substring(index, i))) { + size = sb.length(); + sb.append(str).append(' '); + dfs(s, res, sb, set, i, max, min); + sb.delete(size, sb.length()); + } + } + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ebf2cd5bb6e3dc2531b928769f63030583a7cefb --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-2c3d7c26bef54e678a413cc26bf70fb4", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9a235b2ee699c9dd78743811accbab3302efadf1 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "5d0ce61f218a4f88b1bf30d2672c1872", + "author": "csdn.net", + "keywords": "几何,哈希表,数学" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5e877534173ec580e3a7c9d56fd761897dbf4e67 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/149.exercises/solution.md" @@ -0,0 +1,104 @@ +# 直线上最多的点数 + +

给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。

+ +

 

+ +

示例 1:

+ +
+输入:points = [[1,1],[2,2],[3,3]]
+输出:3
+
+ +

示例 2:

+ +
+输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
+输出:4
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= points.length <= 300
  • +
  • points[i].length == 2
  • +
  • -104 <= xi, yi <= 104
  • +
  • points 中的所有点 互不相同
  • +
+ + +## template + +```java +class Solution { + public int maxPoints(int[][] points) { + int n = points.length; + if (n == 0) + return 0; + if (n == 1) + return 1; + int res = 0; + for (int i = 0; i < n - 1; i++) { + Map slope = new HashMap<>(); + int repeat = 0; + int tmp_max = 0; + for (int j = i + 1; j < n; j++) { + int dy = points[i][1] - points[j][1]; + int dx = points[i][0] - points[j][0]; + if (dy == 0 && dx == 0) { + repeat++; + continue; + } + int g = gcd(dy, dx); + if (g != 0) { + dy /= g; + dx /= g; + } + String tmp = String.valueOf(dy) + "/" + String.valueOf(dx); + slope.put(tmp, slope.getOrDefault(tmp, 0) + 1); + tmp_max = Math.max(tmp_max, slope.get(tmp)); + } + res = Math.max(res, repeat + tmp_max + 1); + } + return res; + } + + private int gcd(int dy, int dx) { + if (dx == 0) + return dy; + else + return gcd(dx, dy % dx); + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..27c149442d81c4cd110e9ce4f49b5f60171ce0ac --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-b75f35caae844eeeb6806341a826b574", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d3d4ae3fbfd4952a259194b0fa2fb752bffe8cd1 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "e3fc4f4e3bde4ff187d865b830fcb4df", + "author": "csdn.net", + "keywords": "数组,二分查找" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d762035c38c0a79acf5a52632d91ace6f2a28258 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/154.exercises/solution.md" @@ -0,0 +1,95 @@ +# 寻找旋转排序数组中的最小值 II + +已知一个长度为 n 的数组,预先按照升序排列,经由 1n旋转 后,得到输入数组。例如,原数组 nums = [0,1,4,4,5,6,7] 在变化后可能得到: +
    +
  • 若旋转 4 次,则可以得到 [4,5,6,7,0,1,4]
  • +
  • 若旋转 7 次,则可以得到 [0,1,4,4,5,6,7]
  • +
+ +

注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]]

+ +

给你一个可能存在 重复 元素值的数组 nums ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,3,5]
+输出:1
+
+ +

示例 2:

+ +
+输入:nums = [2,2,2,0,1]
+输出:0
+
+ +

 

+ +

提示:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 5000
  • +
  • -5000 <= nums[i] <= 5000
  • +
  • nums 原来是一个升序排序的数组,并进行了 1n 次旋转
  • +
+ +

 

+ +

进阶:

+ + + + +## template + +```java +class Solution { + public int findMin(int[] nums) { + int left = 0, right = nums.length - 1; + while (left < right) { + int mid = (left + right) / 2; + if (nums[mid] > nums[right]) + left = mid + 1; + else if (nums[mid] < nums[right]) + right = mid; + else + right = right - 1; + } + return nums[left]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..c2781b2ad5b88f4f0439461662727f96d106f0db --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-a67a9090308a408da4eaa9eb38e880bd", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..7e97db5d7c33ef0731773adfc8f21addbfc2a591 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "65d2cb1d967b46f09b68d428e0e51ef2", + "author": "csdn.net", + "keywords": "数组,桶排序,基数排序,排序" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..688c4fd93f736cdfde2c008fdcfb208ea543c974 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/164.exercises/solution.md" @@ -0,0 +1,72 @@ +# 最大间距 + +

给定一个无序的数组,找出数组在排序之后,相邻元素之间最大的差值。

+ +

如果数组元素个数小于 2,则返回 0。

+ +

示例 1:

+ +
输入: [3,6,9,1]
+输出: 3
+解释: 排序后的数组是 [1,3,6,9], 其中相邻元素 (3,6) 和 (6,9) 之间都存在最大差值 3。
+ +

示例 2:

+ +
输入: [10]
+输出: 0
+解释: 数组元素个数小于 2,因此返回 0。
+ +

说明:

+ +
    +
  • 你可以假设数组中所有元素都是非负整数,且数值在 32 位有符号整数范围内。
  • +
  • 请尝试在线性时间复杂度和空间复杂度的条件下解决此问题。
  • +
+ + +## template + +```java +class Solution { + public int maximumGap(int[] nums) { + int len = nums.length; + Arrays.sort(nums); + int temp = 0; + int res = 0; + if (len < 2) + return 0; + for (int i = 0; i < len - 1; i++) { + temp = nums[i + 1] - nums[i]; + res = Math.max(res, temp); + } + return res; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4285159f87f980634ede40b2ba7a500c3f6f96a3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-52372e92c9d748d3825c04ce1388ade2", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a76127737d72bb35525169de49be926f47e2746d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "b71f25a8bcde4801b81a00fd6be1d380", + "author": "csdn.net", + "keywords": "数组,动态规划,矩阵" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c1ff99dfca46abd4a0f26eac429a291b6428835d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/174.exercises/solution.md" @@ -0,0 +1,113 @@ +# 地下城游戏 + + + +

一些恶魔抓住了公主(P)并将她关在了地下城的右下角。地下城是由 M x N 个房间组成的二维网格。我们英勇的骑士(K)最初被安置在左上角的房间里,他必须穿过地下城并通过对抗恶魔来拯救公主。

+ +

骑士的初始健康点数为一个正整数。如果他的健康点数在某一时刻降至 0 或以下,他会立即死亡。

+ +

有些房间由恶魔守卫,因此骑士在进入这些房间时会失去健康点数(若房间里的值为负整数,则表示骑士将损失健康点数);其他房间要么是空的(房间里的值为 0),要么包含增加骑士健康点数的魔法球(若房间里的值为正整数,则表示骑士将增加健康点数)。

+ +

为了尽快到达公主,骑士决定每次只向右或向下移动一步。

+ +

 

+ +

编写一个函数来计算确保骑士能够拯救到公主所需的最低初始健康点数。

+ +

例如,考虑到如下布局的地下城,如果骑士遵循最佳路径 右 -> 右 -> 下 -> 下,则骑士的初始健康点数至少为 7

+ + + + + + + + + + + + + + + + + +
-2 (K)-33
-5-101
1030-5 (P)
+ + +

 

+ +

说明:

+ +
    +
  • +

    骑士的健康点数没有上限。

    +
  • +
  • 任何房间都可能对骑士的健康点数造成威胁,也可能增加骑士的健康点数,包括骑士进入的左上角房间以及公主被监禁的右下角房间。
  • +
+ +## template + +```java + +class Solution { + public int calculateMinimumHP(int[][] dungeon) { + int row = dungeon.length; + int col = dungeon[0].length; + + int[][] dp = new int[row][col]; + for (int i = row - 1; i >= 0; i--) { + for (int j = col - 1; j >= 0; j--) { + if (i == row - 1 && j == col - 1) { + dp[i][j] = Math.max(1, 1 - dungeon[i][j]); + } else if (i == row - 1) { + dp[i][j] = Math.max(1, dp[i][j + 1] - dungeon[i][j]); + } else if (j == col - 1) { + dp[i][j] = Math.max(1, dp[i + 1][j] - dungeon[i][j]); + } else { + dp[i][j] = Math.max(1, Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]); + } + } + } + return dp[0][0]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..e5c1749751a17d5d5da716accdbd5556cae987de --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-6fad0c8b314e47e9893ea704d5f1d601", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..49f7504f4fe944af9d79193a6d587476efdcf19b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "ec3e59f7d600494180e6584fe82b5ce2", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..a87799eb8ac453fad218e90b90e145b1badac720 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/188.exercises/solution.md" @@ -0,0 +1,98 @@ +# 买卖股票的最佳时机 IV + +

给定一个整数数组 prices ,它的第 i 个元素 prices[i] 是一支给定的股票在第 i 天的价格。

+ +

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。

+ +

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

+ +

 

+ +

示例 1:

+ +
+输入:k = 2, prices = [2,4,1]
+输出:2
+解释:在第 1 天 (股票价格 = 2) 的时候买入,在第 2 天 (股票价格 = 4) 的时候卖出,这笔交易所能获得利润 = 4-2 = 2 。
+ +

示例 2:

+ +
+输入:k = 2, prices = [3,2,6,5,0,3]
+输出:7
+解释:在第 2 天 (股票价格 = 2) 的时候买入,在第 3 天 (股票价格 = 6) 的时候卖出, 这笔交易所能获得利润 = 6-2 = 4 。
+     随后,在第 5 天 (股票价格 = 0) 的时候买入,在第 6 天 (股票价格 = 3) 的时候卖出, 这笔交易所能获得利润 = 3-0 = 3 。
+ +

 

+ +

提示:

+ +
    +
  • 0 <= k <= 100
  • +
  • 0 <= prices.length <= 1000
  • +
  • 0 <= prices[i] <= 1000
  • +
+ + +## template + +```java +class Solution { + public int maxProfit(int k, int[] prices) { + if (k < 1) + return 0; + if (k >= prices.length / 2) + return greedy(prices); + int[][] t = new int[k][2]; + for (int i = 0; i < k; ++i) + t[i][0] = Integer.MIN_VALUE; + for (int p : prices) { + t[0][0] = Math.max(t[0][0], -p); + t[0][1] = Math.max(t[0][1], t[0][0] + p); + for (int i = 1; i < k; ++i) { + + t[i][0] = Math.max(t[i][0], t[i - 1][1] - p); + t[i][1] = Math.max(t[i][1], t[i][0] + p); + } + } + return t[k - 1][1]; + } + + private int greedy(int[] prices) { + int max = 0; + for (int i = 1; i < prices.length; ++i) { + if (prices[i] > prices[i - 1]) + max += prices[i] - prices[i - 1]; + } + return max; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..410b30773dde0a00f4087769bd2e7f5385b4d9e5 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e5dd8fa61e8143a6bbe35efee6d5041e", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..d0c4b6ae8f874c8c50b609f02caf13e58932d987 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "6b4aff8c6fef4c18ae919591114362ca", + "author": "csdn.net", + "keywords": "字典树,数组,字符串,回溯,矩阵" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8062a438d5695a18718e8d68636842ff300d40c0 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/212.exercises/solution.md" @@ -0,0 +1,145 @@ +# 单词搜索 II + +

给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words,找出所有同时在二维网格和字典中出现的单词。

+ +

单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。

+ +

 

+ +

示例 1:

+ +
+输入:board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
+输出:["eat","oath"]
+
+ +

示例 2:

+ +
+输入:board = [["a","b"],["c","d"]], words = ["abcb"]
+输出:[]
+
+ +

 

+ +

提示:

+ +
    +
  • m == board.length
  • +
  • n == board[i].length
  • +
  • 1 <= m, n <= 12
  • +
  • board[i][j] 是一个小写英文字母
  • +
  • 1 <= words.length <= 3 * 104
  • +
  • 1 <= words[i].length <= 10
  • +
  • words[i] 由小写英文字母组成
  • +
  • words 中的所有字符串互不相同
  • +
+ + +## template + +```java + +class Solution { + static int[][] d = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; + static Map notExistWords = new HashMap<>(); + + public List findWords(char[][] board, String[] words) { + List ans = new ArrayList<>(); + Arrays.sort(words); + notExistWords.clear(); + + for (int i = 0; i < words.length; i++) { + String word = words[i]; + + if (i > 0 && word.equals(words[i - 1])) + continue; + + boolean notExistFlag = false; + for (int j = 1; j < word.length(); j++) { + if (notExistWords.containsKey(word.substring(0, j + 1))) { + notExistFlag = true; + break; + } + } + if (notExistFlag) + continue; + + if (exist(board, word)) { + ans.add(word); + } else { + notExistWords.put(word, false); + } + } + + return ans; + } + + public boolean exist(char[][] board, String word) { + int m = board.length; + if (m == 0) + return false; + int n = board[0].length; + if (n == 0) + return false; + if (word.equals("")) + return true; + boolean[][] f = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (word.charAt(0) == board[i][j]) { + f[i][j] = true; + boolean flag = dfs(i, j, 1, board, word, m, n, f); + if (flag) + return true; + f[i][j] = false; + } + } + } + return false; + } + + private boolean dfs(int i, int j, int k, char[][] board, String word, int m, int n, boolean[][] f) { + if (k == word.length()) { + return true; + } + for (int l = 0; l < 4; l++) { + if (i + d[l][0] < m && j + d[l][1] < n && i + d[l][0] >= 0 && j + d[l][1] >= 0 + && board[i + d[l][0]][j + d[l][1]] == word.charAt(k) && !f[i + d[l][0]][j + d[l][1]]) { + f[i + d[l][0]][j + d[l][1]] = true; + boolean flag = dfs(i + d[l][0], j + d[l][1], k + 1, board, word, m, n, f); + if (flag) + return true; + f[i + d[l][0]][j + d[l][1]] = false; + } + } + return false; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..92309ac6a8864587a943bfccc815093eb311e3e3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-0b51fd06b64e4774bc8954dc5a55846f", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1c5eec3d8bbbfd01060b62927b064c6ada8432ba --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "641811da7e134206a7688959d7c62107", + "author": "csdn.net", + "keywords": "字符串,字符串匹配,哈希函数,滚动哈希" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c7ab712c0e5605ebafe15b19364c4037d6147c09 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/214.exercises/solution.md" @@ -0,0 +1,87 @@ +# 最短回文串 + +

给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。

+ +

 

+ +

示例 1:

+ +
+输入:s = "aacecaaa"
+输出:"aaacecaaa"
+
+ +

示例 2:

+ +
+输入:s = "abcd"
+输出:"dcbabcd"
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= s.length <= 5 * 104
  • +
  • s 仅由小写英文字母组成
  • +
+ + +## template + +```java +class Solution { + public static String shortestPalindrome(String s) { + StringBuilder r = new StringBuilder(s).reverse(); + String str = s + "#" + r; + int[] next = next(str); + + String prefix = r.substring(0, r.length() - next[str.length()]); + return prefix + s; + } + + private static int[] next(String P) { + int[] next = new int[P.length() + 1]; + next[0] = -1; + int k = -1; + int i = 1; + + while (i < next.length) { + if (k == -1 || P.charAt(k) == P.charAt(i - 1)) { + next[i++] = ++k; + } else { + k = next[k]; + } + } + return next; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..2bae41a9145003467df83b645903339e0e16a63e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4bceaef36be24391aa79989ab4885413", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..1b8fa6f714449da6170ea6dbb0c82e3e916c02be --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "35aededae12747cd833069bb653f73c6", + "author": "csdn.net", + "keywords": "树状数组,线段树,数组,分治,有序集合,扫描线,堆(优先队列)" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d94cce264e984990d923a5c6c9e06572e02e9527 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/218.exercises/solution.md" @@ -0,0 +1,106 @@ +# 天际线问题 + +

城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的 天际线

+ +

每个建筑物的几何信息由数组 buildings 表示,其中三元组 buildings[i] = [lefti, righti, heighti] 表示:

+ +
    +
  • lefti 是第 i 座建筑物左边缘的 x 坐标。
  • +
  • righti 是第 i 座建筑物右边缘的 x 坐标。
  • +
  • heighti 是第 i 座建筑物的高度。
  • +
+ +

天际线 应该表示为由 “关键点” 组成的列表,格式 [[x1,y1],[x2,y2],...] ,并按 x 坐标 进行 排序关键点是水平线段的左端点。列表中最后一个点是最右侧建筑物的终点,y 坐标始终为 0 ,仅用于标记天际线的终点。此外,任何两个相邻建筑物之间的地面都应被视为天际线轮廓的一部分。

+ +

注意:输出天际线中不得有连续的相同高度的水平线。例如 [...[2 3], [4 5], [7 5], [11 5], [12 7]...] 是不正确的答案;三条高度为 5 的线应该在最终输出中合并为一个:[...[2 3], [4 5], [12 7], ...]

+ +

 

+ +

示例 1:

+ +
+输入:buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
+输出:[[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
+解释:
+图 A 显示输入的所有建筑物的位置和高度,
+图 B 显示由这些建筑物形成的天际线。图 B 中的红点表示输出列表中的关键点。
+ +

示例 2:

+ +
+输入:buildings = [[0,2,3],[2,5,3]]
+输出:[[0,3],[5,0]]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= buildings.length <= 104
  • +
  • 0 <= lefti < righti <= 231 - 1
  • +
  • 1 <= heighti <= 231 - 1
  • +
  • buildingslefti 非递减排序
  • +
+ + +## template + +```java +class Solution { + public List> getSkyline(int[][] buildings) { + int n = buildings.length, m = n << 1; + List> ans = new ArrayList>(); + + int[] boundaries = new int[m]; + for (int i = 0; i < n; i++) { + boundaries[i << 1] = buildings[i][0]; + boundaries[(i << 1) + 1] = buildings[i][1]; + } + Arrays.sort(boundaries); + + PriorityQueue pq = new PriorityQueue((a, b) -> b[1] - a[1]); + int building = 0; + for (int i = 0; i < m; i++) { + if (i > 0 && boundaries[i - 1] == boundaries[i]) + continue; + while (building < n && buildings[building][0] <= boundaries[i]) + pq.offer(new int[] { buildings[building][1], buildings[building++][2] }); + while (!pq.isEmpty() && pq.peek()[0] <= boundaries[i]) + pq.poll(); + int height = (pq.isEmpty()) ? 0 : pq.peek()[1]; + if (ans.size() == 0 || height != ans.get(ans.size() - 1).get(1)) + ans.add(Arrays.asList(boundaries[i], height)); + } + + return ans; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..ecdf7d37c2c10171fb7df782676b0e0fd04ea373 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-09db552af41e4aca920a576c4b507f7c", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..c7039ddc0e73654fd574f9b2829aff0b6ca9af6c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "cda18ca422614f09b5903abd205f0310", + "author": "csdn.net", + "keywords": "栈,递归,数学,字符串" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..274520e1a88699374e736f986c42d1794ac4ffc8 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/224.exercises/solution.md" @@ -0,0 +1,108 @@ +# 基本计算器 + +

给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。

+ +

 

+ +

示例 1:

+ +
+输入:s = "1 + 1"
+输出:2
+
+ +

示例 2:

+ +
+输入:s = " 2-1 + 2 "
+输出:3
+
+ +

示例 3:

+ +
+输入:s = "(1+(4+5+2)-3)+(6+8)"
+输出:23
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 3 * 105
  • +
  • s 由数字、'+''-''('')'、和 ' ' 组成
  • +
  • s 表示一个有效的表达式
  • +
+ + +## template + +```java +class Solution { + public int calculate(String s) { + int res = 0, num = 0; + Stack sta = new Stack(); + char preSign = '+'; + int len = s.length(); + + for (int i = 0; i < len; i++) { + char ch = s.charAt(i); + + if (Character.isDigit(ch)) { + num = num * 10 + ch - '0'; + } + + if ((!Character.isDigit(ch) && ch != ' ') || i == len - 1) { + if (preSign == '+') { + sta.push(num); + } else if (preSign == '-') { + sta.push(-1 * num); + } else if (preSign == '*') { + sta.push(num * sta.pop()); + } else if (preSign == '/') { + sta.push(sta.pop() / num); + } + + num = 0; + preSign = ch; + } + } + + while (!sta.isEmpty()) { + res += sta.pop(); + } + + return res; + + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..416e802109c88545219ccba36d68e20c832d8a12 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-67904c6c97ef4410ad8fd1841d8a89f2", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..b733927ed9d621c3b1f52fd44ca6a54958876aac --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "a7a6461d1d16435bb3e3a3c41151846e", + "author": "csdn.net", + "keywords": "递归,数学,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..458f13d31a92cbaf33cad98113cec29af9fb0bce --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/233.exercises/solution.md" @@ -0,0 +1,84 @@ +# 数字 1 的个数 + +

给定一个整数 n,计算所有小于等于 n 的非负整数中数字 1 出现的个数。

+ +

 

+ +

示例 1:

+ +
+输入:n = 13
+输出:6
+
+ +

示例 2:

+ +
+输入:n = 0
+输出:0
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= n <= 109
  • +
+ + +## template + +```java + +class Solution { + public int countDigitOne(int n) { + if (n < 1) + return 0; + int len = getLenOfNum(n); + if (len == 1) + return 1; + int tmp = (int) Math.pow(10, len - 1); + int first = n / tmp; + int firstOneNum = first == 1 ? n % tmp + 1 : tmp; + int otherOneNUm = first * (len - 1) * (tmp / 10); + return firstOneNum + otherOneNUm + countDigitOne(n % tmp); + } + + private int getLenOfNum(int n) { + int len = 0; + while (n != 0) { + len++; + n /= 10; + } + return len; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..06edda873227f6692cf5b0128c636dfb6eda83bd --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-17dd079975ee4a248cc134c6248803b4", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..27be98cbcf7239cf57639f0afeeebe2eec14f6af --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "08a9984f907642ab82562b6825d11de4", + "author": "csdn.net", + "keywords": "队列,数组,滑动窗口,单调队列,堆(优先队列)" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..c6cfadda61b466ca55d957e748cf460509da0981 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/239.exercises/solution.md" @@ -0,0 +1,126 @@ +# 滑动窗口最大值 + +

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。

+ +

返回滑动窗口中的最大值。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,3,-1,-3,5,3,6,7], k = 3
+输出:[3,3,5,5,6,7]
+解释:
+滑动窗口的位置                最大值
+---------------               -----
+[1  3  -1] -3  5  3  6  7       3
+ 1 [3  -1  -3] 5  3  6  7       3
+ 1  3 [-1  -3  5] 3  6  7       5
+ 1  3  -1 [-3  5  3] 6  7       5
+ 1  3  -1  -3 [5  3  6] 7       6
+ 1  3  -1  -3  5 [3  6  7]      7
+
+ +

示例 2:

+ +
+输入:nums = [1], k = 1
+输出:[1]
+
+ +

示例 3:

+ +
+输入:nums = [1,-1], k = 1
+输出:[1,-1]
+
+ +

示例 4:

+ +
+输入:nums = [9,11], k = 2
+输出:[11]
+
+ +

示例 5:

+ +
+输入:nums = [4,-2], k = 2
+输出:[4]
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
  • 1 <= k <= nums.length
  • +
+ + +## template + +```java + +class Solution { + + public int[] maxSlidingWindow(int[] nums, int k) { + if (nums == null || nums.length < 0 || k <= 0 || k == 1) + return nums; + + queue = new PriorityQueue<>(k, new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o2 - o1; + } + }); + + int[] max = new int[nums.length - k + 1]; + for (int i = 0; i < nums.length; i++) { + + if (i < k - 1) { + queue.add(nums[i]); + } else if (i == k - 1) { + queue.add(nums[i]); + max[0] = queue.peek(); + } else { + + queue.remove(nums[i - k]); + queue.add(nums[i]); + max[i - k + 1] = queue.peek(); + } + } + + return max; + + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..46b2b0a7185971457a688ddb4ba5e88dc01b8376 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-e8251e8978814b88bc5eabff8637a5e9", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..9fb037c311944b082494cff8f961a546b80a6ba7 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "76c0f96d4f6a442481925eb30c1bd079", + "author": "csdn.net", + "keywords": "递归,数学,字符串" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8b8fd20e19f03e19f9f669ae86f35cf3b5e34b60 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/273.exercises/solution.md" @@ -0,0 +1,137 @@ +# 整数转换英文表示 + +

将非负整数 num 转换为其对应的英文表示。

+ +

 

+ +

示例 1:

+ +
+输入:num = 123
+输出:"One Hundred Twenty Three"
+
+ +

示例 2:

+ +
+输入:num = 12345
+输出:"Twelve Thousand Three Hundred Forty Five"
+
+ +

示例 3:

+ +
+输入:num = 1234567
+输出:"One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
+
+ +

示例 4:

+ +
+输入:num = 1234567891
+输出:"One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= num <= 231 - 1
  • +
+ + +## template + +```java +class Solution { + + String[] withinTwentyNum = new String[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", + "Nine", "Ten", + "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; + + String[] theWholeTen = new String[] { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", + "Ninety" }; + + String[] unit = new String[] { "", "Thousand", "Million", "Billion" }; + + public String numberToWords(int num) { + if (num == 0) { + return withinTwentyNum[num]; + } + StringBuilder sb = new StringBuilder(); + int unitCount = 0; + int temp; + + String[] words = new String[32]; + int wordIndex = 0; + + for (int n = num; num > 0; unitCount++) { + + n = num % 1000; + num /= 1000; + if (n == 0) { + continue; + } + + words[wordIndex++] = unit[unitCount]; + temp = n % 100; + if (n % 100 > 0) { + + if (temp >= 20) { + if (temp % 10 > 0) { + + words[wordIndex++] = withinTwentyNum[temp % 10]; + } + + words[wordIndex++] = theWholeTen[temp / 10]; + + } else { + + words[wordIndex++] = withinTwentyNum[temp]; + } + } + temp = n / 100; + if (temp > 0) { + + words[wordIndex++] = "Hundred"; + words[wordIndex++] = withinTwentyNum[temp]; + } + } + + for (int index = wordIndex - 1; index >= 0; index--) { + sb.append(words[index]).append(" "); + } + + return sb.toString().trim(); + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..811a301be1525b9d0f7a9407fd9f7404e0d4278c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-d856be5109cd4a0e88671cfda54c943d", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4fd6f4d529458a32aa92fc9a33b2e516ac3a97b2 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "0eace60236ae48c98b4f3289166eaa40", + "author": "csdn.net", + "keywords": "数学,字符串,回溯" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..284c2b976cf1f97070dbd38462d0f94fb5558e83 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/282.exercises/solution.md" @@ -0,0 +1,126 @@ +# 给表达式添加运算符 + +

给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+- 或 * ,返回所有能够得到目标值的表达式。

+ +

 

+ +

示例 1:

+ +
+输入: num = "123", target = 6
+输出: ["1+2+3", "1*2*3"] 
+
+ +

示例 2:

+ +
+输入: num = "232", target = 8
+输出: ["2*3+2", "2+3*2"]
+ +

示例 3:

+ +
+输入: num = "105", target = 5
+输出: ["1*0+5","10-5"]
+ +

示例 4:

+ +
+输入: num = "00", target = 0
+输出: ["0+0", "0-0", "0*0"]
+
+ +

示例 5:

+ +
+输入: num = "3456237490", target = 9191
+输出: []
+ +

 

+ +

提示:

+ +
    +
  • 1 <= num.length <= 10
  • +
  • num 仅含数字
  • +
  • -231 <= target <= 231 - 1
  • +
+ + +## template + +```java +class Solution { + int n; + String num; + List ans; + int target; + + public List addOperators(String num, int target) { + this.n = num.length(); + this.num = num; + this.target = target; + this.ans = new ArrayList(); + StringBuffer expr = new StringBuffer(); + dfs(expr, 0, 0, 0); + return ans; + } + + public void dfs(StringBuffer sba, long sum, long prepareMultiply, int index) { + StringBuffer sb = new StringBuffer(sba); + if (index == n) { + if (sum == target) { + ans.add(sb.toString()); + } + return; + } + int sign = sb.length(); + if (index > 0) { + sb.append("0"); + } + long val = 0; + for (int i = index; i < n && (i == index || num.charAt(index) != '0'); i++) { + val = val * 10 + (num.charAt(i) - '0'); + sb.append(num.charAt(i)); + if (index == 0) { + dfs(sb, val, val, i + 1); + continue; + } + sb.setCharAt(sign, '+'); + dfs(sb, sum + val, val, i + 1); + sb.setCharAt(sign, '-'); + dfs(sb, sum - val, -val, i + 1); + sb.setCharAt(sign, '*'); + dfs(sb, sum - prepareMultiply + prepareMultiply * val, prepareMultiply * val, i + 1); + } + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..95c7520c7f7fa87fd5c039a4e8505086cf4ce438 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-30c18adba15d4819b2fc5bb47e9b4136", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..63edcdc64fb12a817c987942fb3a522fd2eb23a0 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "2ab425e26dd94bad91dd32f7dc7884b7", + "author": "csdn.net", + "keywords": "设计,双指针,数据流,排序,堆(优先队列)" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ccc9f1ba8378a7ec4051ccf38df14a0a894a12c4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/295.exercises/solution.md" @@ -0,0 +1,99 @@ +# 数据流的中位数 + +

中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。

+ +

例如,

+ +

[2,3,4] 的中位数是 3

+ +

[2,3] 的中位数是 (2 + 3) / 2 = 2.5

+ +

设计一个支持以下两种操作的数据结构:

+ +
    +
  • void addNum(int num) - 从数据流中添加一个整数到数据结构中。
  • +
  • double findMedian() - 返回目前所有元素的中位数。
  • +
+ +

示例:

+ +
addNum(1)
+addNum(2)
+findMedian() -> 1.5
+addNum(3) 
+findMedian() -> 2
+ +

进阶:

+ +
    +
  1. 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法?
  2. +
  3. 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法?
  4. +
+ + +## template + +```java +class MedianFinder { + + PriorityQueue min; + PriorityQueue max; + + /** initialize your data structure here. */ + public MedianFinder() { + min = new PriorityQueue<>(); + max = new PriorityQueue<>((a, b) -> { + return b - a; + }); + } + + public void addNum(int num) { + max.add(num); + min.add(max.remove()); + if (min.size() > max.size()) + max.add(min.remove()); + } + + public double findMedian() { + if (max.size() == min.size()) + return (max.peek() + min.peek()) / 2.0; + else + return max.peek(); + } +} + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder obj = new MedianFinder(); + * obj.addNum(num); + * double param_2 = obj.findMedian(); + */ + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..3d95d09e5e0ed9396332ae5ab15690d75e154903 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-5f27d1e940f2478cb8f7d44917237e07", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..ccb6ce33e9dd1a5b10b79cd120d2cde7e24ade9e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d8c384b178944c5ba45bd1cb17f9927e", + "author": "csdn.net", + "keywords": "树,深度优先搜索,广度优先搜索,设计,字符串,二叉树" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..5788761a0d838a8ab0f7e2c32b5b906f3f9d13a4 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/297.exercises/solution.md" @@ -0,0 +1,139 @@ +# 二叉树的序列化与反序列化 + +

序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。

+ +

请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。

+ +

提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。

+ +

 

+ +

示例 1:

+ +
+输入:root = [1,2,3,null,null,4,5]
+输出:[1,2,3,null,null,4,5]
+
+ +

示例 2:

+ +
+输入:root = []
+输出:[]
+
+ +

示例 3:

+ +
+输入:root = [1]
+输出:[1]
+
+ +

示例 4:

+ +
+输入:root = [1,2]
+输出:[1,2]
+
+ +

 

+ +

提示:

+ +
    +
  • 树中结点数在范围 [0, 104]
  • +
  • -1000 <= Node.val <= 1000
  • +
+ + +## template + +```java +class Solution { + public String serialize(TreeNode root) { + if (root == null) + return "[]"; + ArrayList queue = new ArrayList<>(); + queue.add(root); + for (int i = 0; i < queue.size(); i++) { + TreeNode node = queue.get(i); + if (node == null) + continue; + if (node.left != null) + queue.add(node.left); + if (node.right != null) + queue.add(node.right); + } + while (queue.get(queue.size() - 1) == null) { + queue.remove(queue.size() - 1); + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(queue.get(0)); + for (int i = 1; i < queue.size(); i++) { + if (queue.get(i) == null) { + sb.append(","); + sb.append("null"); + } else { + sb.append(","); + sb.append(queue.get(i).val); + } + } + sb.append("]"); + return sb.toString(); + } + + public TreeNode deserialize(String s) { + if (data.equals("[]")) + return null; + String[] vals = data.substring(1, data.length() - 1).split(","); + ArrayList queue = new ArrayList<>(); + TreeNode root = new TreeNode(Integer.parseInt(vals[0])); + queue.add(root); + int index = 0; + boolean left = true; + for (int i = 1; i < vals.length; i++) { + if (!vals[i].equals("null")) { + TreeNode node = new TreeNode(Integer.parseInt(vals[i])); + if (left) { + queue.get(index).left = node; + } else { + queue.get(index).right = node; + } + } + if (!left) + index++; + left = !left; + } + return root; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..75b896fe57e83a8ae8fd87a7b46d1eefa77ee995 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-bcfb6b6a12354ecc8095b4dcbae73d74", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..29e7c42e144403ba547902959462bbafc386aaf3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "02180dc4ed1d4228a5428ffaebb367df", + "author": "csdn.net", + "keywords": "广度优先搜索,字符串,回溯" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..91cb72a92366b01915bd8b221ad072981b8c6f61 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/301.exercises/solution.md" @@ -0,0 +1,115 @@ +# 删除无效的括号 + +

给你一个由若干括号和字母组成的字符串 s ,删除最小数量的无效括号,使得输入的字符串有效。

+ +

返回所有可能的结果。答案可以按 任意顺序 返回。

+ +

 

+ +

示例 1:

+ +
+输入:s = "()())()"
+输出:["(())()","()()()"]
+
+ +

示例 2:

+ +
+输入:s = "(a)())()"
+输出:["(a())()","(a)()()"]
+
+ +

示例 3:

+ +
+输入:s = ")("
+输出:[""]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= s.length <= 25
  • +
  • s 由小写英文字母以及括号 '('')' 组成
  • +
  • s 中至多含 20 个括号
  • +
+ + +## template + +```java +public class Solution { + + private Set set; + + private String input; + + private int maxLen = 0; + + public List removeInvalidParentheses(String s) { + set = new HashSet<>(); + input = s; + removeInvalidParentheses(0, "", 0, 0); + return new ArrayList<>(set); + } + + private void removeInvalidParentheses(int index, String valid, int leftCount, int rightCount) { + if (leftCount < rightCount) { + return; + } + if (index == input.length()) { + if (leftCount == rightCount) { + if (maxLen < valid.length()) { + maxLen = valid.length(); + set.clear(); + set.add(valid); + } else if (maxLen == valid.length()) { + set.add(valid); + } + } + return; + } + char c = input.charAt(index); + if (c == '(') { + removeInvalidParentheses(index + 1, valid, leftCount, rightCount); + removeInvalidParentheses(index + 1, valid + c, leftCount + 1, rightCount); + } else if (c == ')') { + removeInvalidParentheses(index + 1, valid, leftCount, rightCount); + removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount + 1); + } else { + removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount); + } + } + +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..4b6557e3a7d9a13664d3480513ad547b8aef6c3d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-b52b3fe1d35c4afc92cd3a0863488f2b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0bab99a731e74dc8ad67d5f77a63c49149c5443f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "bef97869141f49ddb448bd2b030c3ddc", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..43e8f698b8d920df07322ce76034786cde4562fd --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/312.exercises/solution.md" @@ -0,0 +1,86 @@ +# 戳气球 + +

n 个气球,编号为0n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。

+ +

现在要求你戳破所有的气球。戳破第 i 个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1] 枚硬币。 这里的 i - 1i + 1 代表和 i 相邻的两个气球的序号。如果 i - 1i + 1 超出了数组的边界,那么就当它是一个数字为 1 的气球。

+ +

求所能获得硬币的最大数量。

+ +

 

+示例 1: + +
+输入:nums = [3,1,5,8]
+输出:167
+解释:
+nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
+coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167
+ +

示例 2:

+ +
+输入:nums = [1,5]
+输出:10
+
+ +

 

+ +

提示:

+ +
    +
  • n == nums.length
  • +
  • 1 <= n <= 500
  • +
  • 0 <= nums[i] <= 100
  • +
+ + +## template + +```java + +public class Solution { + public int maxCoins(int[] iNums) { + int n = iNums.length; + int[] nums = new int[n + 2]; + for (int i = 0; i < n; i++) + nums[i + 1] = iNums[i]; + nums[0] = nums[n + 1] = 1; + int[][] dp = new int[n + 2][n + 2]; + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n - k + 1; i++) { + int j = i + k - 1; + for (int x = i; x <= j; x++) { + dp[i][j] = Math.max(dp[i][j], dp[i][x - 1] + nums[i - 1] * nums[x] * nums[j + 1] + dp[x + 1][j]); + } + } + } + return dp[1][n]; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1a75b0df45fa52150c687d5f6c749f64771cb401 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-4f241557aa794312a6862d10a379bf76", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..664ddac40d7cc10674c379a8a96c1f506ca85739 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "906c31be650743cb9439f1fd1b9e596d", + "author": "csdn.net", + "keywords": "树状数组,线段树,数组,二分查找,分治,有序集合,归并排序" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..f3f6ad66c1252084fbece78eb2fae9da16e18b74 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/315.exercises/solution.md" @@ -0,0 +1,128 @@ +# 计算右侧小于当前元素的个数 + +

给你`一个整数数组 nums ,按要求返回一个新数组 counts 。数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于 nums[i] 的元素的数量。

+ +

 

+ +

示例 1:

+ +
+输入:nums = [5,2,6,1]
+输出:[2,1,1,0] 
+解释:
+5 的右侧有 2 个更小的元素 (2 和 1)
+2 的右侧仅有 1 个更小的元素 (1)
+6 的右侧有 1 个更小的元素 (1)
+1 的右侧有 0 个更小的元素
+
+ +

示例 2:

+ +
+输入:nums = [-1]
+输出:[0]
+
+ +

示例 3:

+ +
+输入:nums = [-1,-1]
+输出:[0,0]
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -104 <= nums[i] <= 104
  • +
+ + +## template + +```java +class Solution { + public static List countSmaller(int[] nums) { + if (nums.length == 0) { + return new ArrayList<>(); + } + int min = Integer.MAX_VALUE; + for (int value : nums) { + if (value < min) { + min = value; + } + } + for (int i = 0; i < nums.length; i++) { + nums[i] = nums[i] - min + 1; + } + + int max = Integer.MIN_VALUE; + for (int value : nums) { + if (value > max) { + max = value; + } + } + + int[] BITree = new int[max + 1]; + BITree[0] = 0; + int[] countArr = new int[nums.length]; + for (int i = nums.length - 1; i >= 0; i--) { + int count = getSum(nums[i] - 1, BITree); + countArr[i] = count; + update(nums[i], BITree); + } + List result = new ArrayList<>(); + for (int value : countArr) { + result.add(value); + } + return result; + } + + public static int getSum(int value, int[] BITree) { + int sum = 0; + while (value > 0) { + sum += BITree[value]; + value -= (value & -value); + } + return sum; + } + + public static void update(int value, int[] BITree) { + while (value <= BITree.length - 1) { + BITree[value] += 1; + value += (value & -value); + } + } + +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..8f380cc7632b770f6c4749eece945254a6092d2d --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-49a3392c40a24861a78109217e39922b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4f503a1cfd85712952dab97b1a4f93ebab9736d6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "8dd3243f9a7549c6b27e3381a52df3d9", + "author": "csdn.net", + "keywords": "栈,贪心,单调栈" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..ccbe8536993c1c87dad4c137153c2a53839cce66 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/321.exercises/solution.md" @@ -0,0 +1,113 @@ +# 拼接最大数 + +

给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。

+ +

求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。

+ +

说明: 请尽可能地优化你算法的时间和空间复杂度。

+ +

示例 1:

+ +
输入:
+nums1 = [3, 4, 6, 5]
+nums2 = [9, 1, 2, 5, 8, 3]
+k = 5
+输出:
+[9, 8, 6, 5, 3]
+ +

示例 2:

+ +
输入:
+nums1 = [6, 7]
+nums2 = [6, 0, 4]
+k = 5
+输出:
+[6, 7, 6, 0, 4]
+ +

示例 3:

+ +
输入:
+nums1 = [3, 9]
+nums2 = [8, 9]
+k = 3
+输出:
+[9, 8, 9]
+ + +## template + +```java + +class Solution { + + private int[] maxInNums(int[] nums, int k) { + int[] max = new int[k]; + int len = nums.length; + + for (int i = 0, j = 0; i < len; ++i) { + + while (j > 0 && k - j < len - i && max[j - 1] < nums[i]) + --j; + if (j < k) + max[j++] = nums[i]; + } + return max; + } + + private boolean greater(int[] nums1Max, int i, int[] nums2Max, int j) { + int lenNums1Max = nums1Max.length; + int lenNums2Max = nums2Max.length; + + while (i < lenNums1Max && j < lenNums2Max && nums1Max[i] == nums2Max[j]) { + ++i; + ++j; + } + + return j == lenNums2Max || (i < lenNums1Max && nums1Max[i] > nums2Max[j]); + } + + private int[] merge(int[] nums1Max, int[] nums2Max) { + int lenCurRes = nums1Max.length + nums2Max.length; + int[] curRes = new int[lenCurRes]; + for (int i = 0, j = 0, m = 0; m < lenCurRes; ++m) { + curRes[m] = greater(nums1Max, i, nums2Max, j) ? nums1Max[i++] : nums2Max[j++]; + } + return curRes; + } + + public int[] maxNumber(int[] nums1, int[] nums2, int k) { + int[] res = null; + for (int i = Math.max(k - nums2.length, 0); i <= Math.min(nums1.length, k); ++i) { + int[] merge = merge(maxInNums(nums1, i), maxInNums(nums2, k - i)); + res = (res == null || greater(merge, 0, res, 0)) ? merge : res; + } + return res; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6a35af838dc8e66b485b135cb0b45a277d83793 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-6b0808a529524be2afa75c84d9c72795", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4f627537331d6850a2f319b03f75b4ee7079a051 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "88d40224a81542f4819bfca909d91ef9", + "author": "csdn.net", + "keywords": "树状数组,线段树,数组,二分查找,分治,有序集合,归并排序" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..989b99bcbeaf30ea9500ddf4da49f6b122b9cb3f --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/327.exercises/solution.md" @@ -0,0 +1,116 @@ +# 区间和的个数 + +

给你一个整数数组 nums 以及两个整数 lowerupper 。求数组中,值位于范围 [lower, upper] (包含 lower 和 upper)之内的 区间和的个数

+ +

区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的元素之和,包含 i 和 j (ij)。

+ +

 

+示例 1: + +
+输入:nums = [-2,5,-1], lower = -2, upper = 2
+输出:3
+解释:存在三个区间:[0,0]、[2,2] 和 [0,2] ,对应的区间和分别是:-2 、-1 、2 。
+
+ +

示例 2:

+ +
+输入:nums = [0], lower = 0, upper = 0
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= nums.length <= 105
  • +
  • -231 <= nums[i] <= 231 - 1
  • +
  • -105 <= lower <= upper <= 105
  • +
  • 题目数据保证答案是一个 32 位 的整数
  • +
+ + +## template + +```java +class Solution { + public int countRangeSum(int[] nums, long lower, long upper) { + long sums[] = new long[nums.length]; + for (int i = 0; i < nums.length; i++) { + sums[i] = ((i - 1 >= 0) ? sums[i - 1] : 0) + nums[i]; + } + + int result = divideAndConquer(sums, 0, sums.length - 1, upper, lower); + + return result; + } + + private int divideAndConquer(long sums[], int start, int end, long upper, long lower) { + if (start > end) + return 0; + if (start == end) + return (sums[start] <= upper && sums[start] >= lower) ? 1 : 0; + int mid = (start + end) / 2; + int counts = 0; + counts += divideAndConquer(sums, start, mid, upper, lower); + counts += divideAndConquer(sums, mid + 1, end, upper, lower); + + int ls = start, le = mid; + while (le >= start && sums[mid + 1] - sums[le] <= upper) + le--; + for (int r = mid + 1; r <= end; r++) { + while (ls <= mid && sums[r] - sums[ls] >= lower) + ls++; + while (le + 1 <= mid && sums[r] - sums[le + 1] > upper) + le++; + if (ls - le - 1 < 0) + continue; + counts += (ls - le - 1); + } + ls = start; + int i = 0, r = mid + 1; + long merged[] = new long[end - start + 1]; + while (ls <= mid || r <= end) { + if (ls > mid || (r <= end && sums[r] < sums[ls])) { + merged[i++] = sums[r++]; + } else { + merged[i++] = sums[ls++]; + } + } + for (i = 0; i < merged.length; i++) { + sums[start + i] = merged[i]; + } + + return counts; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..1fee42842d2d9f2f4fa74d8a94de82d8f9ff94b6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-3b0733c5f34f48f383ca2eb091982455", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.json" similarity index 61% rename from "data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.json" rename to "data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.json" index 33540b2e73f0b580845f48c12347ef9ddf4479ef..e1498d7a876f9fe9bae7d97e0b350780852038eb 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.json" +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.json" @@ -1,7 +1,7 @@ { "type": "code_options", "source": "solution.md", - "exercise_id": "21de49cd4b944f7d80beb85df48fc282", + "exercise_id": "bb7b4b7bacb14924afeb51700887e3ee", "author": "csdn.net", - "keywords": "深度优先搜索,广度优先搜索,图,拓扑排序,数组,字符串" + "keywords": "深度优先搜索,广度优先搜索,图,拓扑排序,记忆化搜索,动态规划" } \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..86da387ede42f453237772f5457ffa304b11f539 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/329.exercises/solution.md" @@ -0,0 +1,108 @@ +# 矩阵中的最长递增路径 + +

给定一个 m x n 整数矩阵 matrix ,找出其中 最长递增路径 的长度。

+ +

对于每个单元格,你可以往上,下,左,右四个方向移动。 你 不能对角线 方向上移动或移动到 边界外(即不允许环绕)。

+ +

 

+ +

示例 1:

+ +
+输入:matrix = [[9,9,4],[6,6,8],[2,1,1]]
+输出:4 
+解释:最长递增路径为 [1, 2, 6, 9]
+ +

示例 2:

+ +
+输入:matrix = [[3,4,5],[3,2,6],[2,2,1]]
+输出:4 
+解释:最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。
+
+ +

示例 3:

+ +
+输入:matrix = [[1]]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 200
  • +
  • 0 <= matrix[i][j] <= 231 - 1
  • +
+ + +## template + +```java + + +class Solution { + public int longestIncreasingPath(int[][] matrix) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return 0; + } + int max = 0; + int row = matrix.length; + int col = matrix[0].length; + int[][] dp = new int[row][col]; + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + max = Math.max(max, loop(matrix, Integer.MIN_VALUE, dp, i, j)); + } + } + return max; + } + + private int loop(int[][] mat, int pre, int[][] dp, int i, int j) { + if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] <= pre) { + return 0; + } + if (dp[i][j] != 0) { + return dp[i][j]; + } + int max = 0; + max = Math.max(max, loop(mat, mat[i][j], dp, i - 1, j)); + max = Math.max(max, loop(mat, mat[i][j], dp, i + 1, j)); + max = Math.max(max, loop(mat, mat[i][j], dp, i, j - 1)); + max = Math.max(max, loop(mat, mat[i][j], dp, i, j + 1)); + dp[i][j] = max + 1; + return dp[i][j]; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..81c683e78c2d4564973fcd1c3444fcf54208e960 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-14a8ae11af7e487c83cd7d251d03057b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..be6b5c80d20beda4d69428e1dfe15c97e100fc11 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "d28e13128f7b4dfd8a54b89b23cba61f", + "author": "csdn.net", + "keywords": "贪心,数组" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..eabe5f0adb2e7a693812644b35e2dfd63a0c8994 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/330.exercises/solution.md" @@ -0,0 +1,74 @@ +# 按要求补齐数组 + +

给定一个已排序的正整数数组 nums,和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。

+ +

示例 1:

+ +
输入: nums = [1,3], n = 6
+输出: 1 
+解释:
+根据 nums 里现有的组合 [1], [3], [1,3],可以得出 1, 3, 4。
+现在如果我们将 2 添加到 nums 中, 组合变为: [1], [2], [3], [1,3], [2,3], [1,2,3]。
+其和可以表示数字 1, 2, 3, 4, 5, 6,能够覆盖 [1, 6] 区间里所有的数。
+所以我们最少需要添加一个数字。
+ +

示例 2:

+ +
输入: nums = [1,5,10], n = 20
+输出: 2
+解释: 我们需要添加 [2, 4]。
+
+ +

示例 3:

+ +
输入: nums = [1,2,2], n = 5
+输出: 0
+
+ + +## template + +```java +class Solution { + public int minPatches(int[] nums, int n) { + int patches = 0, i = 0; + long miss = 1; + while (miss <= n) { + if (i < nums.length && nums[i] <= miss) + miss += nums[i++]; + else { + miss += miss; + patches++; + } + } + return patches; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..f361f3112963e9e520583403a489d3c7e701005a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-99e37371b73f4c7eae96ca1ad461fed9", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..a047ff3361094c971abaf33dffe6d1a1acd97143 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "4caea52122df439b895b005ba78c3be9", + "author": "csdn.net", + "keywords": "几何,数组,数学" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..af2ed686fc17587e13488a2f208ecefb51a4d15a --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/335.exercises/solution.md" @@ -0,0 +1,92 @@ +# 路径交叉 + +

给你一个整数数组 distance

+ +

X-Y 平面上的点 (0,0) 开始,先向北移动 distance[0] 米,然后向西移动 distance[1] 米,向南移动 distance[2] 米,向东移动 distance[3] 米,持续移动。也就是说,每次移动后你的方位会发生逆时针变化。

+ +

判断你所经过的路径是否相交。如果相交,返回 true ;否则,返回 false

+ +

 

+ +

示例 1:

+ +
+输入:distance = [2,1,1,2]
+输出:true
+
+ +

示例 2:

+ +
+输入:distance = [1,2,3,4]
+输出:false
+
+ +

示例 3:

+ +
+输入:distance = [1,1,1,1]
+输出:true
+ +

 

+ +

提示:

+ +
    +
  • 1 <= distance.length <= 105
  • +
  • 1 <= distance[i] <= 105
  • +
+ + +## template + +```java +class Solution { + public boolean isSelfCrossing(int[] x) { + if (x.length < 4) + return false; + int a = 0, b = 0, c = 0; + int d = x[0], e = x[1], f = x[2]; + for (int i = 3; i < x.length; i++) { + a = b; + b = c; + c = d; + d = e; + e = f; + f = x[i]; + if (e < c - a && f >= d) + return true; + if (c - a <= e && e <= c && f >= (d - b < 0 ? d : d - b)) + return true; + } + return false; + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..6b6a8c979651b446d9d2db3c36af4d77ffbdef96 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-9a1045ab45014e6d995b200ebe6dcc90", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..5c0fcc2a4259f9145cccdc3e36b04b80b782bc81 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "2816b054ec174bcbbcb978a637b9fef2", + "author": "csdn.net", + "keywords": "字典树,数组,哈希表,字符串" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..7f49a0c7cafb19d9753844c7377d5acd0d8087ad --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/336.exercises/solution.md" @@ -0,0 +1,149 @@ +# 回文对 + +

给定一组 互不相同 的单词, 找出所有 不同 的索引对 (i, j),使得列表中的两个单词, words[i] + words[j] ,可拼接成回文串。

+ +

 

+ +

示例 1:

+ +
+输入:words = ["abcd","dcba","lls","s","sssll"]
+输出:[[0,1],[1,0],[3,2],[2,4]] 
+解释:可拼接成的回文串为 ["dcbaabcd","abcddcba","slls","llssssll"]
+
+ +

示例 2:

+ +
+输入:words = ["bat","tab","cat"]
+输出:[[0,1],[1,0]] 
+解释:可拼接成的回文串为 ["battab","tabbat"]
+ +

示例 3:

+ +
+输入:words = ["a",""]
+输出:[[0,1],[1,0]]
+
+  + +

提示:

+ +
    +
  • 1 <= words.length <= 5000
  • +
  • 0 <= words[i].length <= 300
  • +
  • words[i] 由小写英文字母组成
  • +
+ + +## template + +```java +class Solution { + private static List> ans = new ArrayList<>(); + + public List> palindromePairs(String[] words) { + if (words == null || words.length == 0) { + return null; + } + + ans = new ArrayList<>(); + TrieNode root = new TrieNode(); + + for (int i = 0; i < words.length; i++) { + addWord(root, words[i], i); + } + + for (int i = 0; i < words.length; i++) { + find(root, words[i], i); + } + return ans; + } + + private static class TrieNode { + + int index; + TrieNode[] next; + List palindIndex; + + public TrieNode() { + index = -1; + next = new TrieNode[26]; + palindIndex = new ArrayList<>(); + } + } + + private static void addWord(TrieNode root, String word, int index) { + for (int i = word.length() - 1; i >= 0; i--) { + int ch = word.charAt(i) - 'a'; + if (root.next[ch] == null) { + root.next[ch] = new TrieNode(); + } + + if (isPalindrome(word, 0, i)) { + root.palindIndex.add(index); + } + root = root.next[ch]; + } + root.index = index; + root.palindIndex.add(index); + } + + private static void find(TrieNode root, String word, int index) { + for (int i = 0; i < word.length(); i++) { + + if (root.index != -1 && root.index != index && isPalindrome(word, i, word.length() - 1)) { + ans.add(Arrays.asList(index, root.index)); + } + + if (root.next[word.charAt(i) - 'a'] == null) { + return; + } + root = root.next[word.charAt(i) - 'a']; + } + + for (int i : root.palindIndex) { + if (i != index) { + ans.add(Arrays.asList(index, i)); + } + } + } + + private static boolean isPalindrome(String string, int l, int r) { + while (l < r) { + if (string.charAt(l++) != string.charAt(r--)) { + return false; + } + } + return true; + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..a6a039ce8948c5ee76c31c32c0bbd68da2835ba9 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-dcc9b95062df4bac904b194f845979a1", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..85b575a75f21e3f20bb744ae67bb07afa3e8c2ff --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "31d723fbab1348c7a4d401af35935327", + "author": "csdn.net", + "keywords": "设计,二分查找,有序集合" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..9cb91f9e60302de0bb6350928055894a4775a858 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/352.exercises/solution.md" @@ -0,0 +1,118 @@ +# 将数据流变为多个不相交区间 + +

 给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。

+ +

实现 SummaryRanges 类:

+ +
+
+
    +
  • SummaryRanges() 使用一个空数据流初始化对象。
  • +
  • void addNum(int val) 向数据流中加入整数 val
  • +
  • int[][] getIntervals() 以不相交区间 [starti, endi] 的列表形式返回对数据流中整数的总结。
  • +
+ +

 

+ +

示例:

+ +
+输入:
+["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
+[[], [1], [], [3], [], [7], [], [2], [], [6], []]
+输出:
+[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]
+
+解释:
+SummaryRanges summaryRanges = new SummaryRanges();
+summaryRanges.addNum(1);      // arr = [1]
+summaryRanges.getIntervals(); // 返回 [[1, 1]]
+summaryRanges.addNum(3);      // arr = [1, 3]
+summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
+summaryRanges.addNum(7);      // arr = [1, 3, 7]
+summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
+summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
+summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
+summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
+summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
+
+ +

 

+ +

提示:

+ +
    +
  • 0 <= val <= 104
  • +
  • 最多调用 addNumgetIntervals 方法 3 * 104
  • +
+
+
+ +

 

+ +

进阶:如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?

+ + +## template + +```java +class SummaryRanges { + private final TreeSet tree = new TreeSet<>(); + + public void addNum(int val) { + tree.add(val); + } + + public int[][] getIntervals() { + ArrayList result = new ArrayList<>(1 << 2); + Iterator iterator = tree.iterator(); + int[] array = new int[tree.size()]; + int i = 0; + while (iterator.hasNext()) + array[i++] = iterator.next(); + int length = array.length; + if (length == 0) + return result.toArray(new int[0][]); + int start = array[0]; + + for (i = 0; i < length; ++i) { + if (i + 1 < length && array[i + 1] - array[i] != 1) { + result.add(new int[] { start, array[i] }); + start = array[i + 1]; + } else if (i + 1 == length) { + result.add(new int[] { start, array[i] }); + } + } + + return result.toArray(new int[result.size()][]); + } + +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..af3c6b477b13036c526ff785c5d10af6fdfd814e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-85a1e7d8799a4f958055839d7976da9f", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..e9e56de37b7bb73082fc99822eeed8cc7722c0e6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "05e15bfc873841b9bb8f1d2fc70a532a", + "author": "csdn.net", + "keywords": "数组,二分查找,动态规划,排序" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..d7488f57c4b411dd309c64da97e7bdaad0ec3ce3 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/354.exercises/solution.md" @@ -0,0 +1,102 @@ +# 俄罗斯套娃信封问题 + +

给你一个二维整数数组 envelopes ,其中 envelopes[i] = [wi, hi] ,表示第 i 个信封的宽度和高度。

+ +

当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

+ +

请计算 最多能有多少个 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

+ +

注意:不允许旋转信封。

+  + +

示例 1:

+ +
+输入:envelopes = [[5,4],[6,4],[6,7],[2,3]]
+输出:3
+解释:最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
+ +

示例 2:

+ +
+输入:envelopes = [[1,1],[1,1],[1,1]]
+输出:1
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= envelopes.length <= 5000
  • +
  • envelopes[i].length == 2
  • +
  • 1 <= wi, hi <= 104
  • +
+ + +## template + +```java + +class Solution { + public int maxEnvelopes(int[][] envelopes) { + int n = envelopes.length; + if (n == 0) + return 0; + Arrays.sort(envelopes, new Comparator() { + public int compare(int[] a, int[] b) { + if (a[0] != b[0]) + return a[0] - b[0]; + return b[1] - a[1]; + } + }); + + List lastHeight = new ArrayList<>(); + lastHeight.add(envelopes[0][1]); + for (int i = 1; i < n; i++) { + int h = envelopes[i][1]; + if (h > lastHeight.get(lastHeight.size() - 1)) + lastHeight.add(h); + else { + int l = 0, r = lastHeight.size() - 1; + while (r > l) { + int m = (l + r) >> 1; + if (lastHeight.get(m) < h) + l = m + 1; + else + r = m; + } + lastHeight.set(l, h); + } + } + return lastHeight.size(); + } +} + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..d0e11b2d19649be9a1c6e1deb4dd6eccca2e3a3c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-a11c5f6070274e91b5323a99da5e211c", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..4a9d1dba065c208ffff638d8e6e153f83ef62fec --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "50dee266fb4848cd8dc96aedf97b3ccf", + "author": "csdn.net", + "keywords": "数组,二分查找,动态规划,矩阵,有序集合" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..6a32a61d47d97203c7fa2b09abf25239911632e6 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/363.exercises/solution.md" @@ -0,0 +1,116 @@ +# 矩形区域不超过 K 的最大数值和 + +

给你一个 m x n 的矩阵 matrix 和一个整数 k ,找出并返回矩阵内部矩形区域的不超过 k 的最大数值和。

+ +

题目数据保证总会存在一个数值和不超过 k 的矩形区域。

+ +

 

+ +

示例 1:

+ +
+输入:matrix = [[1,0,1],[0,-2,3]], k = 2
+输出:2
+解释:蓝色边框圈出来的矩形区域 [[0, 1], [-2, 3]] 的数值和是 2,且 2 是不超过 k 的最大数字(k = 2)。
+
+ +

示例 2:

+ +
+输入:matrix = [[2,2,-1]], k = 3
+输出:3
+
+ +

 

+ +

提示:

+ +
    +
  • m == matrix.length
  • +
  • n == matrix[i].length
  • +
  • 1 <= m, n <= 100
  • +
  • -100 <= matrix[i][j] <= 100
  • +
  • -105 <= k <= 105
  • +
+ +

 

+ +

进阶:如果行数远大于列数,该如何设计解决方案?

+ + +## template + +```java +class Solution { + public int maxSumSubmatrix(int[][] matrix, int k) { + + int row = matrix.length; + if (row <= 0) + return 0; + int line = matrix[0].length; + int max = Integer.MIN_VALUE; + + for (int left = 0; left < line; left++) { + + int[] rowSum = new int[row]; + + for (int right = left; right < line; right++) { + + for (int i = 0; i < row; i++) { + rowSum[i] += matrix[i][right]; + } + + max = Math.max(max, dpmax(rowSum, k)); + + } + + } + + return max; + + } + + private int dpmax(int[] arr, int k) { + int max = Integer.MIN_VALUE; + int n = arr.length; + for (int top = 0; top < n; top++) { + int sum = 0; + for (int bottom = top; bottom < n; bottom++) { + sum += arr[bottom]; + if (sum > max && sum <= k) + max = sum; + } + } + return max; + } + +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..40e61ffbc1835756b7f01847bd755250c7640768 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-100343f408f44221b30732cc26603dc1", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6f6be79b09f65a45695764e13537b2e90044b1eb --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "db1158dcd7384feb9692778d45fbab07", + "author": "csdn.net", + "keywords": "设计,数组,哈希表,数学,随机化" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..09c4b8b80ee3c06ac655046fe7230e86a8ddd8eb --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/381.exercises/solution.md" @@ -0,0 +1,142 @@ +# O(1) 时间插入、删除和获取随机元素 + +

设计一个支持在平均 时间复杂度 O(1) , 执行以下操作的数据结构。

+ +

注意: 允许出现重复元素。

+ +
    +
  1. insert(val):向集合中插入元素 val。
  2. +
  3. remove(val):当 val 存在时,从集合中移除一个 val。
  4. +
  5. getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
  6. +
+ +

示例:

+ +
// 初始化一个空的集合。
+RandomizedCollection collection = new RandomizedCollection();
+
+// 向集合中插入 1 。返回 true 表示集合不包含 1 。
+collection.insert(1);
+
+// 向集合中插入另一个 1 。返回 false 表示集合包含 1 。集合现在包含 [1,1] 。
+collection.insert(1);
+
+// 向集合中插入 2 ,返回 true 。集合现在包含 [1,1,2] 。
+collection.insert(2);
+
+// getRandom 应当有 2/3 的概率返回 1 ,1/3 的概率返回 2 。
+collection.getRandom();
+
+// 从集合中删除 1 ,返回 true 。集合现在包含 [1,2] 。
+collection.remove(1);
+
+// getRandom 应有相同概率返回 1 和 2 。
+collection.getRandom();
+
+ + +## template + +```java +class RandomizedCollection { + + private Map> map; + + private List list; + + private Random random; + + private int size = 0; + + public RandomizedCollection() { + map = new HashMap<>(); + list = new ArrayList<>(); + random = new Random(); + } + + public boolean insert(int val) { + if (map.containsKey(val)) { + Set indexes = map.get(val); + list.add(size, val); + indexes.add(size); + size++; + return false; + } else { + Set indexes = new HashSet<>(); + map.put(val, indexes); + list.add(size, val); + indexes.add(size); + size++; + return true; + } + } + + public boolean remove(int val) { + if (!map.containsKey(val)) { + return false; + } + Set indexes = map.get(val); + if (list.get(size - 1) == val) { + indexes.remove(size - 1); + size--; + } else { + + Iterator it = indexes.iterator(); + int index = it.next(); + it.remove(); + + int last = list.get(size - 1); + list.set(index, last); + Set set = map.get(last); + + set.remove(size - 1); + set.add(index); + size--; + } + if (indexes.size() == 0) { + map.remove(val); + } + return true; + } + + public int getRandom() { + return list.get(random.nextInt(size)); + } +} + +/** + * Your RandomizedCollection object will be instantiated and called as such: + * RandomizedCollection obj = new RandomizedCollection(); + * boolean param_1 = obj.insert(val); + * boolean param_2 = obj.remove(val); + * int param_3 = obj.getRandom(); + */ + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..0469f4becc037a331df49fb94a769371eda9db5e --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-3383493bbc954d8ab4c4f392d23acc4b", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..0c7681772718c8a757905fc1acdf75990459423c --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "b6ee5c4de3444ab79995fd11d74453e5", + "author": "csdn.net", + "keywords": "数组,扫描线" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..99505a71d7dc0f308370d91e11e5ba87e9c0295b --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/391.exercises/solution.md" @@ -0,0 +1,152 @@ +# 完美矩形 + +

我们有 N 个与坐标轴对齐的矩形, 其中 N > 0, 判断它们是否能精确地覆盖一个矩形区域。

+ +

每个矩形用左下角的点和右上角的点的坐标来表示。例如, 一个单位正方形可以表示为 [1,1,2,2]。 ( 左下角的点的坐标为 (1, 1) 以及右上角的点的坐标为 (2, 2) )。

+ +

+ +

示例 1:

+ +
rectangles = [
+  [1,1,3,3],
+  [3,1,4,2],
+  [3,2,4,4],
+  [1,3,2,4],
+  [2,3,3,4]
+]
+
+返回 true。5个矩形一起可以精确地覆盖一个矩形区域。
+
+ +

 

+ +

+ +

示例 2:

+ +
rectangles = [
+  [1,1,2,3],
+  [1,3,2,4],
+  [3,1,4,2],
+  [3,2,4,4]
+]
+
+返回 false。两个矩形之间有间隔,无法覆盖成一个矩形。
+
+ +

 

+ +

+ +

示例 3:

+ +
rectangles = [
+  [1,1,3,3],
+  [3,1,4,2],
+  [1,3,2,4],
+  [3,2,4,4]
+]
+
+返回 false。图形顶端留有间隔,无法覆盖成一个矩形。
+
+ +

 

+ +

+ +

示例 4:

+ +
rectangles = [
+  [1,1,3,3],
+  [3,1,4,2],
+  [1,3,2,4],
+  [2,2,4,4]
+]
+
+返回 false。因为中间有相交区域,虽然形成了矩形,但不是精确覆盖。
+
+ + +## template + +```java + +class Solution { + + public boolean isRectangleCover(int[][] rectangles) { + int left = Integer.MAX_VALUE; + int right = Integer.MIN_VALUE; + int top = Integer.MIN_VALUE; + int bottom = Integer.MAX_VALUE; + int n = rectangles.length; + + Set set = new HashSet<>(); + int sumArea = 0; + + for (int i = 0; i < n; i++) { + + left = Math.min(left, rectangles[i][0]); + bottom = Math.min(bottom, rectangles[i][1]); + right = Math.max(right, rectangles[i][2]); + top = Math.max(top, rectangles[i][3]); + + sumArea += (rectangles[i][3] - rectangles[i][1]) * (rectangles[i][2] - rectangles[i][0]); + + String lt = rectangles[i][0] + " " + rectangles[i][3]; + String lb = rectangles[i][0] + " " + rectangles[i][1]; + String rt = rectangles[i][2] + " " + rectangles[i][3]; + String rb = rectangles[i][2] + " " + rectangles[i][1]; + + if (!set.contains(lt)) + set.add(lt); + else + set.remove(lt); + if (!set.contains(lb)) + set.add(lb); + else + set.remove(lb); + if (!set.contains(rt)) + set.add(rt); + else + set.remove(rt); + if (!set.contains(rb)) + set.add(rb); + else + set.remove(rb); + } + + if (set.size() == 4 && set.contains(left + " " + top) && set.contains(left + " " + bottom) + && set.contains(right + " " + bottom) && set.contains(right + " " + top)) { + return sumArea == (right - left) * (top - bottom); + } + return false; + } +} +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/config.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/config.json" new file mode 100644 index 0000000000000000000000000000000000000000..918267b455106f75c9ef2ecd6cd73581969fc140 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/config.json" @@ -0,0 +1,10 @@ +{ + "node_id": "dailycode-d1ca2fb8838445e5a9556a48a746c78a", + "keywords": [], + "children": [], + "keywords_must": [], + "keywords_forbid": [], + "export": [ + "solution.json" + ] +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.json" "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.json" new file mode 100644 index 0000000000000000000000000000000000000000..6e8b6c1173936ea442f4589b311adb7c5e3fde65 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "source": "solution.md", + "exercise_id": "c484f0e1a775406281353e95fa8a105b", + "author": "csdn.net", + "keywords": "数组,动态规划" +} \ No newline at end of file diff --git "a/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.md" "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.md" new file mode 100644 index 0000000000000000000000000000000000000000..8d1899c673de7106f1c4bb088ca06326295ac4f1 --- /dev/null +++ "b/data/3.dailycode\351\253\230\351\230\266/2.java/403.exercises/solution.md" @@ -0,0 +1,87 @@ +# 青蛙过河 + +

一只青蛙想要过河。 假定河流被等分为若干个单元格,并且在每一个单元格内都有可能放有一块石子(也有可能没有)。 青蛙可以跳上石子,但是不可以跳入水中。

+ +

给你石子的位置列表 stones(用单元格序号 升序 表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一块石子上)。

+ +

开始时, 青蛙默认已站在第一块石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格 1 跳至单元格 2 )。

+ +

如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1k 或 k + 1 个单位。 另请注意,青蛙只能向前方(终点的方向)跳跃。

+ +

 

+ +

示例 1:

+ +
+输入:stones = [0,1,3,5,6,8,12,17]
+输出:true
+解释:青蛙可以成功过河,按照如下方案跳跃:跳 1 个单位到第 2 块石子, 然后跳 2 个单位到第 3 块石子, 接着 跳 2 个单位到第 4 块石子, 然后跳 3 个单位到第 6 块石子, 跳 4 个单位到第 7 块石子, 最后,跳 5 个单位到第 8 个石子(即最后一块石子)。
+ +

示例 2:

+ +
+输入:stones = [0,1,2,3,4,8,9,11]
+输出:false
+解释:这是因为第 5 和第 6 个石子之间的间距太大,没有可选的方案供青蛙跳跃过去。
+ +

 

+ +

提示:

+ +
    +
  • 2 <= stones.length <= 2000
  • +
  • 0 <= stones[i] <= 231 - 1
  • +
  • stones[0] == 0
  • +
+ + +## template + +```java +class Solution { + + public boolean canCross(int[] stones) { + for (int i = 1; i < stones.length; i++) + if (stones[i] > stones[i - 1] + i) + return false; + return dfs(stones, 0, 1); + } + + public boolean dfs(int[] stones, int index, int v) { + if (index < 0 || v < 1) + return false; + return index == stones.length - 1 + || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) || + dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) || + dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1); + } +} + + +``` + +## 答案 + +```java + +``` + +## 选项 + +### A + +```java + +``` + +### B + +```java + +``` + +### C + +```java + +``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/102.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/102.exercises/solution.md" index 0d327959277be7b719ffaff29b3d9e1ce4aa68fd..377fafadce6a9f292642329990ec6ee0725c1ba9 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/102.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/102.exercises/solution.md" @@ -29,7 +29,42 @@ ## template ```java - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> levelOrder(TreeNode root) { + List> l = new ArrayList<>(); + Queue q = new LinkedList(); + if (root != null) { + q.add(root); + } + while (!q.isEmpty()) { + List l2 = new ArrayList<>(); + int number = q.size(); + while (number > 0) { + TreeNode t = q.poll(); + l2.add(t.val); + if (t.left != null) { + q.add(t.left); + } + if (t.right != null) { + q.add(t.right); + } + number--; + } + l.add(l2); + } + return l; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/103.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/103.exercises/solution.md" index 70aaf6e177f81afffc36f3ba74282e542e45e595..ae68b131b350321d2495f1e68cab40caacec7e7a 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/103.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/103.exercises/solution.md" @@ -28,6 +28,61 @@ ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> zigzagLevelOrder(TreeNode root) { + List> list = new LinkedList<>(); + if (root == null) { + return list; + } + Stack stack1 = new Stack<>(); + stack1.push(root); + + boolean postive = true; + + while (!stack1.isEmpty()) { + Stack stack2 = new Stack<>(); + List subList = new LinkedList<>(); + while (!stack1.isEmpty()) { + TreeNode current = stack1.pop(); + subList.add(current.val); + if (postive) { + if (current.left != null) { + stack2.push(current.left); + } + if (current.right != null) { + stack2.push(current.right); + } + } else { + if (current.right != null) { + stack2.push(current.right); + } + if (current.left != null) { + stack2.push(current.left); + } + } + + } + postive = !postive; + + stack1 = stack2; + list.add(subList); + } + + return list; + + } +} + ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/105.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/105.exercises/solution.md" index df64dac36280bdc17e66a5b36c2b5a814356d1e3..4b44f326fce12330006c7cfdfc4d2425c78b6ff8 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/105.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/105.exercises/solution.md" @@ -36,6 +36,51 @@ ## template ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +public class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + if (preorder.length != inorder.length) + return null; + if (preorder.length == 0) + return null; + if (preorder.length == 1) + return new TreeNode(preorder[0]); + return buildTree(preorder, inorder, 0, preorder.length - 1, 0, inorder.length - 1); + } + + private TreeNode buildTree(int[] preorder, int[] inorder, int prei, int prej, int ini, int inj) { + if (prei > prej || ini > inj || prei < 0 || prej >= preorder.length || ini < 0 || inj >= inorder.length) + return null; + if (prej - prei < 0) + return null; + if (prei == prej) + return new TreeNode(preorder[prei]); + TreeNode root = new TreeNode(preorder[prei]); + int inFlag = 0; + for (int i = ini; i <= inj; i++) { + if (inorder[i] == root.val) { + inFlag = i; + break; + } + } + int num_left = inFlag - ini; + int num_right = inj - inFlag; + root.left = buildTree(preorder, inorder, prei + 1, prei + num_left, ini, inFlag - 1); + root.right = buildTree(preorder, inorder, prej - num_right + 1, prej, inFlag + 1, inj); + + return root; + + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/106.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/106.exercises/solution.md" index ee15a734bd053547234876a6f0e5865fc447e3ce..37636406ce32941e660370bdc34ededff609e111 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/106.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/106.exercises/solution.md" @@ -23,7 +23,42 @@ ## template ```java - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public TreeNode buildTree(int[] inorder, int[] postorder) { + return helper(inorder, postorder, postorder.length - 1, 0, inorder.length - 1); + } + + public TreeNode helper(int[] inorder, int[] postorder, int postEnd, int inStart, int inEnd) { + if (inStart > inEnd) { + return null; + } + + int currentVal = postorder[postEnd]; + TreeNode current = new TreeNode(currentVal); + + int inIndex = 0; + for (int i = inStart; i <= inEnd; i++) { + if (inorder[i] == currentVal) { + inIndex = i; + } + } + TreeNode left = helper(inorder, postorder, postEnd - (inEnd - inIndex) - 1, inStart, inIndex - 1); + TreeNode right = helper(inorder, postorder, postEnd - 1, inIndex + 1, inEnd); + current.left = left; + current.right = right; + return current; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/107.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/107.exercises/solution.md" index 61ccf247b31da381b7685328617506bf6301f1ce..9177280110d7d9bde39ab26e3482e60f61c6f417 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/107.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/107.exercises/solution.md" @@ -27,6 +27,40 @@ ## template ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List> levelOrderBottom(TreeNode root) { + List> list1 = new ArrayList<>(); + if (root == null) + return list1; + + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + List list2 = new ArrayList<>(); + int count = queue.size(); + for (int i = 0; i < count; i++) { + TreeNode node = queue.poll(); + list2.add(node.val); + if (node.left != null) + queue.add(node.left); + if (node.right != null) + queue.add(node.right); + } + list1.add(0, list2); + } + return list1; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/109.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/109.exercises/solution.md" index 3bf557919603e22af6bcc4edfcc64332153b1e6c..e68848dac3a9085f69ef76f20aea199f4b8a4df2 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/109.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/109.exercises/solution.md" @@ -22,6 +22,71 @@ ```java +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; + } +} + +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public TreeNode sortedListToBST(ListNode head) { + if (head == null) + return null; + return helper(head, null); + } + + private TreeNode helper(ListNode start, ListNode end) { + if (start == end) + return null; + + ListNode slow = start; + ListNode fast = start; + + while (fast != end && fast.next != end) { + + slow = slow.next; + + fast = fast.next.next; + } + + TreeNode root = new TreeNode(slow.val); + + root.left = helper(start, slow); + + root.right = helper(slow.next, end); + return root; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/113.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/113.exercises/solution.md" index 4bc111d705fd30901ce6de893bb2d1c96ad405fb..f6ae8e888a02f8cec21b94d75b34274913d8f54b 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/113.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/113.exercises/solution.md" @@ -45,6 +45,31 @@ ## template ```java +class Solution { + public List> pathSum(TreeNode root, int sum) { + if (root == null) + return new ArrayList<>(); + List> ans = new ArrayList<>(); + if (root.val == sum && root.left == null && root.right == null) { + List arr = new ArrayList<>(); + arr.add(root.val); + ans.add(arr); + return ans; + } + List> left = pathSum(root.left, sum - root.val); + List> right = pathSum(root.right, sum - root.val); + for (List list : left) { + + list.add(0, root.val); + ans.add(list); + } + for (List list : right) { + list.add(0, root.val); + ans.add(list); + } + return ans; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/114.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/114.exercises/solution.md" index c4816c79a828149f317485181e32755b8a678266..f97441f2119876d32710b6810587965c8ae9f71f 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/114.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/114.exercises/solution.md" @@ -47,8 +47,33 @@ ## template ```java - - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public void flatten(TreeNode root) { + while (root != null) { + if (root.left == null) + root = root.right; + else { + TreeNode pre = root.left; + while (pre.right != null) + pre = pre.right; + pre.right = root.right; + root.right = root.left; + root.left = null; + root = root.right; + } + } + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/116.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/116.exercises/solution.md" index 99a271dcb167935ee07661c0b7fe34d2d5169171..8f0423e45fe2c459787b108002e36f10f805adba 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/116.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/116.exercises/solution.md" @@ -48,8 +48,41 @@ struct Node { ## template ```java - - +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node connect(Node root) { + if (root == null) + return root; + if (root.left != null) + root.left.next = root.right; + if (root.next != null && root.right != null) { + root.right.next = root.next.left; + } + connect(root.left); + connect(root.right); + return root; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/117.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/117.exercises/solution.md" index b6743f3a90b6ffcd9646c81e3e06dd9e605f789a..ae2d602a85fa8379a20af2eb32e445d4183b73e0 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/117.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/117.exercises/solution.md" @@ -52,7 +52,61 @@ struct Node { ## template ```java - +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node connect(Node root) { + if (root == null || (root.left == null && root.right == null)) { + return root; + } + if (root.left != null && root.right != null) { + root.left.next = root.right; + root.next = getrightnext(root); + } + if (root.left != null) { + root.left.next = getrightnext(root); + } + if (root.right != null) { + root.right.next = getrightnext(root); + } + connect(root.right); + connect(root.left); + return root; + + } + + public static Node getrightnext(Node root) { + while (root.next != null) { + if (root.left != null) { + return root.left; + } + if (root.right != null) { + return root.right; + } + root = root.next; + } + return null; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/120.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/120.exercises/solution.md" index dfb0ba8fc6800e435b87450e7df186dfe87908ac..e4b5cc0530cc587a5725d4a1afe68264b89b4129 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/120.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/120.exercises/solution.md" @@ -49,7 +49,33 @@ ## template ```java - +class Solution { + public int minimumTotal(List> triangle) { + int[] dp = new int[triangle.size()]; + int ans = 0; + dp[0] = triangle.get(0).get(0); + int temp, prev = 0, cur; + for (int i = 1; i < triangle.size(); i++) { + for (int j = 0; j < triangle.get(i).size(); j++) { + temp = triangle.get(i).get(j); + cur = dp[j]; + if (j == 0) + dp[j] = cur + temp; + else if (j == triangle.get(i).size() - 1) + dp[j] = prev + temp; + else + dp[j] = Math.min(prev, cur) + temp; + prev = cur; + } + } + + for (int i = 1; i < dp.length; i++) { + if (dp[ans] > dp[i]) + ans = i; + } + return dp[ans]; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/128.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/128.exercises/solution.md" index 556a7c7fca88b5fcb9980ad967a61049c58e08a9..167477f2b98e4b4c61d23148f9def83b9f5fdd7a 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/128.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/128.exercises/solution.md" @@ -33,6 +33,32 @@ ## template ```java +class Solution { + public int longestConsecutive(int[] nums) { + Set num_set = new HashSet(); + for (int num : nums) { + num_set.add(num); + } + + int longestStreak = 0; + + for (int num : num_set) { + if (!num_set.contains(num - 1)) { + int currentNum = num; + int currentStreak = 1; + + while (num_set.contains(currentNum + 1)) { + currentNum += 1; + currentStreak += 1; + } + + longestStreak = Math.max(longestStreak, currentStreak); + } + } + + return longestStreak; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/129.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/129.exercises/solution.md" index baf4dfa0012ce1eb7a4950be57bc89e77ab70500..c65cee1f9ebf0ecee59e48a90af8489ecfd75f9a 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/129.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/129.exercises/solution.md" @@ -54,6 +54,52 @@ ```java +public class Solution { + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } + + StringBuffer tempPath = new StringBuffer(); + int sum = 0; + + public void dfs(TreeNode root) { + char c = (char) (root.val + '0'); + tempPath.append(c); + if (root.left == null && root.right == null) { + String s = tempPath.toString(); + int num = Integer.valueOf(s); + sum += num; + } + if (root.left != null) { + dfs(root.left); + tempPath.delete(tempPath.length() - 1, tempPath.length()); + } + if (root.right != null) { + dfs(root.right); + tempPath.delete(tempPath.length() - 1, tempPath.length()); + } + } + + public int sumNumbers(TreeNode root) { + dfs(root); + return sum; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/130.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/130.exercises/solution.md" index 9b4400de4df0f8c549cf979d09d2c7e67a846b83..3efc558a4928c66d723ce22fd0b1447c9b7a959e 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/130.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/130.exercises/solution.md" @@ -38,6 +38,56 @@ ```java +class Solution { + int[] dx = { 1, -1, 0, 0 }; + int[] dy = { 0, 0, 1, -1 }; + + public void solve(char[][] board) { + int n = board.length; + if (n == 0) { + return; + } + int m = board[0].length; + Queue queue = new LinkedList(); + for (int i = 0; i < n; i++) { + if (board[i][0] == 'O') { + queue.offer(new int[] { i, 0 }); + } + if (board[i][m - 1] == 'O') { + queue.offer(new int[] { i, m - 1 }); + } + } + for (int i = 1; i < m - 1; i++) { + if (board[0][i] == 'O') { + queue.offer(new int[] { 0, i }); + } + if (board[n - 1][i] == 'O') { + queue.offer(new int[] { n - 1, i }); + } + } + while (!queue.isEmpty()) { + int[] cell = queue.poll(); + int x = cell[0], y = cell[1]; + board[x][y] = 'A'; + for (int i = 0; i < 4; i++) { + int mx = x + dx[i], my = y + dy[i]; + if (mx < 0 || my < 0 || mx >= n || my >= m || board[mx][my] != 'O') { + continue; + } + queue.offer(new int[] { mx, my }); + } + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (board[i][j] == 'A') { + board[i][j] = 'O'; + } else if (board[i][j] == 'O') { + board[i][j] = 'X'; + } + } + } + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/131.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/131.exercises/solution.md" index 4c656dd786a414f3ec52e6f1b9dbd6ec89c5b7d8..8da1848153c4415ecb1d153d767f1c005c7796f3 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/131.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/131.exercises/solution.md" @@ -34,7 +34,41 @@ ```java - +class Solution { + public List> partition(String s) { + boolean[][] dp = new boolean[s.length()][s.length()]; + for (int len = 1; len <= s.length(); len++) { + for (int i = 0; i <= s.length() - len; i++) { + if (len == 1) + dp[i][i] = true; + else if (s.charAt(i) == s.charAt(i + len - 1) && (len == 2 || dp[i + 1][i + len - 2])) { + dp[i][i + len - 1] = true; + } + } + } + return solution(s, 0, dp); + } + + public List> solution(String s, int start, boolean[][] dp) { + ArrayList> list = new ArrayList<>(); + if (start == s.length()) { + List li = new ArrayList<>(); + list.add(li); + return list; + } + for (int i = start; i < s.length(); i++) { + if (dp[start][i]) { + String first = s.substring(start, i + 1); + for (List li : solution(s, i + 1, dp)) { + li.add(0, first); + list.add(li); + } + } + } + return list; + } + +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/133.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/133.exercises/solution.md" index 2cde941e722fb1daf0ee69d4d3b8a24046521109..b38073a29f06896f6db2a4ecfb99a04197f300d5 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/133.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/133.exercises/solution.md" @@ -75,7 +75,51 @@ ```java - +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node cloneGraph(Node node) { + HashMap visited = new HashMap<>(); + return DFS(node, visited); + } + + public Node DFS(Node node, HashMap visited) { + if (node == null) { + return node; + } + + if (visited.containsKey(node)) { + return visited.get(node); + } + + Node clone = new Node(node.val, new ArrayList<>()); + visited.put(node, clone); + + for (Node n : node.neighbors) { + clone.neighbors.add(DFS(n, visited)); + } + return clone; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/134.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/134.exercises/solution.md" index b6fba141053feff1f4c018e4e2f96656498db668..6bd72a60b21b270187f7f501689091cdc3bba610 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/134.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/134.exercises/solution.md" @@ -51,6 +51,23 @@ cost = [3,4,3] ## template ```java +class Solution { + public int canCompleteCircuit(int[] gas, int[] cost) { + int n = gas.length; + int sum = 0, cur = 0, start = 0; + for (int i = 0; i < n; i++) { + sum = sum + gas[i] - cost[i]; + if (cur < 0) { + start = i; + cur = gas[i] - cost[i]; + } else + cur = cur + gas[i] - cost[i]; + } + if (sum < 0) + return -1; + return start; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/137.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/137.exercises/solution.md" index 10786e85a34cdf9b1d971306c841becbea974470..5f5a4a67f4b82074c8b9ca3be06ee8fa7878ab62 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/137.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/137.exercises/solution.md" @@ -36,6 +36,26 @@ ## template ```java +class Solution { + public int singleNumber(int[] nums) { + + int ret = 0; + for (int i = 0; i < 32; ++i) { + int bitnums = 0; + + int bit = 1 << i; + for (int num : nums) { + + if ((num & bit) != 0) + bitnums++; + } + + if (bitnums % 3 != 0) + ret |= bit; + } + return ret; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/138.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/138.exercises/solution.md" index 67730e8d4207e04c5782628e6c9b253a524557ef..eda011dcc6a96b6aca7c72be21ada4850de7149f 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/138.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/138.exercises/solution.md" @@ -68,7 +68,46 @@ ## template ```java - +class Node { + public int val; + public Node left; + public Node right; + public Node next; + + public Node() { + } + + public Node(int _val) { + val = _val; + } + + public Node(int _val, Node _left, Node _right, Node _next) { + val = _val; + left = _left; + right = _right; + next = _next; + } +}; + +class Solution { + public Node copyRandomList(Node head) { + if (head == null) + return null; + Map map = new HashMap(); + Node p = head; + while (p != null) { + map.put(p, new Node(p.val)); + p = p.next; + } + p = head; + while (p != null) { + map.get(p).next = map.get(p.next); + map.get(p).random = map.get(p.random); + p = p.next; + } + return map.get(head); + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/139.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/139.exercises/solution.md" index d5be2734552ea18cae3ea274220dc36945f9531f..c6cca1ed668c2771a634715407a5b1262d32b31c 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/139.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/139.exercises/solution.md" @@ -35,7 +35,40 @@ ```java - +public class Solution { + public boolean wordBreak(String s, List wordDict) { + int length = s.length(); + int size = wordDict.size(); + if (length == 0) { + return true; + } + if (size == 0) { + return false; + } + + boolean[] dp = new boolean[length]; + for (int i = 0; i < length; i++) { + for (int j = 0; j < size; j++) { + String now = wordDict.get(j); + if (i < now.length()) { + String test = s.substring(0, i + 1); + if (now.equals(test)) { + dp[i] = true; + break; + } + } + if (i >= now.length()) { + String test = s.substring(i - now.length() + 1, i + 1); + if (dp[i - now.length()] && test.equals(now)) { + dp[i] = true; + break; + } + } + } + } + return dp[length - 1]; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/142.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/142.exercises/solution.md" index 1bc4092e609d66aedf3ce570f0633ae27640924b..40cd6228811f66cc6996647459b65a7980ed9941 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/142.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/142.exercises/solution.md" @@ -58,7 +58,42 @@ ## template ```java - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +public class Solution { + public ListNode detectCycle(ListNode head) { + if (head == null || head.next == null) { + return null; + } + ListNode slow = head; + ListNode fast = head; + + while (true) { + if (fast == null || fast.next == null) { + return null; + } + fast = fast.next.next; + slow = slow.next; + if (fast == slow) { + break; + } + } + slow = head; + while (slow != fast) { + slow = slow.next; + fast = fast.next; + + } + return fast; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/143.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/143.exercises/solution.md" index fc64393256df86b4218ce6ff6e712c3d07bb419b..7bbd3f3dd948a0c43b3e960358fddb7b83f1cba7 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/143.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/143.exercises/solution.md" @@ -40,7 +40,45 @@ ## template ```java - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public void reorderList(ListNode head) { + if (head == null) { + return; + } + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + + ListNode cur = slow.next, pre = null, next = null; + slow.next = null; + while (cur != null) { + next = cur.next; + cur.next = pre; + pre = cur; + cur = next; + } + + ListNode p1 = head, p2 = pre; + while (p1 != null && p2 != null) { + next = p2.next; + p2.next = p1.next; + p1.next = p2; + p1 = p2.next; + p2 = next; + } + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/146.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/146.exercises/solution.md" index d2792ab016b6cafc6558516e7622f4b8ff2b31e3..767313204815063b0d066e059f4e257bad488da0 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/146.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/146.exercises/solution.md" @@ -58,6 +58,67 @@ lRUCache.get(4); // 返回 4 ```java +class LRUCache { + + class Node { + Node prev, next; + int key, value; + + Node(int _key, int _value) { + key = _key; + value = _value; + } + } + + Node head = new Node(0, 0), tail = new Node(0, 0); + Map map = new HashMap<>(); + int max_len; + + public LRUCache(int capacity) { + max_len = capacity; + + head.next = tail; + tail.prev = head; + } + + public int get(int key) { + if (map.containsKey(key)) { + Node node = map.get(key); + remove(node); + add(node); + return node.value; + } else { + return -1; + } + } + + public void put(int key, int value) { + if (map.containsKey(key)) { + remove(map.get(key)); + } + + if (map.size() == max_len) { + remove(head.next); + } + add(new Node(key, value)); + } + + private void remove(Node node) { + map.remove(node.key); + + node.prev.next = node.next; + node.next.prev = node.prev; + } + + private void add(Node node) { + map.put(node.key, node); + Node pre_tail = tail.prev; + node.next = tail; + tail.prev = node; + pre_tail.next = node; + node.prev = pre_tail; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/147.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/147.exercises/solution.md" index a3bb99ad3491707734353bd335f24c1eb394e978..a1c627ff7de937377bff7d76d084e6ddef01ad4f 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/147.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/147.exercises/solution.md" @@ -34,8 +34,44 @@ ## template ```java - - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + + +class Solution { + public ListNode insertionSortList(ListNode head) { + if (head == null) + return head; + ListNode res = new ListNode(head.val); + ListNode left = head.next; + while ((left != null)) { + ListNode cur = left; + left = left.next; + + if (cur.val <= res.val) { + cur.next = res; + res = cur; + continue; + } + + ListNode p = res; + ListNode last = p; + while (p != null && p.val < cur.val) { + last = p; + p = p.next; + } + last.next = cur; + last.next.next = p; + } + return res; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/148.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/148.exercises/solution.md" index c0a282baeff6132861771027fc716eb3c27bd2d3..bb862a6a5095775fd7248d243c91d9a3050b1ed6 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/148.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/148.exercises/solution.md" @@ -44,7 +44,74 @@ ## template ```java - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public ListNode sortList(ListNode head) { + + if (head == null) + return head; + + return mergeSort(head); + } + + public ListNode mergeSort(ListNode head) { + + if (head.next == null) + return head; + + ListNode p1 = head; + ListNode p2 = head.next; + + if (p2 != null && p2.next != null) { + p1 = p1.next; + p2 = p2.next.next; + } + ListNode left = head; + ListNode right = p1.next; + p1.next = null; + + left = mergeSort(left); + right = mergeSort(right); + + return merge(left, right); + } + + public ListNode merge(ListNode left, ListNode right) { + + ListNode head = null; + + if (left.val < right.val) { + head = left; + left = left.next; + } else { + head = right; + right = right.next; + } + ListNode tmp = head; + + while (left != null && right != null) { + if (left.val < right.val) { + tmp.next = left; + left = left.next; + } else { + tmp.next = right; + right = right.next; + } + tmp = tmp.next; + } + + tmp.next = left != null ? left : right; + return head; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/150.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/150.exercises/solution.md" index 5db61932b28f1432ada8ff65d8a8e56d0e7c33d1..7339944e932643ecbd7116953be7be7c08af7f3d 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/150.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/150.exercises/solution.md" @@ -77,7 +77,26 @@ ## template ```java - +class Solution { + public int evalRPN(String[] tokens) { + Stack stack = new Stack<>(); + for (String s : tokens) { + if (s.equals("+")) { + stack.push(stack.pop() + stack.pop()); + } else if (s.equals("-")) { + stack.push(-stack.pop() + stack.pop()); + } else if (s.equals("*")) { + stack.push(stack.pop() * stack.pop()); + } else if (s.equals("/")) { + int num1 = stack.pop(); + stack.push(stack.pop() / num1); + } else { + stack.push(Integer.parseInt(s)); + } + } + return stack.pop(); + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/151.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/151.exercises/solution.md" index 63720138860829d3dd0fb82675278d8a66082a20..ff96068b2f4239b6a7ce387f86db7dad8ab17848 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/151.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/151.exercises/solution.md" @@ -78,6 +78,64 @@ ## template ```java +class Solution { + public StringBuilder trimSpaces(String s) { + int left = 0, right = s.length() - 1; + + while (left <= right && s.charAt(left) == ' ') + ++left; + + while (left <= right && s.charAt(right) == ' ') + --right; + + StringBuilder sb = new StringBuilder(); + while (left <= right) { + char c = s.charAt(left); + + if (c != ' ') + sb.append(c); + else if (sb.charAt(sb.length() - 1) != ' ') + sb.append(c); + + ++left; + } + return sb; + } + + public void reverse(StringBuilder sb, int left, int right) { + while (left < right) { + char tmp = sb.charAt(left); + sb.setCharAt(left++, sb.charAt(right)); + sb.setCharAt(right--, tmp); + } + } + + public void reverseEachWord(StringBuilder sb) { + int n = sb.length(); + int start = 0, end = 0; + + while (start < n) { + + while (end < n && sb.charAt(end) != ' ') + ++end; + + reverse(sb, start, end - 1); + + start = end + 1; + ++end; + } + } + + public String reverseWords(String s) { + StringBuilder sb = trimSpaces(s); + + reverse(sb, 0, sb.length() - 1); + + reverseEachWord(sb); + + return sb.toString(); + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/152.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/152.exercises/solution.md" index ccd835ad71dbe2ddac5908d570f5e9ec069213c3..3182a5a6aa427dc0dc0d5ae9e12762dbce45bfe2 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/152.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/152.exercises/solution.md" @@ -22,7 +22,23 @@ ```java - +class Solution { + public int maxProduct(int[] nums) { + int max = Integer.MIN_VALUE, imax = 1, imin = 1; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0) { + int tmp = imax; + imax = imin; + imin = tmp; + } + imax = Math.max(imax * nums[i], nums[i]); + imin = Math.min(imin * nums[i], nums[i]); + + max = Math.max(max, imax); + } + return max; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/153.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/153.exercises/solution.md" index 9fdbe99a88e759fda212b933e16062a951775bbb..cbe9dce6a6c8676d0db413439ae0117e0107d15a 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/153.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/153.exercises/solution.md" @@ -53,7 +53,21 @@ ```java - +class Solution { + public int findMin(int[] nums) { + int low = 0, high = nums.length - 1, mid = 0; + while (low <= high) { + mid = (low + high) / 2; + if (nums[mid] > nums[high]) + low = mid + 1; + else if (nums[mid] < nums[high]) + high = mid; + else + high--; + } + return nums[low]; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.md" deleted file mode 100644 index 4b5dc65e76b65547f56576229029ffad148ed322..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/156.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 上下翻转二叉树 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.json" "b/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.json" deleted file mode 100644 index 97e1a3853d34373a62b36499e6b10482de1d066a..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "d666ad4e4abf4b04952714416541e073", - "author": "csdn.net", - "keywords": "哈希表,字符串,滑动窗口" -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.md" deleted file mode 100644 index 0ab293a777da8c4e3bb27a75aa3e44b38f2e4aa3..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/159.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 至多包含两个不同字符的最长子串 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.md" deleted file mode 100644 index 94e700b02daa5b4f37ad4e0373fb780ccffcfc8c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/161.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 相隔为 1 的编辑距离 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/162.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/162.exercises/solution.md" index 91a91a7a2362e51b09a029416d543088e97823c6..1a84306bb29f7c9433361c5f1ea28f7276f102f6 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/162.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/162.exercises/solution.md" @@ -41,7 +41,22 @@ ```java - +class Solution { + public int findPeakElement(int[] nums) { + if (nums.length == 1) + return 0; + int len = nums.length; + int low = 0, mid = 0, high = nums.length - 1; + while (low < high) { + mid = (low + high) / 2; + if (nums[mid] > nums[mid + 1]) + high = mid; + else + low = mid + 1; + } + return low; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/165.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/165.exercises/solution.md" index 7a93dc234b921d4cc6361ebe462af4211d954765..e146b43b4e859b12a10a0e24bc108430cd28f4bf 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/165.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/165.exercises/solution.md" @@ -70,6 +70,23 @@ ```java +class Solution { + public int compareVersion(String version1, String version2) { + String[] a1 = version1.split("\\."); + String[] a2 = version2.split("\\."); + + for (int n = 0; n < Math.max(a1.length, a2.length); n++) { + int i = (n < a1.length ? Integer.valueOf(a1[n]) : 0); + int j = (n < a2.length ? Integer.valueOf(a2[n]) : 0); + if (i < j) + return -1; + else if (i > j) + return 1; + } + return 0; + } +} + ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/166.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/166.exercises/solution.md" index b318354ba397f527835fff82d172518aafd2d10f..2d9403f59deca559addc9a906fc9842aa2e00f21 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/166.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/166.exercises/solution.md" @@ -59,7 +59,35 @@ ```java - +class Solution { + public String fractionToDecimal(int numerator, int denominator) { + if (numerator == 0) + return "0"; + StringBuilder str = new StringBuilder(); + if (numerator < 0 ^ denominator < 0) + str.append('-'); + long dividend = Math.abs(Long.valueOf(numerator)); + long divisor = Math.abs(Long.valueOf(denominator)); + str.append(String.valueOf(dividend / divisor)); + long remainter = dividend % divisor; + if (remainter == 0) + return str.toString(); + str.append('.'); + Map map = new HashMap<>(); + while (remainter != 0) { + if (map.containsKey(remainter)) { + str.insert(map.get(remainter), "("); + str.append(")"); + break; + } + map.put(remainter, str.length()); + remainter *= 10; + str.append(String.valueOf(remainter / divisor)); + remainter %= divisor; + } + return str.toString(); + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/173.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/173.exercises/solution.md" index 05bedb0d9d905227ae5b78c91d280fbede9ec03c..807e519acef3ad1bb5df9bdc79413605c12f6baa 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/173.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/173.exercises/solution.md" @@ -61,6 +61,48 @@ bSTIterator.hasNext(); // 返回 False ## template ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class BSTIterator { + LinkedList stack = new LinkedList<>(); + + public BSTIterator(TreeNode root) { + TreeNode cur = root; + while (cur != null) { + stack.push(cur); + cur = cur.left; + } + } + + public int next() { + TreeNode n = stack.pop(); + TreeNode cur = n.right; + while (cur != null) { + stack.push(cur); + cur = cur.left; + } + return n.val; + } + + public boolean hasNext() { + return !stack.isEmpty(); + } +} + +/** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.json" "b/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.json" deleted file mode 100644 index 521ae862955a1c1da870a6a1ece61ed7474ca472..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "8782ed2269774b5397d896d5f6542f36", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.md" deleted file mode 100644 index be5401389823e9696c3e1441923008c9408a3ddb..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/177.exercises/solution.md" +++ /dev/null @@ -1,55 +0,0 @@ -# 第N高的薪水 - -

编写一个 SQL 查询,获取 Employee 表中第 高的薪水(Salary)。

- -
+----+--------+
-| Id | Salary |
-+----+--------+
-| 1  | 100    |
-| 2  | 200    |
-| 3  | 300    |
-+----+--------+
-
- -

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 高的薪水,那么查询应返回 null

- -
+------------------------+
-| getNthHighestSalary(2) |
-+------------------------+
-| 200                    |
-+------------------------+
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/config.json" deleted file mode 100644 index 5cb1b35d9f824b794fe65f9e60ce7265b78ce4ce..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-82e2c01379fa42e9a9914367270ff513", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.json" "b/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.json" deleted file mode 100644 index 0827f10492e8aa143d444192e1589f48050efcab..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "5c4c5b520d1045db809a310366487eec", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.md" deleted file mode 100644 index a40402e396ccd61574b33730f2acfb67e3883276..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/178.exercises/solution.md" +++ /dev/null @@ -1,67 +0,0 @@ -# 分数排名 - -

编写一个 SQL 查询来实现分数排名。

- -

如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。

- -
+----+-------+
-| Id | Score |
-+----+-------+
-| 1  | 3.50  |
-| 2  | 3.65  |
-| 3  | 4.00  |
-| 4  | 3.85  |
-| 5  | 4.00  |
-| 6  | 3.65  |
-+----+-------+
-
- -

例如,根据上述给定的 Scores 表,你的查询应该返回(按分数从高到低排列):

- -
+-------+------+
-| Score | Rank |
-+-------+------+
-| 4.00  | 1    |
-| 4.00  | 1    |
-| 3.85  | 2    |
-| 3.65  | 3    |
-| 3.65  | 3    |
-| 3.50  | 4    |
-+-------+------+
-
- -

重要提示:对于 MySQL 解决方案,如果要转义用作列名的保留字,可以在关键字之前和之后使用撇号。例如 `Rank`

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/179.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/179.exercises/solution.md" index 174aa61d143d95aea16ce513b4e22352769a0652..f562d2d23b4befacdd84ecfa9d5c9d63d0eab245 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/179.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/179.exercises/solution.md" @@ -46,7 +46,38 @@ ## template ```java - +class Solution { + public String largestNumber(int[] nums) { + + String[] str = new String[nums.length]; + + for (int i = 0; i < nums.length; i++) + str[i] = String.valueOf(nums[i]); + + Arrays.parallelSort(str); + + for (int i = 1; i < str.length; i++) + for (int j = 0; j < i; j++) { + if (str[i].length() > str[j].length() && str[i].substring(0, str[j].length()).equals(str[j])) { + StringBuilder str1 = new StringBuilder(); + StringBuilder str2 = new StringBuilder(); + str1.append(str[i] + str[j]); + str2.append(str[j] + str[i]); + if (str2.toString().compareTo(str1.toString()) > 0) { + String tmp = str[i]; + str[i] = str[j]; + str[j] = tmp; + } + } + } + StringBuilder ans = new StringBuilder(); + + for (int i = str.length - 1; i >= 0; i--) + ans.append(str[i]); + + return ans.charAt(0) == '0' ? "0" : ans.toString(); + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/config.json" deleted file mode 100644 index fba3c2994e62ec5828f017888b991067cff44c94..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-5f05b742e35445149011055afed9fa9e", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.json" "b/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.json" deleted file mode 100644 index 8a1052979f437b09e784cd1594bd01a9f422e18e..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "bdda9c0e7f924dd2b600b11de361e606", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.md" deleted file mode 100644 index fd8c3be80c93fdb141d7d1c9cb9594fcdaae41b8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/180.exercises/solution.md" +++ /dev/null @@ -1,81 +0,0 @@ -# 连续出现的数字 - -

表:Logs

- -
-+-------------+---------+
-| Column Name | Type    |
-+-------------+---------+
-| id          | int     |
-| num         | varchar |
-+-------------+---------+
-id 是这个表的主键。
- -

 

- -

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

- -

返回的结果表中的数据可以按 任意顺序 排列。

- -

 

- -

查询结果格式如下面的例子所示:

- -

 

- -
-Logs 表:
-+----+-----+
-| Id | Num |
-+----+-----+
-| 1  | 1   |
-| 2  | 1   |
-| 3  | 1   |
-| 4  | 2   |
-| 5  | 1   |
-| 6  | 2   |
-| 7  | 2   |
-+----+-----+
-
-Result 表:
-+-----------------+
-| ConsecutiveNums |
-+-----------------+
-| 1               |
-+-----------------+
-1 是唯一连续出现至少三次的数字。
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/config.json" deleted file mode 100644 index 44292f68871444fadb38798f6223bdecf149ee23..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-868aee1532de49d78dc95a814bd83b28", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.json" "b/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.json" deleted file mode 100644 index c03374c69e4257f78824dcbbf0fe38fc1884a191..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "9d681ac067df42ebad48106a3a426611", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.md" deleted file mode 100644 index 3c0319bfb8dcc8e4a8869bf69bbfe19976523626..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/184.exercises/solution.md" +++ /dev/null @@ -1,70 +0,0 @@ -# 部门工资最高的员工 - -

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。

- -
+----+-------+--------+--------------+
-| Id | Name  | Salary | DepartmentId |
-+----+-------+--------+--------------+
-| 1  | Joe   | 70000  | 1            |
-| 2  | Jim   | 90000  | 1            |
-| 3  | Henry | 80000  | 2            |
-| 4  | Sam   | 60000  | 2            |
-| 5  | Max   | 90000  | 1            |
-+----+-------+--------+--------------+
- -

Department 表包含公司所有部门的信息。

- -
+----+----------+
-| Id | Name     |
-+----+----------+
-| 1  | IT       |
-| 2  | Sales    |
-+----+----------+
- -

编写一个 SQL 查询,找出每个部门工资最高的员工。对于上述表,您的 SQL 查询应返回以下行(行的顺序无关紧要)。

- -
+------------+----------+--------+
-| Department | Employee | Salary |
-+------------+----------+--------+
-| IT         | Max      | 90000  |
-| IT         | Jim      | 90000  |
-| Sales      | Henry    | 80000  |
-+------------+----------+--------+
- -

解释:

- -

Max 和 Jim 在 IT 部门的工资都是最高的,Henry 在销售部的工资最高。

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/config.json" deleted file mode 100644 index 4df94a5422e93b4aaa40aca078a638c88cc8ca66..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-13e12e894dc14a74acbdd071409e92a6", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.md" deleted file mode 100644 index 43980f895488e2e77deb4cd732b46f52da2e2f18..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/186.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 翻转字符串里的单词 II - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/187.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/187.exercises/solution.md" index 3e02b37e22a9bbd1c543bc054a0c3af579b1c7b2..aebf1f37ff7d3ab440e2277b494967251ee3e1ac 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/187.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/187.exercises/solution.md" @@ -33,6 +33,18 @@ ## template ```java +class Solution { + public List findRepeatedDnaSequences(String s) { + Set set = new HashSet<>(); + Set help = new HashSet<>(); + for (int i = 0; i <= s.length() - 10; i++) { + String cur = s.substring(i, i + 10); + if (!set.add(cur)) + help.add(cur); + } + return new ArrayList(help); + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/189.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/189.exercises/solution.md" index 2137b07c035c1d272d9103b19efb4ad7e2cf7165..c7ceee910177fc7abcd97752b49e592519022f69 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/189.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/189.exercises/solution.md" @@ -51,7 +51,33 @@ ```java - +class Solution { + public void rotate(int[] nums, int k) { + if (k > nums.length) { + k = k % nums.length; + } + int mark = nums.length - k; + int mark2 = (nums.length + mark) / 2; + for (int i = 0; i < (mark + 1) / 2; i++) { + swap(nums, i, mark - 1 - i); + } + + for (int j = mark; j < mark2; j++) { + swap(nums, j, nums.length + mark - 1 - j); + } + + for (int index = 0; index < nums.length / 2; index++) { + swap(nums, index, nums.length - 1 - index); + } + + } + + public static void swap(int[] nums, int a, int b) { + int temp = nums[a]; + nums[a] = nums[b]; + nums[b] = temp; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/config.json" deleted file mode 100644 index 51153b24f63e6f497613e4bbbf415063c4277b89..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-67548272ccf143f9965b7e297433bbe9", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.md" deleted file mode 100644 index c53711ae2ef617c2ad62f6f6dd49ff87a50faa6d..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/192.exercises/solution.md" +++ /dev/null @@ -1,68 +0,0 @@ -# 统计词频 - -

写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。

- -

为了简单起见,你可以假设:

- -
    -
  • words.txt只包括小写字母和 ' ' 。
  • -
  • 每个单词只由小写字母组成。
  • -
  • 单词间由一个或多个空格字符分隔。
  • -
- -

示例:

- -

假设 words.txt 内容如下:

- -
the day is sunny the the
-the sunny is is
-
- -

你的脚本应当输出(以词频降序排列):

- -
the 4
-is 3
-sunny 2
-day 1
-
- -

说明:

- -
    -
  • 不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。
  • -
  • 你可以使用一行 Unix pipes 实现吗?
  • -
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/config.json" "b/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/config.json" deleted file mode 100644 index 251d5369714b7b2ed9635242f600044c18cf4492..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-f8db0f19c87246cab8b6e2536a83bd36", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.md" deleted file mode 100644 index a59e145d285122739c6b804bce77e92de5755f71..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\344\270\255\347\255\211/java/194.exercises/solution.md" +++ /dev/null @@ -1,58 +0,0 @@ -# 转置文件 - -

给定一个文件 file.txt,转置它的内容。

- -

你可以假设每行列数相同,并且每个字段由 ' ' 分隔。

- -

 

- -

示例:

- -

假设 file.txt 文件内容如下:

- -
-name age
-alice 21
-ryan 30
-
- -

应当输出:

- -
-name alice ryan
-age 21 30
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/198.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/198.exercises/solution.md" index 6b4f0c91b411be0864aff084ca7790963a333bdb..b9eee115a00e74b68db40f510ad4172922dcd099 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/198.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/198.exercises/solution.md" @@ -36,6 +36,22 @@ ## template ```java +class Solution { + public int rob(int[] nums) { + if (nums.length == 0) + return 0; + if (nums.length == 1) + return nums[0]; + int result[] = new int[nums.length]; + result[0] = nums[0]; + result[1] = Math.max(nums[0], nums[1]); + for (int i = 2; i < result.length; i++) { + result[i] = Math.max(result[i - 1], result[i - 2] + nums[i]); + } + + return result[result.length - 1]; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/199.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/199.exercises/solution.md" index 4d28a0790f6fe768f5f3fe979b5a85ecb9d73af2..0f1f99f0005c3f00123078903285b2c78b1962ae 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/199.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/199.exercises/solution.md" @@ -40,7 +40,37 @@ ## template ```java - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List rightSideView(TreeNode root) { + List list = new ArrayList(); + if (root == null) + return list; + + list.add(root.val); + List list1 = rightSideView(root.right); + List list2 = rightSideView(root.left); + + list.addAll(list1); + + if (list1.size() < list2.size()) { + for (int i = list1.size(); i < list2.size(); i++) { + list.add(list2.get(i)); + } + } + + return list; + } +} ``` diff --git "a/data_source/exercises/\344\270\255\347\255\211/java/200.exercises/solution.md" "b/data_source/exercises/\344\270\255\347\255\211/java/200.exercises/solution.md" index 267259a2cf21e828133bcdd7d83d3e5cc3d736fb..8fda15e7537ff93184c5afec2e61789bd11f074a 100644 --- "a/data_source/exercises/\344\270\255\347\255\211/java/200.exercises/solution.md" +++ "b/data_source/exercises/\344\270\255\347\255\211/java/200.exercises/solution.md" @@ -48,7 +48,32 @@ ```java - +class Solution { + public int numIslands(char[][] grid) { + int islandNum = 0; + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[0].length; j++) { + if (grid[i][j] == '1') { + infect(grid, i, j); + islandNum++; + } + } + } + return islandNum; + } + + public void infect(char[][] grid, int i, int j) { + if (i < 0 || i >= grid.length || + j < 0 || j >= grid[0].length || grid[i][j] != '1') { + return; + } + grid[i][j] = '2'; + infect(grid, i + 1, j); + infect(grid, i - 1, j); + infect(grid, i, j + 1); + infect(grid, i, j - 1); + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/115.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/115.exercises/solution.md" index 6d52eb263563106d8627fc294924349402e1eeed..931f1d2e6cbd2114b2839fa7e13edc6e14960103 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/115.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/115.exercises/solution.md" @@ -46,7 +46,26 @@ ## template ```java - +class Solution { + public int numDistinct(String s, String t) { + int n1 = s.length(); + int n2 = t.length(); + int[][] dp = new int[n1 + 1][n2 + 1]; + for (int i = 0; i <= n1; i++) { + dp[i][0] = 1; + } + for (int i = 1; i <= n1; i++) { + for (int j = 1; j <= n2; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + } else { + dp[i][j] = dp[i - 1][j]; + } + } + } + return dp[n1][n2]; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/123.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/123.exercises/solution.md" index 2b7c782129ea52e5d603d84648fd87eac9d37949..32164c153cc964410dce342f6a43c25923f70f70 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/123.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/123.exercises/solution.md" @@ -53,7 +53,45 @@ ## template ```java - +public class Solution { + public int maxProfit(int[] prices) { + int result = 0; + if (prices.length == 0) { + return result; + } + int firstDealSell; + int secondDealSell; + for (secondDealSell = prices.length - 1; secondDealSell > 0; secondDealSell--) { + if (prices[secondDealSell - 1] < prices[secondDealSell]) { + break; + } + } + for (firstDealSell = 1; firstDealSell < prices.length; firstDealSell++) { + while (firstDealSell + 1 < prices.length && prices[firstDealSell + 1] >= prices[firstDealSell]) { + firstDealSell++; + } + int result1 = maxProfit(prices, 0, firstDealSell); + int result2 = maxProfit(prices, firstDealSell + 1, secondDealSell); + if (result1 + result2 > result) { + result = result1 + result2; + } + } + return result; + } + + private int maxProfit(int[] prices, int left, int right) { + int result = 0; + if (right - left < 1) { + return result; + } + int minPrice = prices[left]; + for (int i = left + 1; i <= right; i++) { + result = Math.max(result, prices[i] - minPrice); + minPrice = Math.min(minPrice, prices[i]); + } + return result; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/124.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/124.exercises/solution.md" index b3b66fae1e2a1a6eb7a8e11a15fa658715b4f24a..24727689f7bc9a16655053faf8f4e467f4efbb89 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/124.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/124.exercises/solution.md" @@ -36,8 +36,29 @@ ## template ```java +class Solution { + int maxSum = Integer.MIN_VALUE; + public int maxPathSum(TreeNode root) { + maxGain(root); + return maxSum; + } + public int maxGain(TreeNode node) { + if (node == null) { + return 0; + } + + int leftGain = Math.max(maxGain(node.left), 0); + int rightGain = Math.max(maxGain(node.right), 0); + + int priceNewpath = node.val + leftGain + rightGain; + + maxSum = Math.max(maxSum, priceNewpath); + + return node.val + Math.max(leftGain, rightGain); + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/126.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/126.exercises/solution.md" index 5549c84f89a92c7bd67a567328fd5cbdbe4bfc57..5e9df658fda74e08e816bdc84ce01e08c09fb451 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/126.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/126.exercises/solution.md" @@ -52,6 +52,96 @@ ## template ```java +class Solution { + public List> findLadders(String beginWord, String endWord, List wordList) { + + List> res = new ArrayList<>(); + if (wordList == null) + return res; + + Set dicts = new HashSet<>(wordList); + if (!dicts.contains(endWord)) + return res; + if (dicts.contains(beginWord)) + dicts.remove(beginWord); + + Set endList = new HashSet<>(), + beginList = new HashSet<>(); + + Map> map = new HashMap<>(); + beginList.add(beginWord); + endList.add(endWord); + bfs(map, beginList, endList, beginWord, endWord, dicts, false); + + List subList = new ArrayList<>(); + subList.add(beginWord); + dfs(map, res, subList, beginWord, endWord); + return res; + } + + void dfs(Map> map, + List> result, List subList, + String beginWord, String endWord) { + if (beginWord.equals(endWord)) { + result.add(new ArrayList<>(subList)); + return; + } + if (!map.containsKey(beginWord)) { + return; + } + for (String word : map.get(beginWord)) { + subList.add(word); + dfs(map, result, subList, word, endWord); + subList.remove(subList.size() - 1); + } + } + + void bfs(Map> map, Set beginList, Set endList, String beginWord, + String endWord, Set wordList, boolean reverse) { + if (beginList.size() == 0) + return; + wordList.removeAll(beginList); + boolean finish = false; + Set temp = new HashSet<>(); + for (String str : beginList) { + char[] charr = str.toCharArray(); + for (int chI = 0; chI < charr.length; chI++) { + char old = charr[chI]; + for (char ch = 'a'; ch <= 'z'; ch++) { + if (ch == old) + continue; + charr[chI] = ch; + String newstr = new String(charr); + if (!wordList.contains(newstr)) { + continue; + } + + if (endList.contains(newstr)) { + finish = true; + } else { + temp.add(newstr); + } + + String key = reverse ? newstr : str; + String value = reverse ? str : newstr; + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(value); + + } + charr[chI] = old; + } + } + if (!finish) { + if (temp.size() > endList.size()) { + bfs(map, endList, temp, beginWord, endWord, wordList, !reverse); + } else { + bfs(map, temp, endList, beginWord, endWord, wordList, reverse); + } + } + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/127.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/127.exercises/solution.md" index 52825bf86d63d3ca716988ee9ae6e8377a9dd3a8..1714c4726473bc99d9d6a2dbcddb35a050377010 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/127.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/127.exercises/solution.md" @@ -45,7 +45,107 @@ ## template ```java - +public class Solution { + + HashMap> mapPositive = new HashMap>(); + HashMap> mapNegative = new HashMap>(); + HashMap mapVisit = new HashMap(); + + int result = Integer.MAX_VALUE; + + public int ladderLength(String beginWord, String endWord, List wordList) { + + for (int i = 0; i < wordList.size(); i++) { + addWordToMap(wordList.get(i)); + } + addWordToMap(beginWord); + mapVisit.put(beginWord, true); + findEnd(beginWord, endWord, 1); + if (result == Integer.MAX_VALUE) { + return 0; + } + return result; + } + + private void addWordToMap(String word) { + int length = word.length(); + if (length == 0) { + return; + } + char[] chars = word.toCharArray(); + for (int i = 0; i < length; i++) { + char now = chars[i]; + chars[i] = '*'; + String regex = new String(chars); + List list; + + if (mapPositive.containsKey(word)) { + list = mapPositive.get(word); + } else { + list = new ArrayList(); + mapPositive.put(word, list); + } + list.add(regex); + + if (mapNegative.containsKey(regex)) { + list = mapNegative.get(regex); + } else { + list = new ArrayList(); + mapNegative.put(regex, list); + } + list.add(word); + + chars[i] = now; + } + mapVisit.put(word, false); + } + + private void findEnd(String nowWord, String endWord, int nowStep) { + if (endWord.equals(nowWord)) { + if (nowStep < result) { + result = nowStep; + } + return; + } + List candidates = getCandidates(nowWord); + if (candidates == null || candidates.size() == 0) { + return; + } + for (int i = 0; i < candidates.size(); i++) { + String now = candidates.get(i); + mapVisit.put(now, true); + findEnd(now, endWord, nowStep + 1); + mapVisit.put(now, false); + } + } + + private List getCandidates(String word) { + List list = mapPositive.get(word); + if (list == null) { + return new ArrayList(); + } + Set set = new HashSet(); + for (int i = 0; i < list.size(); i++) { + String regex = list.get(i); + List regexList = mapNegative.get(regex); + if (regexList == null) { + continue; + } + for (int j = 0; j < regexList.size(); j++) { + String now = regexList.get(j); + if (mapVisit.get(now) == false) { + set.add(now); + } + } + } + List candidates = new ArrayList(); + for (String string : set) { + candidates.add(string); + } + return candidates; + } + +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/132.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/132.exercises/solution.md" index 3eecd071568a84c0f3887cf614661b9431f1ecee..34b6238a9eb88b1fb0a0c4e32769ce397c4e2bf3 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/132.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/132.exercises/solution.md" @@ -45,6 +45,33 @@ ## template ```java +class Solution { + public int minCut(String s) { + int n = s.length(); + boolean[][] g = new boolean[n][n]; + for (int i = 0; i < n; i++) { + Arrays.fill(g[i], true); + } + for (int i = n - 1; i >= 0; i--) { + for (int j = i + 1; j < n; j++) { + g[i][j] = (s.charAt(i) == s.charAt(j) && g[i + 1][j - 1]); + } + } + int[] f = new int[n]; + Arrays.fill(f, Integer.MAX_VALUE); + for (int i = 0; i < n; i++) { + if (g[0][i]) { + f[i] = 0; + } + for (int j = 0; j < i; j++) { + if (g[j + 1][i]) { + f[i] = Math.min(f[i], f[j] + 1); + } + } + } + return f[n - 1]; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/135.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/135.exercises/solution.md" index 5ed72918fb039478f354180825afe055c54cd384..03f6f20d00682244e1e200d84e707b2d23366d9d 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/135.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/135.exercises/solution.md" @@ -33,7 +33,39 @@ ## template ```java - +class Solution { + public int candy(int[] ratings) { + int len = ratings.length; + int[] left = new int[len]; + int[] right = new int[len]; + + left[0] = 1; + right[len - 1] = 1; + + for (int i = 1; i < len; i++) { + if (ratings[i] > ratings[i - 1]) { + left[i] = left[i - 1] + 1; + } else { + left[i] = 1; + } + } + + for (int i = len - 2; i >= 0; i--) { + if (ratings[i] > ratings[i + 1]) { + right[i] = right[i + 1] + 1; + } else { + right[i] = 1; + } + } + + int res = 0; + for (int i = 0; i < len; i++) { + res += Math.max(left[i], right[i]); + } + + return res; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/140.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/140.exercises/solution.md" index 2679949ebae124b372e6ca72f47a9d7defb92523..a82f1a19c361f3bc8c1480d4bf81a7cf5cb3848b 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/140.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/140.exercises/solution.md" @@ -48,6 +48,52 @@ wordDict = ["cats", "dog", "sand", "and" ## template ```java +class Solution { + public List wordBreak(String s, List wordDict) { + + List res = new ArrayList<>(); + int max = 0, min = Integer.MAX_VALUE; + Set set = new HashSet<>(); + for (String word : wordDict) { + set.add(word); + max = Integer.max(max, word.length()); + min = Integer.min(min, word.length()); + } + + boolean f[] = new boolean[s.length() + 1]; + f[0] = true; + for (int i = 1; i < s.length() + 1; i++) { + for (int j = Math.max(i - max, 0); j <= i - min; j++) { + if (f[j] && set.contains(s.substring(j, i))) { + f[i] = true; + break; + } + } + } + if (f[s.length()]) { + dfs(s, res, new StringBuilder(), set, 0, max, min); + } + return res; + } + + private void dfs(String s, List res, StringBuilder sb, Set set, int index, int max, int min) { + if (index == s.length()) { + sb.deleteCharAt(sb.length() - 1); + res.add(sb.toString()); + return; + } + String str; + int size; + for (int i = index + min; i <= s.length() && i <= index + max; i++) { + if (set.contains(str = s.substring(index, i))) { + size = sb.length(); + sb.append(str).append(' '); + dfs(s, res, sb, set, i, max, min); + sb.delete(size, sb.length()); + } + } + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/149.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/149.exercises/solution.md" index 39f892d2e52cd8232bfc5e897b9320a2259bfbc4..5e877534173ec580e3a7c9d56fd761897dbf4e67 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/149.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/149.exercises/solution.md" @@ -33,6 +33,46 @@ ## template ```java +class Solution { + public int maxPoints(int[][] points) { + int n = points.length; + if (n == 0) + return 0; + if (n == 1) + return 1; + int res = 0; + for (int i = 0; i < n - 1; i++) { + Map slope = new HashMap<>(); + int repeat = 0; + int tmp_max = 0; + for (int j = i + 1; j < n; j++) { + int dy = points[i][1] - points[j][1]; + int dx = points[i][0] - points[j][0]; + if (dy == 0 && dx == 0) { + repeat++; + continue; + } + int g = gcd(dy, dx); + if (g != 0) { + dy /= g; + dx /= g; + } + String tmp = String.valueOf(dy) + "/" + String.valueOf(dx); + slope.put(tmp, slope.getOrDefault(tmp, 0) + 1); + tmp_max = Math.max(tmp_max, slope.get(tmp)); + } + res = Math.max(res, repeat + tmp_max + 1); + } + return res; + } + + private int gcd(int dy, int dx) { + if (dx == 0) + return dy; + else + return gcd(dx, dy % dx); + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/154.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/154.exercises/solution.md" index dfd86d16e08b752c7494f7b9d4a0342f3a1579da..d762035c38c0a79acf5a52632d91ace6f2a28258 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/154.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/154.exercises/solution.md" @@ -50,7 +50,21 @@ ## template ```java - +class Solution { + public int findMin(int[] nums) { + int left = 0, right = nums.length - 1; + while (left < right) { + int mid = (left + right) / 2; + if (nums[mid] > nums[right]) + left = mid + 1; + else if (nums[mid] < nums[right]) + right = mid; + else + right = right - 1; + } + return nums[left]; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/config.json" deleted file mode 100644 index 7f7815c60cd2046ad9e0d3b6c2feca330899dd9e..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-3547c19dcfb2426a8a3c2110558880b4", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.json" deleted file mode 100644 index e2bb68fd0d3bd5f0f90e3fc58ccc1c04ab7fc992..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "d68fa5ce0e184f30aab48fc355489379", - "author": "csdn.net", - "keywords": "字符串,交互,模拟" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.md" deleted file mode 100644 index ca8ef2e7e6ac9bca3c5a522aec2592de9b3521aa..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/158.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 用 Read4 读取 N 个字符 II - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/164.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/164.exercises/solution.md" index 06d03823d74d6d5b68ece491d9a56d9aabb6be3e..688c4fd93f736cdfde2c008fdcfb208ea543c974 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/164.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/164.exercises/solution.md" @@ -27,7 +27,21 @@ ## template ```java - +class Solution { + public int maximumGap(int[] nums) { + int len = nums.length; + Arrays.sort(nums); + int temp = 0; + int res = 0; + if (len < 2) + return 0; + for (int i = 0; i < len - 1; i++) { + temp = nums[i + 1] - nums[i]; + res = Math.max(res, temp); + } + return res; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/174.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/174.exercises/solution.md" index b18407be98c66a171481c591178f86f233b7ec0c..c1ff99dfca46abd4a0f26eac429a291b6428835d 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/174.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/174.exercises/solution.md" @@ -62,7 +62,28 @@ table.dungeon, .dungeon th, .dungeon td { ```java - +class Solution { + public int calculateMinimumHP(int[][] dungeon) { + int row = dungeon.length; + int col = dungeon[0].length; + + int[][] dp = new int[row][col]; + for (int i = row - 1; i >= 0; i--) { + for (int j = col - 1; j >= 0; j--) { + if (i == row - 1 && j == col - 1) { + dp[i][j] = Math.max(1, 1 - dungeon[i][j]); + } else if (i == row - 1) { + dp[i][j] = Math.max(1, dp[i][j + 1] - dungeon[i][j]); + } else if (j == col - 1) { + dp[i][j] = Math.max(1, dp[i + 1][j] - dungeon[i][j]); + } else { + dp[i][j] = Math.max(1, Math.min(dp[i + 1][j], dp[i][j + 1]) - dungeon[i][j]); + } + } + } + return dp[0][0]; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/config.json" deleted file mode 100644 index 152973c4ef827a40c31dcb696ce742d73024fab8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-f143b2dac1554fb283ee1c82d8cbcbc7", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.json" deleted file mode 100644 index 8db09dc10b831a578943587aa44186e58de46ef8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "41dad2700ae94a6f8f696c95f7bc9f02", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.md" deleted file mode 100644 index 9f7019b054215588b32d2290a44f1d641ce5ea85..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/185.exercises/solution.md" +++ /dev/null @@ -1,75 +0,0 @@ -# 部门工资前三高的所有员工 - -

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId

- -
+----+-------+--------+--------------+
-| Id | Name  | Salary | DepartmentId |
-+----+-------+--------+--------------+
-| 1  | Joe   | 85000  | 1            |
-| 2  | Henry | 80000  | 2            |
-| 3  | Sam   | 60000  | 2            |
-| 4  | Max   | 90000  | 1            |
-| 5  | Janet | 69000  | 1            |
-| 6  | Randy | 85000  | 1            |
-| 7  | Will  | 70000  | 1            |
-+----+-------+--------+--------------+
- -

Department 表包含公司所有部门的信息。

- -
+----+----------+
-| Id | Name     |
-+----+----------+
-| 1  | IT       |
-| 2  | Sales    |
-+----+----------+
- -

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

- -
+------------+----------+--------+
-| Department | Employee | Salary |
-+------------+----------+--------+
-| IT         | Max      | 90000  |
-| IT         | Randy    | 85000  |
-| IT         | Joe      | 85000  |
-| IT         | Will     | 70000  |
-| Sales      | Henry    | 80000  |
-| Sales      | Sam      | 60000  |
-+------------+----------+--------+
- -

解释:

- -

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/188.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/188.exercises/solution.md" index 805439cbbc4912b067e1fb8dafb1b7193499d09d..a87799eb8ac453fad218e90b90e145b1badac720 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/188.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/188.exercises/solution.md" @@ -37,6 +37,36 @@ ## template ```java +class Solution { + public int maxProfit(int k, int[] prices) { + if (k < 1) + return 0; + if (k >= prices.length / 2) + return greedy(prices); + int[][] t = new int[k][2]; + for (int i = 0; i < k; ++i) + t[i][0] = Integer.MIN_VALUE; + for (int p : prices) { + t[0][0] = Math.max(t[0][0], -p); + t[0][1] = Math.max(t[0][1], t[0][0] + p); + for (int i = 1; i < k; ++i) { + + t[i][0] = Math.max(t[i][0], t[i - 1][1] - p); + t[i][1] = Math.max(t[i][1], t[i][0] + p); + } + } + return t[k - 1][1]; + } + + private int greedy(int[] prices) { + int max = 0; + for (int i = 1; i < prices.length; ++i) { + if (prices[i] > prices[i - 1]) + max += prices[i] - prices[i - 1]; + } + return max; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/212.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/212.exercises/solution.md" index 4558e3fdd4eaed174a879a7cb0f359e5db57cd9f..8062a438d5695a18718e8d68636842ff300d40c0 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/212.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/212.exercises/solution.md" @@ -40,7 +40,82 @@ ```java - +class Solution { + static int[][] d = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; + static Map notExistWords = new HashMap<>(); + + public List findWords(char[][] board, String[] words) { + List ans = new ArrayList<>(); + Arrays.sort(words); + notExistWords.clear(); + + for (int i = 0; i < words.length; i++) { + String word = words[i]; + + if (i > 0 && word.equals(words[i - 1])) + continue; + + boolean notExistFlag = false; + for (int j = 1; j < word.length(); j++) { + if (notExistWords.containsKey(word.substring(0, j + 1))) { + notExistFlag = true; + break; + } + } + if (notExistFlag) + continue; + + if (exist(board, word)) { + ans.add(word); + } else { + notExistWords.put(word, false); + } + } + + return ans; + } + + public boolean exist(char[][] board, String word) { + int m = board.length; + if (m == 0) + return false; + int n = board[0].length; + if (n == 0) + return false; + if (word.equals("")) + return true; + boolean[][] f = new boolean[m][n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (word.charAt(0) == board[i][j]) { + f[i][j] = true; + boolean flag = dfs(i, j, 1, board, word, m, n, f); + if (flag) + return true; + f[i][j] = false; + } + } + } + return false; + } + + private boolean dfs(int i, int j, int k, char[][] board, String word, int m, int n, boolean[][] f) { + if (k == word.length()) { + return true; + } + for (int l = 0; l < 4; l++) { + if (i + d[l][0] < m && j + d[l][1] < n && i + d[l][0] >= 0 && j + d[l][1] >= 0 + && board[i + d[l][0]][j + d[l][1]] == word.charAt(k) && !f[i + d[l][0]][j + d[l][1]]) { + f[i + d[l][0]][j + d[l][1]] = true; + boolean flag = dfs(i + d[l][0], j + d[l][1], k + 1, board, word, m, n, f); + if (flag) + return true; + f[i + d[l][0]][j + d[l][1]] = false; + } + } + return false; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/214.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/214.exercises/solution.md" index 3338ea620c4a762e5a77af394b3c029f40cf19da..c7ab712c0e5605ebafe15b19364c4037d6147c09 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/214.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/214.exercises/solution.md" @@ -31,7 +31,32 @@ ## template ```java - +class Solution { + public static String shortestPalindrome(String s) { + StringBuilder r = new StringBuilder(s).reverse(); + String str = s + "#" + r; + int[] next = next(str); + + String prefix = r.substring(0, r.length() - next[str.length()]); + return prefix + s; + } + + private static int[] next(String P) { + int[] next = new int[P.length() + 1]; + next[0] = -1; + int k = -1; + int i = 1; + + while (i < next.length) { + if (k == -1 || P.charAt(k) == P.charAt(i - 1)) { + next[i++] = ++k; + } else { + k = next[k]; + } + } + return next; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/218.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/218.exercises/solution.md" index b86b9b582927304eeaf99e2764e622b561eec51d..d94cce264e984990d923a5c6c9e06572e02e9527 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/218.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/218.exercises/solution.md" @@ -47,7 +47,35 @@ ## template ```java - +class Solution { + public List> getSkyline(int[][] buildings) { + int n = buildings.length, m = n << 1; + List> ans = new ArrayList>(); + + int[] boundaries = new int[m]; + for (int i = 0; i < n; i++) { + boundaries[i << 1] = buildings[i][0]; + boundaries[(i << 1) + 1] = buildings[i][1]; + } + Arrays.sort(boundaries); + + PriorityQueue pq = new PriorityQueue((a, b) -> b[1] - a[1]); + int building = 0; + for (int i = 0; i < m; i++) { + if (i > 0 && boundaries[i - 1] == boundaries[i]) + continue; + while (building < n && buildings[building][0] <= boundaries[i]) + pq.offer(new int[] { buildings[building][1], buildings[building++][2] }); + while (!pq.isEmpty() && pq.peek()[0] <= boundaries[i]) + pq.poll(); + int height = (pq.isEmpty()) ? 0 : pq.peek()[1]; + if (ans.size() == 0 || height != ans.get(ans.size() - 1).get(1)) + ans.add(Arrays.asList(boundaries[i], height)); + } + + return ans; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/224.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/224.exercises/solution.md" index 9c322ea474f431a17a711eb6f66f4cfc454977c4..274520e1a88699374e736f986c42d1794ac4ffc8 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/224.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/224.exercises/solution.md" @@ -39,6 +39,44 @@ ## template ```java +class Solution { + public int calculate(String s) { + int res = 0, num = 0; + Stack sta = new Stack(); + char preSign = '+'; + int len = s.length(); + + for (int i = 0; i < len; i++) { + char ch = s.charAt(i); + + if (Character.isDigit(ch)) { + num = num * 10 + ch - '0'; + } + + if ((!Character.isDigit(ch) && ch != ' ') || i == len - 1) { + if (preSign == '+') { + sta.push(num); + } else if (preSign == '-') { + sta.push(-1 * num); + } else if (preSign == '*') { + sta.push(num * sta.pop()); + } else if (preSign == '/') { + sta.push(sta.pop() / num); + } + + num = 0; + preSign = ch; + } + } + + while (!sta.isEmpty()) { + res += sta.pop(); + } + + return res; + + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/233.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/233.exercises/solution.md" index bbb45de788d0fbf1b77be97afe8ca60cb85a8454..458f13d31a92cbaf33cad98113cec29af9fb0bce 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/233.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/233.exercises/solution.md" @@ -31,6 +31,29 @@ ```java +class Solution { + public int countDigitOne(int n) { + if (n < 1) + return 0; + int len = getLenOfNum(n); + if (len == 1) + return 1; + int tmp = (int) Math.pow(10, len - 1); + int first = n / tmp; + int firstOneNum = first == 1 ? n % tmp + 1 : tmp; + int otherOneNUm = first * (len - 1) * (tmp / 10); + return firstOneNum + otherOneNUm + countDigitOne(n % tmp); + } + + private int getLenOfNum(int n) { + int len = 0; + while (n != 0) { + len++; + n /= 10; + } + return len; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/239.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/239.exercises/solution.md" index 2cf5bfa8150da773ce92e8fba3697498664b5ba4..c6cfadda61b466ca55d957e748cf460509da0981 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/239.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/239.exercises/solution.md" @@ -64,7 +64,39 @@ ```java - +class Solution { + + public int[] maxSlidingWindow(int[] nums, int k) { + if (nums == null || nums.length < 0 || k <= 0 || k == 1) + return nums; + + queue = new PriorityQueue<>(k, new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o2 - o1; + } + }); + + int[] max = new int[nums.length - k + 1]; + for (int i = 0; i < nums.length; i++) { + + if (i < k - 1) { + queue.add(nums[i]); + } else if (i == k - 1) { + queue.add(nums[i]); + max[0] = queue.peek(); + } else { + + queue.remove(nums[i - k]); + queue.add(nums[i]); + max[i - k + 1] = queue.peek(); + } + } + + return max; + + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/config.json" deleted file mode 100644 index 6518d1a69de6635fce9026ed44bab12ed4019fbf..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-d1f2c2db52e74a919d090d16eb19b2d0", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.json" deleted file mode 100644 index ea60ae3364c59486410d18cbb812bc5b9d8694d5..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "ef60cf92510c465f8b7207236f99eeb6", - "author": "csdn.net", - "keywords": "递归,数组,字符串" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.md" deleted file mode 100644 index 71f8f33753c39c03e6927b5d72a58ac5b58c6a29..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/248.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 中心对称数 III - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/config.json" deleted file mode 100644 index 9f48928875daade4566d5aa1dcf8c323fd3347de..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-c08c4c3a8ccc49488dc218bbb734345e", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.json" deleted file mode 100644 index f0449c4611edd8f19de347016d0c214d93afbc78..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "9f8337e2f8294a569ad4325372e38a4f", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.md" deleted file mode 100644 index 730faec6f2ac752941bf9ace026fa40e5027dd27..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/262.exercises/solution.md" +++ /dev/null @@ -1,146 +0,0 @@ -# 行程和用户 - -表:Trips -
-
-
-+-------------+----------+
-| Column Name | Type     |
-+-------------+----------+
-| Id          | int      |
-| Client_Id   | int      |
-| Driver_Id   | int      |
-| City_Id     | int      |
-| Status      | enum     |
-| Request_at  | date     |     
-+-------------+----------+
-Id 是这张表的主键。
-这张表中存所有出租车的行程信息。每段行程有唯一 Id ,其中 Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外键。
-Status 是一个表示行程状态的枚举类型,枚举成员为(‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’) 。
-
- -

 

- -
-
-

表:Users

-
-
- -
-+-------------+----------+
-| Column Name | Type     |
-+-------------+----------+
-| Users_Id    | int      |
-| Banned      | enum     |
-| Role        | enum     |
-+-------------+----------+
-Users_Id 是这张表的主键。
-这张表中存所有用户,每个用户都有一个唯一的 Users_Id ,Role 是一个表示用户身份的枚举类型,枚举成员为 (‘client’, ‘driver’, ‘partner’) 。
-Banned 是一个表示用户是否被禁止的枚举类型,枚举成员为 (‘Yes’, ‘No’) 。
-
- -

 

- -

写一段 SQL 语句查出 "2013-10-01" 至 "2013-10-03" 期间非禁止用户(乘客和司机都必须未被禁止)的取消率。非禁止用户即 Banned 为 No 的用户,禁止用户即 Banned 为 Yes 的用户。

- -

取消率 的计算方式如下:(被司机或乘客取消的非禁止用户生成的订单数量) / (非禁止用户生成的订单总数)。

- -

返回结果表中的数据可以按任意顺序组织。其中取消率 Cancellation Rate 需要四舍五入保留 两位小数

- -

 

- -

查询结果格式如下例所示:

- -
-Trips 表:
-+----+-----------+-----------+---------+---------------------+------------+
-| Id | Client_Id | Driver_Id | City_Id | Status              | Request_at |
-+----+-----------+-----------+---------+---------------------+------------+
-| 1  | 1         | 10        | 1       | completed           | 2013-10-01 |
-| 2  | 2         | 11        | 1       | cancelled_by_driver | 2013-10-01 |
-| 3  | 3         | 12        | 6       | completed           | 2013-10-01 |
-| 4  | 4         | 13        | 6       | cancelled_by_client | 2013-10-01 |
-| 5  | 1         | 10        | 1       | completed           | 2013-10-02 |
-| 6  | 2         | 11        | 6       | completed           | 2013-10-02 |
-| 7  | 3         | 12        | 6       | completed           | 2013-10-02 |
-| 8  | 2         | 12        | 12      | completed           | 2013-10-03 |
-| 9  | 3         | 10        | 12      | completed           | 2013-10-03 |
-| 10 | 4         | 13        | 12      | cancelled_by_driver | 2013-10-03 |
-+----+-----------+-----------+---------+---------------------+------------+
-
-Users 表:
-+----------+--------+--------+
-| Users_Id | Banned | Role   |
-+----------+--------+--------+
-| 1        | No     | client |
-| 2        | Yes    | client |
-| 3        | No     | client |
-| 4        | No     | client |
-| 10       | No     | driver |
-| 11       | No     | driver |
-| 12       | No     | driver |
-| 13       | No     | driver |
-+----------+--------+--------+
-
-Result 表:
-+------------+-------------------+
-| Day        | Cancellation Rate |
-+------------+-------------------+
-| 2013-10-01 | 0.33              |
-| 2013-10-02 | 0.00              |
-| 2013-10-03 | 0.50              |
-+------------+-------------------+
-
-2013-10-01:
-  - 共有 4 条请求,其中 2 条取消。
-  - 然而,Id=2 的请求是由禁止用户(User_Id=2)发出的,所以计算时应当忽略它。
-  - 因此,总共有 3 条非禁止请求参与计算,其中 1 条取消。
-  - 取消率为 (1 / 3) = 0.33
-2013-10-02:
-  - 共有 3 条请求,其中 0 条取消。
-  - 然而,Id=6 的请求是由禁止用户发出的,所以计算时应当忽略它。
-  - 因此,总共有 2 条非禁止请求参与计算,其中 0 条取消。
-  - 取消率为 (0 / 2) = 0.00
-2013-10-03:
-  - 共有 3 条请求,其中 1 条取消。
-  - 然而,Id=8 的请求是由禁止用户发出的,所以计算时应当忽略它。
-  - 因此,总共有 2 条非禁止请求参与计算,其中 1 条取消。
-  - 取消率为 (1 / 2) = 0.50
-
-
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/config.json" deleted file mode 100644 index aa3bb45c0a759e274fa88181ace15c2a86d2cba6..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-ccf342bf60c648cda2b0cf4c3021c4c1", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.md" deleted file mode 100644 index 6507397c83fc3b23cb0015fc1c352e2055dea694..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/265.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 粉刷房子 II - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/config.json" deleted file mode 100644 index fdc01d589e6b826316eb2f72aad7a864f2f8bf7f..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-7501906f06d2434f8aefa491cc761c49", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.md" deleted file mode 100644 index 761d75bcfc10c4a08a0979cf69fd3b5c868af44c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/269.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 火星词典 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/config.json" deleted file mode 100644 index 42eea7b4cf2f262e5fd087705963309650d5e892..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-cffda9526d064f7fa57a0858936f8d26", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.json" deleted file mode 100644 index 81a65243bcdaeba5bccd2f91cfe277ba2c99f39e..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "d114c28fb45e4d2896876a26b18d731c", - "author": "csdn.net", - "keywords": "栈,树,深度优先搜索,二叉搜索树,双指针,二叉树,堆(优先队列)" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.md" deleted file mode 100644 index decf90a65f9a0307b0ea03359c30fe45ebd58b11..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/272.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 最接近的二叉搜索树值 II - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/273.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/273.exercises/solution.md" index b5a8db0c0e8758df8bd9fac6bf1424a82ad198af..8b8fd20e19f03e19f9f669ae86f35cf3b5e34b60 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/273.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/273.exercises/solution.md" @@ -44,6 +44,68 @@ ## template ```java +class Solution { + + String[] withinTwentyNum = new String[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", + "Nine", "Ten", + "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; + + String[] theWholeTen = new String[] { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", + "Ninety" }; + + String[] unit = new String[] { "", "Thousand", "Million", "Billion" }; + + public String numberToWords(int num) { + if (num == 0) { + return withinTwentyNum[num]; + } + StringBuilder sb = new StringBuilder(); + int unitCount = 0; + int temp; + + String[] words = new String[32]; + int wordIndex = 0; + + for (int n = num; num > 0; unitCount++) { + + n = num % 1000; + num /= 1000; + if (n == 0) { + continue; + } + + words[wordIndex++] = unit[unitCount]; + temp = n % 100; + if (n % 100 > 0) { + + if (temp >= 20) { + if (temp % 10 > 0) { + + words[wordIndex++] = withinTwentyNum[temp % 10]; + } + + words[wordIndex++] = theWholeTen[temp / 10]; + + } else { + + words[wordIndex++] = withinTwentyNum[temp]; + } + } + temp = n / 100; + if (temp > 0) { + + words[wordIndex++] = "Hundred"; + words[wordIndex++] = withinTwentyNum[temp]; + } + } + + for (int index = wordIndex - 1; index >= 0; index--) { + sb.append(words[index]).append(" "); + } + + return sb.toString().trim(); + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/282.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/282.exercises/solution.md" index 61b575696f09851d52be2dcae345f41eacf0adb2..284c2b976cf1f97070dbd38462d0f94fb5558e83 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/282.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/282.exercises/solution.md" @@ -50,6 +50,51 @@ ## template ```java +class Solution { + int n; + String num; + List ans; + int target; + + public List addOperators(String num, int target) { + this.n = num.length(); + this.num = num; + this.target = target; + this.ans = new ArrayList(); + StringBuffer expr = new StringBuffer(); + dfs(expr, 0, 0, 0); + return ans; + } + + public void dfs(StringBuffer sba, long sum, long prepareMultiply, int index) { + StringBuffer sb = new StringBuffer(sba); + if (index == n) { + if (sum == target) { + ans.add(sb.toString()); + } + return; + } + int sign = sb.length(); + if (index > 0) { + sb.append("0"); + } + long val = 0; + for (int i = index; i < n && (i == index || num.charAt(index) != '0'); i++) { + val = val * 10 + (num.charAt(i) - '0'); + sb.append(num.charAt(i)); + if (index == 0) { + dfs(sb, val, val, i + 1); + continue; + } + sb.setCharAt(sign, '+'); + dfs(sb, sum + val, val, i + 1); + sb.setCharAt(sign, '-'); + dfs(sb, sum - val, -val, i + 1); + sb.setCharAt(sign, '*'); + dfs(sb, sum - prepareMultiply + prepareMultiply * val, prepareMultiply * val, i + 1); + } + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/295.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/295.exercises/solution.md" index 2c17ccc3f1981fa407130f541c2cbaa2e32e69d5..ccc9f1ba8378a7ec4051ccf38df14a0a894a12c4 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/295.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/295.exercises/solution.md" @@ -34,6 +34,40 @@ findMedian() -> 2 ## template ```java +class MedianFinder { + + PriorityQueue min; + PriorityQueue max; + + /** initialize your data structure here. */ + public MedianFinder() { + min = new PriorityQueue<>(); + max = new PriorityQueue<>((a, b) -> { + return b - a; + }); + } + + public void addNum(int num) { + max.add(num); + min.add(max.remove()); + if (min.size() > max.size()) + max.add(min.remove()); + } + + public double findMedian() { + if (max.size() == min.size()) + return (max.peek() + min.peek()) / 2.0; + else + return max.peek(); + } +} + +/** + * Your MedianFinder object will be instantiated and called as such: + * MedianFinder obj = new MedianFinder(); + * obj.addNum(num); + * double param_2 = obj.findMedian(); + */ ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/config.json" deleted file mode 100644 index ccd4b72edd1205b6c35f8f5a56371923e3ba62f3..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-36c7f67b7f9c4e9ba907a4e8be2a13de", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.json" deleted file mode 100644 index e75b58983d9c9a47f46cf799fa3d3d4b6edc94b7..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "404accf9a4bb4da3a7a3d98995991644", - "author": "csdn.net", - "keywords": "数组,数学,矩阵,排序" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.md" deleted file mode 100644 index ecc24cdfb6a746ff016ddfc5becd549ef06fb1fa..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/296.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 最佳的碰头地点 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/297.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/297.exercises/solution.md" index 02e0b1b030f4b9c89ab8023a77aef338f4bdbab2..5788761a0d838a8ab0f7e2c32b5b906f3f9d13a4 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/297.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/297.exercises/solution.md" @@ -49,6 +49,65 @@ ## template ```java +class Solution { + public String serialize(TreeNode root) { + if (root == null) + return "[]"; + ArrayList queue = new ArrayList<>(); + queue.add(root); + for (int i = 0; i < queue.size(); i++) { + TreeNode node = queue.get(i); + if (node == null) + continue; + if (node.left != null) + queue.add(node.left); + if (node.right != null) + queue.add(node.right); + } + while (queue.get(queue.size() - 1) == null) { + queue.remove(queue.size() - 1); + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(queue.get(0)); + for (int i = 1; i < queue.size(); i++) { + if (queue.get(i) == null) { + sb.append(","); + sb.append("null"); + } else { + sb.append(","); + sb.append(queue.get(i).val); + } + } + sb.append("]"); + return sb.toString(); + } + + public TreeNode deserialize(String s) { + if (data.equals("[]")) + return null; + String[] vals = data.substring(1, data.length() - 1).split(","); + ArrayList queue = new ArrayList<>(); + TreeNode root = new TreeNode(Integer.parseInt(vals[0])); + queue.add(root); + int index = 0; + boolean left = true; + for (int i = 1; i < vals.length; i++) { + if (!vals[i].equals("null")) { + TreeNode node = new TreeNode(Integer.parseInt(vals[i])); + if (left) { + queue.get(index).left = node; + } else { + queue.get(index).right = node; + } + } + if (!left) + index++; + left = !left; + } + return root; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/301.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/301.exercises/solution.md" index 46bdd9c20b15e1794ff6eccb266a7073bf4ef48c..91cb72a92366b01915bd8b221ad072981b8c6f61 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/301.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/301.exercises/solution.md" @@ -41,7 +41,50 @@ ## template ```java - +public class Solution { + + private Set set; + + private String input; + + private int maxLen = 0; + + public List removeInvalidParentheses(String s) { + set = new HashSet<>(); + input = s; + removeInvalidParentheses(0, "", 0, 0); + return new ArrayList<>(set); + } + + private void removeInvalidParentheses(int index, String valid, int leftCount, int rightCount) { + if (leftCount < rightCount) { + return; + } + if (index == input.length()) { + if (leftCount == rightCount) { + if (maxLen < valid.length()) { + maxLen = valid.length(); + set.clear(); + set.add(valid); + } else if (maxLen == valid.length()) { + set.add(valid); + } + } + return; + } + char c = input.charAt(index); + if (c == '(') { + removeInvalidParentheses(index + 1, valid, leftCount, rightCount); + removeInvalidParentheses(index + 1, valid + c, leftCount + 1, rightCount); + } else if (c == ')') { + removeInvalidParentheses(index + 1, valid, leftCount, rightCount); + removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount + 1); + } else { + removeInvalidParentheses(index + 1, valid + c, leftCount, rightCount); + } + } + +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/config.json" deleted file mode 100644 index 4671476d8b01c8293e1baa645604a1921c763d17..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-d03cac76638d45c880761651ad4278e5", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.json" deleted file mode 100644 index f94aa31fe9294984f8f97886d80d8a4b77ecb80a..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "bff1f6a264234f8aa25f5e7f17858eca", - "author": "csdn.net", - "keywords": "深度优先搜索,广度优先搜索,数组,二分查找,矩阵" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.md" deleted file mode 100644 index 6851d2412a52d3667722175c5972b74d9a719e1c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/302.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 包含全部黑色像素的最小矩形 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/config.json" deleted file mode 100644 index b125ffb8f63f55f520c92f0bd9a967022fd5e25a..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-c0f910927c42491fb720452e44b6bf2e", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.json" deleted file mode 100644 index fcab07ddde93b4a276d2f4b4a628506ff8ac7687..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "41459ff4146a45d0a8ad931f4b54aefc", - "author": "csdn.net", - "keywords": "并查集,数组" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.md" deleted file mode 100644 index 003a0236fdcb914b326625828c2d6b5f5bfdce65..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/305.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 岛屿数量 II - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/config.json" deleted file mode 100644 index f1dec8454b1e2cfc7bd015ef946420eb097707cd..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-e631586abfb749ed954690142eef2eee", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.json" deleted file mode 100644 index 12fa2c1bbe116610f7f59f3b2eadcd6faf57e7d9..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "d8d2b2d2c8c140298f5ea75a80bd26fa", - "author": "csdn.net", - "keywords": "设计,树状数组,线段树,数组,矩阵" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.md" deleted file mode 100644 index 511a3d7329d28382cc7231ef5bd4bda1a20c4dd2..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/308.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 二维区域和检索 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/312.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/312.exercises/solution.md" index 3d91a28dd06d23977b47a6eba0b390fb7862de0d..43e8f698b8d920df07322ce76034786cde4562fd 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/312.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/312.exercises/solution.md" @@ -38,7 +38,25 @@ coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167 ```java - +public class Solution { + public int maxCoins(int[] iNums) { + int n = iNums.length; + int[] nums = new int[n + 2]; + for (int i = 0; i < n; i++) + nums[i + 1] = iNums[i]; + nums[0] = nums[n + 1] = 1; + int[][] dp = new int[n + 2][n + 2]; + for (int k = 1; k <= n; k++) { + for (int i = 1; i <= n - k + 1; i++) { + int j = i + k - 1; + for (int x = i; x <= j; x++) { + dp[i][j] = Math.max(dp[i][j], dp[i][x - 1] + nums[i - 1] * nums[x] * nums[j + 1] + dp[x + 1][j]); + } + } + } + return dp[1][n]; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/315.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/315.exercises/solution.md" index f5a17d287ebcf6bdacdb0e3de2492c9cd7f02036..f3f6ad66c1252084fbece78eb2fae9da16e18b74 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/315.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/315.exercises/solution.md" @@ -43,6 +43,60 @@ ## template ```java +class Solution { + public static List countSmaller(int[] nums) { + if (nums.length == 0) { + return new ArrayList<>(); + } + int min = Integer.MAX_VALUE; + for (int value : nums) { + if (value < min) { + min = value; + } + } + for (int i = 0; i < nums.length; i++) { + nums[i] = nums[i] - min + 1; + } + + int max = Integer.MIN_VALUE; + for (int value : nums) { + if (value > max) { + max = value; + } + } + + int[] BITree = new int[max + 1]; + BITree[0] = 0; + int[] countArr = new int[nums.length]; + for (int i = nums.length - 1; i >= 0; i--) { + int count = getSum(nums[i] - 1, BITree); + countArr[i] = count; + update(nums[i], BITree); + } + List result = new ArrayList<>(); + for (int value : countArr) { + result.add(value); + } + return result; + } + + public static int getSum(int value, int[] BITree) { + int sum = 0; + while (value > 0) { + sum += BITree[value]; + value -= (value & -value); + } + return sum; + } + + public static void update(int value, int[] BITree) { + while (value <= BITree.length - 1) { + BITree[value] += 1; + value += (value & -value); + } + } + +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/config.json" deleted file mode 100644 index b4d98de8f78b9b26ddd65eccab13ce0317b79873..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-3be8e3fbcacb44639959949276bca02e", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.json" deleted file mode 100644 index 41b1dbf667718df5a1149db03ed01332f26a74d5..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "5946135e1c624e9ca632a86451f1afa4", - "author": "csdn.net", - "keywords": "广度优先搜索,数组,矩阵" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.md" deleted file mode 100644 index b996199a430f7c9b58395f4332475d0c1680adeb..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/317.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 离建筑物最近的距离 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/321.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/321.exercises/solution.md" index 22dbea86f77a70d97ef8a9dc25bd1c862021170d..ccbe8536993c1c87dad4c137153c2a53839cce66 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/321.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/321.exercises/solution.md" @@ -38,7 +38,52 @@ k = 3 ```java - +class Solution { + + private int[] maxInNums(int[] nums, int k) { + int[] max = new int[k]; + int len = nums.length; + + for (int i = 0, j = 0; i < len; ++i) { + + while (j > 0 && k - j < len - i && max[j - 1] < nums[i]) + --j; + if (j < k) + max[j++] = nums[i]; + } + return max; + } + + private boolean greater(int[] nums1Max, int i, int[] nums2Max, int j) { + int lenNums1Max = nums1Max.length; + int lenNums2Max = nums2Max.length; + + while (i < lenNums1Max && j < lenNums2Max && nums1Max[i] == nums2Max[j]) { + ++i; + ++j; + } + + return j == lenNums2Max || (i < lenNums1Max && nums1Max[i] > nums2Max[j]); + } + + private int[] merge(int[] nums1Max, int[] nums2Max) { + int lenCurRes = nums1Max.length + nums2Max.length; + int[] curRes = new int[lenCurRes]; + for (int i = 0, j = 0, m = 0; m < lenCurRes; ++m) { + curRes[m] = greater(nums1Max, i, nums2Max, j) ? nums1Max[i++] : nums2Max[j++]; + } + return curRes; + } + + public int[] maxNumber(int[] nums1, int[] nums2, int k) { + int[] res = null; + for (int i = Math.max(k - nums2.length, 0); i <= Math.min(nums1.length, k); ++i) { + int[] merge = merge(maxInNums(nums1, i), maxInNums(nums2, k - i)); + res = (res == null || greater(merge, 0, res, 0)) ? merge : res; + } + return res; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/327.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/327.exercises/solution.md" index 7a555da9cd210f5f9c1d0f2bbe5780d436096302..989b99bcbeaf30ea9500ddf4da49f6b122b9cb3f 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/327.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/327.exercises/solution.md" @@ -35,7 +35,57 @@ ## template ```java - +class Solution { + public int countRangeSum(int[] nums, long lower, long upper) { + long sums[] = new long[nums.length]; + for (int i = 0; i < nums.length; i++) { + sums[i] = ((i - 1 >= 0) ? sums[i - 1] : 0) + nums[i]; + } + + int result = divideAndConquer(sums, 0, sums.length - 1, upper, lower); + + return result; + } + + private int divideAndConquer(long sums[], int start, int end, long upper, long lower) { + if (start > end) + return 0; + if (start == end) + return (sums[start] <= upper && sums[start] >= lower) ? 1 : 0; + int mid = (start + end) / 2; + int counts = 0; + counts += divideAndConquer(sums, start, mid, upper, lower); + counts += divideAndConquer(sums, mid + 1, end, upper, lower); + + int ls = start, le = mid; + while (le >= start && sums[mid + 1] - sums[le] <= upper) + le--; + for (int r = mid + 1; r <= end; r++) { + while (ls <= mid && sums[r] - sums[ls] >= lower) + ls++; + while (le + 1 <= mid && sums[r] - sums[le + 1] > upper) + le++; + if (ls - le - 1 < 0) + continue; + counts += (ls - le - 1); + } + ls = start; + int i = 0, r = mid + 1; + long merged[] = new long[end - start + 1]; + while (ls <= mid || r <= end) { + if (ls > mid || (r <= end && sums[r] < sums[ls])) { + merged[i++] = sums[r++]; + } else { + merged[i++] = sums[ls++]; + } + } + for (i = 0; i < merged.length; i++) { + sums[start + i] = merged[i]; + } + + return counts; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/329.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/329.exercises/solution.md" index 7d7c08baee91421fa15fc8a559e25cfb3551121e..86da387ede42f453237772f5457ffa304b11f539 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/329.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/329.exercises/solution.md" @@ -45,6 +45,40 @@ ```java +class Solution { + public int longestIncreasingPath(int[][] matrix) { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { + return 0; + } + int max = 0; + int row = matrix.length; + int col = matrix[0].length; + int[][] dp = new int[row][col]; + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + max = Math.max(max, loop(matrix, Integer.MIN_VALUE, dp, i, j)); + } + } + return max; + } + + private int loop(int[][] mat, int pre, int[][] dp, int i, int j) { + if (i < 0 || j < 0 || i >= mat.length || j >= mat[0].length || mat[i][j] <= pre) { + return 0; + } + if (dp[i][j] != 0) { + return dp[i][j]; + } + int max = 0; + max = Math.max(max, loop(mat, mat[i][j], dp, i - 1, j)); + max = Math.max(max, loop(mat, mat[i][j], dp, i + 1, j)); + max = Math.max(max, loop(mat, mat[i][j], dp, i, j - 1)); + max = Math.max(max, loop(mat, mat[i][j], dp, i, j + 1)); + dp[i][j] = max + 1; + return dp[i][j]; + } +} + ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/330.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/330.exercises/solution.md" index 9b1610c31fdac57b5ddee14ae37110391c05da9f..eabe5f0adb2e7a693812644b35e2dfd63a0c8994 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/330.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/330.exercises/solution.md" @@ -29,7 +29,21 @@ ## template ```java - +class Solution { + public int minPatches(int[] nums, int n) { + int patches = 0, i = 0; + long miss = 1; + while (miss <= n) { + if (i < nums.length && nums[i] <= miss) + miss += nums[i++]; + else { + miss += miss; + patches++; + } + } + return patches; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/335.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/335.exercises/solution.md" index 7c14b7f9e91fe9f2a69eed25215ff234fb2a6ac7..af2ed686fc17587e13488a2f208ecefb51a4d15a 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/335.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/335.exercises/solution.md" @@ -41,7 +41,27 @@ ## template ```java - +class Solution { + public boolean isSelfCrossing(int[] x) { + if (x.length < 4) + return false; + int a = 0, b = 0, c = 0; + int d = x[0], e = x[1], f = x[2]; + for (int i = 3; i < x.length; i++) { + a = b; + b = c; + c = d; + d = e; + e = f; + f = x[i]; + if (e < c - a && f >= d) + return true; + if (c - a <= e && e <= c && f >= (d - b < 0 ? d : d - b)) + return true; + } + return false; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/336.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/336.exercises/solution.md" index 8716f54d280a6a3efae8c4cdbbecd9cc49bba254..7f49a0c7cafb19d9753844c7377d5acd0d8087ad 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/336.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/336.exercises/solution.md" @@ -39,6 +39,85 @@ ## template ```java +class Solution { + private static List> ans = new ArrayList<>(); + + public List> palindromePairs(String[] words) { + if (words == null || words.length == 0) { + return null; + } + + ans = new ArrayList<>(); + TrieNode root = new TrieNode(); + + for (int i = 0; i < words.length; i++) { + addWord(root, words[i], i); + } + + for (int i = 0; i < words.length; i++) { + find(root, words[i], i); + } + return ans; + } + + private static class TrieNode { + + int index; + TrieNode[] next; + List palindIndex; + + public TrieNode() { + index = -1; + next = new TrieNode[26]; + palindIndex = new ArrayList<>(); + } + } + + private static void addWord(TrieNode root, String word, int index) { + for (int i = word.length() - 1; i >= 0; i--) { + int ch = word.charAt(i) - 'a'; + if (root.next[ch] == null) { + root.next[ch] = new TrieNode(); + } + + if (isPalindrome(word, 0, i)) { + root.palindIndex.add(index); + } + root = root.next[ch]; + } + root.index = index; + root.palindIndex.add(index); + } + + private static void find(TrieNode root, String word, int index) { + for (int i = 0; i < word.length(); i++) { + + if (root.index != -1 && root.index != index && isPalindrome(word, i, word.length() - 1)) { + ans.add(Arrays.asList(index, root.index)); + } + + if (root.next[word.charAt(i) - 'a'] == null) { + return; + } + root = root.next[word.charAt(i) - 'a']; + } + + for (int i : root.palindIndex) { + if (i != index) { + ans.add(Arrays.asList(index, i)); + } + } + } + + private static boolean isPalindrome(String string, int l, int r) { + while (l < r) { + if (string.charAt(l++) != string.charAt(r--)) { + return false; + } + } + return true; + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/352.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/352.exercises/solution.md" index e871d7a4dae0abfbb8c3bd0a218838f539b4b6f2..9cb91f9e60302de0bb6350928055894a4775a858 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/352.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/352.exercises/solution.md" @@ -56,7 +56,38 @@ summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]] ## template ```java - +class SummaryRanges { + private final TreeSet tree = new TreeSet<>(); + + public void addNum(int val) { + tree.add(val); + } + + public int[][] getIntervals() { + ArrayList result = new ArrayList<>(1 << 2); + Iterator iterator = tree.iterator(); + int[] array = new int[tree.size()]; + int i = 0; + while (iterator.hasNext()) + array[i++] = iterator.next(); + int length = array.length; + if (length == 0) + return result.toArray(new int[0][]); + int start = array[0]; + + for (i = 0; i < length; ++i) { + if (i + 1 < length && array[i + 1] - array[i] != 1) { + result.add(new int[] { start, array[i] }); + start = array[i + 1]; + } else if (i + 1 == length) { + result.add(new int[] { start, array[i] }); + } + } + + return result.toArray(new int[result.size()][]); + } + +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/354.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/354.exercises/solution.md" index baf3c80dd4c5480030c880050d849ad88472b541..d7488f57c4b411dd309c64da97e7bdaad0ec3ce3 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/354.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/354.exercises/solution.md" @@ -38,6 +38,40 @@ ```java +class Solution { + public int maxEnvelopes(int[][] envelopes) { + int n = envelopes.length; + if (n == 0) + return 0; + Arrays.sort(envelopes, new Comparator() { + public int compare(int[] a, int[] b) { + if (a[0] != b[0]) + return a[0] - b[0]; + return b[1] - a[1]; + } + }); + + List lastHeight = new ArrayList<>(); + lastHeight.add(envelopes[0][1]); + for (int i = 1; i < n; i++) { + int h = envelopes[i][1]; + if (h > lastHeight.get(lastHeight.size() - 1)) + lastHeight.add(h); + else { + int l = 0, r = lastHeight.size() - 1; + while (r > l) { + int m = (l + r) >> 1; + if (lastHeight.get(m) < h) + l = m + 1; + else + r = m; + } + lastHeight.set(l, h); + } + } + return lastHeight.size(); + } +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/config.json" "b/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/config.json" deleted file mode 100644 index 06a7e16152aa41dd9e656ac9b80f7bc7d297e2f0..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-e7b31efc2d444c2fbb1c22f08bac3647", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.json" "b/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.json" deleted file mode 100644 index 552ae8432baf2df1b9f8a118d68c4d2c51f6f3fe..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "2f4d31ae0ddb4330bd370c68dfe19b02", - "author": "csdn.net", - "keywords": "贪心,哈希表,字符串,计数,排序,堆(优先队列)" -} \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.md" deleted file mode 100644 index fab1a2661242d9647ce175738f458858edc38932..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\345\233\260\351\232\276/java/358.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# K 距离间隔重排字符串 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/363.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/363.exercises/solution.md" index 2fd9957cfc1bf5453599adc7b204c058c9ba497e..6a32a61d47d97203c7fa2b09abf25239911632e6 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/363.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/363.exercises/solution.md" @@ -41,6 +41,50 @@ ## template ```java +class Solution { + public int maxSumSubmatrix(int[][] matrix, int k) { + + int row = matrix.length; + if (row <= 0) + return 0; + int line = matrix[0].length; + int max = Integer.MIN_VALUE; + + for (int left = 0; left < line; left++) { + + int[] rowSum = new int[row]; + + for (int right = left; right < line; right++) { + + for (int i = 0; i < row; i++) { + rowSum[i] += matrix[i][right]; + } + + max = Math.max(max, dpmax(rowSum, k)); + + } + + } + + return max; + + } + + private int dpmax(int[] arr, int k) { + int max = Integer.MIN_VALUE; + int n = arr.length; + for (int top = 0; top < n; top++) { + int sum = 0; + for (int bottom = top; bottom < n; bottom++) { + sum += arr[bottom]; + if (sum > max && sum <= k) + max = sum; + } + } + return max; + } + +} ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/381.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/381.exercises/solution.md" index 2d6380639e3b3b6d3099d8fcb5b8f538c4f8c704..09c4b8b80ee3c06ac655046fe7230e86a8ddd8eb 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/381.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/381.exercises/solution.md" @@ -38,6 +38,79 @@ collection.getRandom(); ## template ```java +class RandomizedCollection { + + private Map> map; + + private List list; + + private Random random; + + private int size = 0; + + public RandomizedCollection() { + map = new HashMap<>(); + list = new ArrayList<>(); + random = new Random(); + } + + public boolean insert(int val) { + if (map.containsKey(val)) { + Set indexes = map.get(val); + list.add(size, val); + indexes.add(size); + size++; + return false; + } else { + Set indexes = new HashSet<>(); + map.put(val, indexes); + list.add(size, val); + indexes.add(size); + size++; + return true; + } + } + + public boolean remove(int val) { + if (!map.containsKey(val)) { + return false; + } + Set indexes = map.get(val); + if (list.get(size - 1) == val) { + indexes.remove(size - 1); + size--; + } else { + + Iterator it = indexes.iterator(); + int index = it.next(); + it.remove(); + + int last = list.get(size - 1); + list.set(index, last); + Set set = map.get(last); + + set.remove(size - 1); + set.add(index); + size--; + } + if (indexes.size() == 0) { + map.remove(val); + } + return true; + } + + public int getRandom() { + return list.get(random.nextInt(size)); + } +} + +/** + * Your RandomizedCollection object will be instantiated and called as such: + * RandomizedCollection obj = new RandomizedCollection(); + * boolean param_1 = obj.insert(val); + * boolean param_2 = obj.remove(val); + * int param_3 = obj.getRandom(); + */ ``` diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/391.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/391.exercises/solution.md" index 9a0877d5389b1791e99f975902bf09ac7197201b..99505a71d7dc0f308370d91e11e5ba87e9c0295b 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/391.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/391.exercises/solution.md" @@ -72,7 +72,57 @@ ```java - +class Solution { + + public boolean isRectangleCover(int[][] rectangles) { + int left = Integer.MAX_VALUE; + int right = Integer.MIN_VALUE; + int top = Integer.MIN_VALUE; + int bottom = Integer.MAX_VALUE; + int n = rectangles.length; + + Set set = new HashSet<>(); + int sumArea = 0; + + for (int i = 0; i < n; i++) { + + left = Math.min(left, rectangles[i][0]); + bottom = Math.min(bottom, rectangles[i][1]); + right = Math.max(right, rectangles[i][2]); + top = Math.max(top, rectangles[i][3]); + + sumArea += (rectangles[i][3] - rectangles[i][1]) * (rectangles[i][2] - rectangles[i][0]); + + String lt = rectangles[i][0] + " " + rectangles[i][3]; + String lb = rectangles[i][0] + " " + rectangles[i][1]; + String rt = rectangles[i][2] + " " + rectangles[i][3]; + String rb = rectangles[i][2] + " " + rectangles[i][1]; + + if (!set.contains(lt)) + set.add(lt); + else + set.remove(lt); + if (!set.contains(lb)) + set.add(lb); + else + set.remove(lb); + if (!set.contains(rt)) + set.add(rt); + else + set.remove(rt); + if (!set.contains(rb)) + set.add(rb); + else + set.remove(rb); + } + + if (set.size() == 4 && set.contains(left + " " + top) && set.contains(left + " " + bottom) + && set.contains(right + " " + bottom) && set.contains(right + " " + top)) { + return sumArea == (right - left) * (top - bottom); + } + return false; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\345\233\260\351\232\276/java/403.exercises/solution.md" "b/data_source/exercises/\345\233\260\351\232\276/java/403.exercises/solution.md" index 5314f8751ca7db8df231a8a0353a21c71c9ff464..8d1899c673de7106f1c4bb088ca06326295ac4f1 100644 --- "a/data_source/exercises/\345\233\260\351\232\276/java/403.exercises/solution.md" +++ "b/data_source/exercises/\345\233\260\351\232\276/java/403.exercises/solution.md" @@ -38,6 +38,24 @@ ## template ```java +class Solution { + + public boolean canCross(int[] stones) { + for (int i = 1; i < stones.length; i++) + if (stones[i] > stones[i - 1] + i) + return false; + return dfs(stones, 0, 1); + } + + public boolean dfs(int[] stones, int index, int v) { + if (index < 0 || v < 1) + return false; + return index == stones.length - 1 + || (index > 0 && dfs(stones, Arrays.binarySearch(stones, stones[index] + v + 1), v + 1)) || + dfs(stones, Arrays.binarySearch(stones, stones[index] + v), v) || + dfs(stones, Arrays.binarySearch(stones, stones[index] + v - 1), v - 1); + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/104.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/104.exercises/solution.md" index 74261e6c2a9955bb7d8e2db79e6bdc1318233961..1d5766383fdda74d979389ba8a69b332ef2a7a20 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/104.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/104.exercises/solution.md" @@ -22,6 +22,42 @@ ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public int maxDepth(TreeNode root) { + + if (root == null) { + return 0; + } + + int deep = 1; + if (root.left == null && root.right == null) { + return 1; + } + + int leftDeep = 0; + if (root.left != null) { + + leftDeep = 1 + maxDepth(root.left); + } + + int rightDeep = 0; + if (root.right != null) { + rightDeep = 1 + maxDepth(root.right); + } + + return deep + leftDeep > rightDeep ? leftDeep : rightDeep; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/108.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/108.exercises/solution.md" index c95b2dbcfcfcfdb1dce47bd5906dc563b8dd0900..9656e855510746a2e2ef7a20c1ba61d15bcc2421 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/108.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/108.exercises/solution.md" @@ -37,7 +37,40 @@ ## template ```java - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + return traversal(nums, 0, nums.length - 1); + } + + public TreeNode traversal(int[] nums, int left, int right) { + if (left > right) + return null; + int mid = left + ((right - left) / 2); + TreeNode node = new TreeNode(nums[mid]); + node.left = traversal(nums, left, mid - 1); + node.right = traversal(nums, mid + 1, right); + return node; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/110.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/110.exercises/solution.md" index 22d377532dd9b4cfb5b60ca7a848139edba9034a..ceca68b567883670d4a0d6a3cc241be802d5b90b 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/110.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/110.exercises/solution.md" @@ -44,7 +44,34 @@ ## template ```java - +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +public class Solution { + public boolean isBalanced(TreeNode root) { + + if (root == null) { + return true; + } + + return (Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1) && isBalanced(root.left) + && isBalanced(root.right); + } + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/111.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/111.exercises/solution.md" index c9b3fca6696895257e92196608f6a01a4a237928..168561774d495e6881b45c89312825e911565987 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/111.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/111.exercises/solution.md" @@ -35,7 +35,41 @@ ## template ```java - +class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public int minDepth(TreeNode root) { + + if (root == null) { + return 0; + } + + if (root.left == null && root.right == null) { + return 1; + } + + int min_depth = Integer.MAX_VALUE; + if (root.left != null) { + int countLeft = 1; + countLeft += minDepth(root.left); + min_depth = Math.min(countLeft, min_depth); + } + if (root.right != null) { + int countRight = 1; + countRight += minDepth(root.right); + min_depth = Math.min(min_depth, countRight); + } + return min_depth; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/112.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/112.exercises/solution.md" index 5d3d76b496a22538c621ed966534328c81df14cd..91a7a5c082db6f446fd095b43bd17d22a24304f6 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/112.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/112.exercises/solution.md" @@ -42,7 +42,54 @@ ```java - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } +} + +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + if (root == null) + return false; + return traversal(root, targetSum - root.val); + } + + public boolean traversal(TreeNode root, int count) { + if (root.left == null && root.right == null && count == 0) { + return true; + } + if (root.left == null && root.right == null) { + return false; + } + if (root.left != null) { + count -= root.left.val; + if (traversal(root.left, count)) + return true; + count += root.left.val; + } + if (root.right != null) { + count -= root.right.val; + if (traversal(root.right, count)) + return true; + count += root.right.val; + } + return false; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/118.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/118.exercises/solution.md" index 1e5bfa7021d0aa23d5aafa2d0aeb4d506f933671..c032f3a5cec8c022f6c5f2c5ff5faca3ad64af55 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/118.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/118.exercises/solution.md" @@ -34,8 +34,30 @@ ## template ```java - - +class Solution { + public List> generate(int numRows) { + List> res = new ArrayList>(); + if (numRows == 0) { + return res; + } + for (int i = 0; i < numRows; ++i) { + List temp = new ArrayList(); + for (int j = 0; j <= i; ++j) { + temp.add(numberget(i, j)); + } + res.add(temp); + } + return res; + } + + public int numberget(int i, int j) { + if (j == 0 || i == j) { + return 1; + } else { + return (numberget(i - 1, j - 1) + numberget(i - 1, j)); + } + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/119.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/119.exercises/solution.md" index bb472f8ce55ae8ff9365cc23dbd59012c56f915b..f7c1b9096c18f4e6cd88eae908e79707402e0b86 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/119.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/119.exercises/solution.md" @@ -47,8 +47,17 @@ ## template ```java - - +class Solution { + public List getRow(int rowIndex) { + List row = new ArrayList(); + for (int i = 0; i < rowIndex + 1; i++) { + row.add(0, 1); + for (int j = 1; j < row.size() - 1; j++) + row.set(j, row.get(j) + row.get(j + 1)); + } + return row; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/121.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/121.exercises/solution.md" index 1d31acbe65ad10a2edf2f05c59e1a87bd83571e9..4b3bd22cd60b8258526d9750bf381a07505812fa 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/121.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/121.exercises/solution.md" @@ -38,8 +38,17 @@ ## template ```java - - +class Solution { + public int maxProfit(int[] prices) { + int temp = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + temp = temp + prices[i + 1] - prices[i]; + } + } + return temp; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/122.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/122.exercises/solution.md" index 0d4a4d3a6526557e6c7ce82efc3c6b0062523a1e..1b1a9e8e5bed9b79cfd0037b0cb1bb11b103c2fe 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/122.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/122.exercises/solution.md" @@ -46,8 +46,17 @@ ## template ```java - - +class Solution { + public int maxProfit(int[] prices) { + int profit = 0; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i] < prices[i + 1]) { + profit += prices[i + 1] - prices[i]; + } + } + return profit; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/125.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/125.exercises/solution.md" index 39c101e4fd2c3b6cb51892c31eaf4fb7a8745283..a8f09e4933f435600755e62f009d050d97f3f5f0 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/125.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/125.exercises/solution.md" @@ -35,8 +35,29 @@ ## template ```java - - +class Solution { + public boolean isPalindrome(String s) { + StringBuffer str = new StringBuffer(); + int len = s.length(); + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + if (Character.isLetterOrDigit(c)) { + str.append(Character.toLowerCase(c)); + } + } + int left = 0; + int right = str.length() - 1; + while (left < right) { + if (str.charAt(left) != str.charAt(right)) { + return false; + } + left++; + right--; + } + return true; + + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/136.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/136.exercises/solution.md" index 918500b1135d7462965995db8d5086d6e30bd12c..a95e7d213e853ae00d0126f3d749fe63255358cd 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/136.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/136.exercises/solution.md" @@ -21,7 +21,15 @@ ## template ```java - +class Solution { + public int singleNumber(int[] nums) { + int res = 0; + for (int num : nums) { + res ^= num; + } + return res; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/141.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/141.exercises/solution.md" index ab9e5da845e00c5f145b847f79aede898b27721c..04feeba296420193fb2bf2e47cae370bff9f3386 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/141.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/141.exercises/solution.md" @@ -56,7 +56,31 @@ ```java - +class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } +} + +public class Solution { + public boolean hasCycle(ListNode head) { + if (head == null || head.next == null) + return false; + ListNode fast = head.next; + ListNode slow = head; + while (fast != slow) { + if (fast.next == null || fast.next.next == null) + return false; + fast = fast.next.next; + slow = slow.next; + } + return true; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/144.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/144.exercises/solution.md" index b658298f900f62cba9ab8e95786c0db61c8e423b..80a37e8a782684a972f4a22b4a708287a3d8d4b9 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/144.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/144.exercises/solution.md" @@ -57,6 +57,39 @@ ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List preorderTraversal(TreeNode root) { + List resultList = new ArrayList<>(); + if (root == null) { + return resultList; + } + + helper(resultList, root); + + return resultList; + } + + public void helper(List resultList, TreeNode root) { + if (root == null) + return; + + resultList.add(root.val); + helper(resultList, root.left); + helper(resultList, root.right); + + } + +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/145.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/145.exercises/solution.md" index d9e77c77486ac52f4a1b85488e72466d18d789ce..ec4126a91f2b41b1b395b7c0d5453097bcf413e3 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/145.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/145.exercises/solution.md" @@ -20,6 +20,40 @@ ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + +class Solution { + public List postorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + Stack nodeStack = new Stack<>(); + TreeNode nodeTemp = root; + TreeNode preNode = null; + while (nodeTemp != null || !nodeStack.isEmpty()) { + while (nodeTemp != null) { + nodeStack.push(nodeTemp); + nodeTemp = nodeTemp.left; + } + nodeTemp = nodeStack.peek(); + if (nodeTemp.right == null || nodeTemp.right == preNode) { + nodeTemp = nodeStack.pop(); + list.add(nodeTemp.val); + preNode = nodeTemp; + nodeTemp = null; + } else { + nodeTemp = nodeTemp.right; + } + } + return list; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/155.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/155.exercises/solution.md" index 8baf063eba0b170a97ea12a462cd22eb97fd255d..0a8fb8cbed9b247aa2e6bba7375be53da60959b5 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/155.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/155.exercises/solution.md" @@ -43,8 +43,60 @@ minStack.getMin(); --> 返回 -2. ## template ```java +class MinStack { + Stack data_stack; + Stack min_stack; + /** initialize your data structure here. */ + public MinStack() { + data_stack = new Stack(); + min_stack = new Stack(); + } + + public void push(int x) { + data_stack.push(x); + if (min_stack.isEmpty()) { + + min_stack.push(x); + } else { + if (x > min_stack.peek()) { + + x = min_stack.peek(); + } + min_stack.push(x); + } + + } + + public void pop() { + + data_stack.pop(); + min_stack.pop(); + + } + + public int top() { + + return data_stack.peek(); + + } + + public int getMin() { + + return min_stack.peek(); + + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(x); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/config.json" deleted file mode 100644 index 9c18feb0ef9cc4eea74ed33381dba6a564d8f8c4..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-6ee744bf25ac419f93e2635a209f84ef", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.json" deleted file mode 100644 index ba8ede4099ad68c1a2d695f9fc20291659e3dc55..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "af73bd99a21f4a2b8a508d0d2f988966", - "author": "csdn.net", - "keywords": "字符串,交互,模拟" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.md" deleted file mode 100644 index 050fb415374edeb70f552578665973d4ba8fab75..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/157.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 用 Read4 读取 N 个字符 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/config.json" deleted file mode 100644 index 09789331debf9c659c101a8c1156e2f6a39f5e10..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-647ab94a94d64a15a231c62a79e47c40", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.md" deleted file mode 100644 index 25973fa9ae65957f7ce5aa894552398046486095..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/160.exercises/solution.md" +++ /dev/null @@ -1,102 +0,0 @@ -# 相交链表 - -

给你两个单链表的头节点 headAheadB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null

- -

图示两个链表在节点 c1 开始相交

- -

- -

题目数据 保证 整个链式结构中不存在环。

- -

注意,函数返回结果后,链表必须 保持其原始结构

- -

 

- -

示例 1:

- -

- -
-输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
-输出:Intersected at '8'
-解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
-从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
-在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
-
- -

示例 2:

- -

- -
-输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
-输出:Intersected at '2'
-解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
-从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
-在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
-
- -

示例 3:

- -

- -
-输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
-输出:null
-解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
-由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
-这两个链表不相交,因此返回 null 。
-
- -

 

- -

提示:

- -
    -
  • listA 中节点数目为 m
  • -
  • listB 中节点数目为 n
  • -
  • 0 <= m, n <= 3 * 104
  • -
  • 1 <= Node.val <= 105
  • -
  • 0 <= skipA <= m
  • -
  • 0 <= skipB <= n
  • -
  • 如果 listAlistB 没有交点,intersectVal0
  • -
  • 如果 listAlistB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]
  • -
- -

 

- -

进阶:你能否设计一个时间复杂度 O(n) 、仅用 O(1) 内存的解决方案?

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/config.json" deleted file mode 100644 index 219f5309e29191bde097bbd00fae6d9f426e0d69..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-a556b4a993134863ae6dbfe799bc91fb", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.md" deleted file mode 100644 index 072a752703197ec601ef07145d1e9fc2990dec49..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/163.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 缺失的区间 - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/167.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/167.exercises/solution.md" index 61e1103ea7391298ca3e6fe3ac1487606aa907ed..25d289cd97f93ed2fb1b8e37d22892cf81bd9b29 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/167.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/167.exercises/solution.md" @@ -45,7 +45,30 @@ ## template ```java - +class Solution { + public int[] twoSum(int[] numbers, int target) { + int[] answer = new int[2]; + int n = numbers.length; + for (int i = 0; i < n - 1; i++) { + int t = target - numbers[i]; + answer[0] = i + 1; + for (int j = i + 1; j < n; j++) { + if (numbers[j] > t) { + break; + } + if (numbers[j] == t) { + answer[1] = j + 1; + break; + } + } + + if (answer[1] != 0) { + break; + } + } + return answer; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/168.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/168.exercises/solution.md" index 3576ae28b35479a78dc7f996401e611a420e7bdd..f1bfa83ab6ddda1852907254a0bf2c62276933e8 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/168.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/168.exercises/solution.md" @@ -57,7 +57,21 @@ AB -> 28 ## template ```java - +class Solution { + public String convertToTitle(int n) { + if (n <= 0) { + return ""; + } + StringBuilder sb = new StringBuilder(); + while (n > 0) { + n--; + sb.append((char) (n % 26 + 'A')); + + n = n / 26; + } + return sb.reverse().toString(); + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/169.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/169.exercises/solution.md" index 2712756e0415dd433b72f892ecc7672e00ee05d8..bc2c320022faba922c7afa55daf2d1a5b80fcb7a 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/169.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/169.exercises/solution.md" @@ -31,7 +31,23 @@ ## template ```java - +class Solution { + public int majorityElement(int[] nums) { + int res = nums[0]; + int count = 0; + for (int i = 0; i < nums.length; i++) { + if (res == nums[i]) { + count++; + } else { + count--; + } + if (count == 0) { + res = nums[i + 1]; + } + } + return res; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/config.json" deleted file mode 100644 index cb4093a3da8d9df860c4257b83b49507ea5ceaaf..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-d123a15cbda7450da9b03b0be0d8f7ab", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.json" deleted file mode 100644 index 8ce75761940fac8b5ca142f4c43c70c73128825f..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "e0c0e259cf8b4ff3a917be302a88e4a0", - "author": "csdn.net", - "keywords": "设计,数组,哈希表,双指针,数据流" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.md" deleted file mode 100644 index b8c6e7654013c9242a6db589fefc0020cbe6ec92..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/170.exercises/solution.md" +++ /dev/null @@ -1,36 +0,0 @@ -# 两数之和 III - - - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/171.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/171.exercises/solution.md" index 9b076c1ee161d5cddcdc4f8d2f635517f3b71cd6..e12ec0a1147ddbae63ac8894a1b62628c39593f5 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/171.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/171.exercises/solution.md" @@ -60,7 +60,17 @@ ## template ```java - +class Solution { + public int titleToNumber(String s) { + char[] charArray = s.toCharArray(); + int res = 0; + for (int i = 0; i < charArray.length; i++) { + res = res * 26 + (charArray[i] - 'A' + 1); + } + + return res; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/172.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/172.exercises/solution.md" index 9e94bba3bb1a5e29147bbae2005a89e1cc619aba..106b878f661e6a0e24b7899906f6dcc83d2fc12e 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/172.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/172.exercises/solution.md" @@ -45,7 +45,17 @@ ## template ```java - +class Solution { + public int trailingZeroes(int n) { + int count = 0; + while (n >= 5) { + + count += n / 5; + n /= 5; + } + return count; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/config.json" deleted file mode 100644 index 81aaf8cc96196030d420776ee419a16e7fb49999..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-a45abd9f52404d759b8b789bd4078dbb", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.json" deleted file mode 100644 index 3ae786a7c04de7d48261beddd63bdd3c9d95e994..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "12b33b2124354f51a421ccf2475dc997", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.md" deleted file mode 100644 index 565466e6861f87644d0e9237ba25ed4422dd968c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/175.exercises/solution.md" +++ /dev/null @@ -1,69 +0,0 @@ -# 组合两个表 - -

表1: Person

- -
+-------------+---------+
-| 列名         | 类型     |
-+-------------+---------+
-| PersonId    | int     |
-| FirstName   | varchar |
-| LastName    | varchar |
-+-------------+---------+
-PersonId 是上表主键
-
- -

表2: Address

- -
+-------------+---------+
-| 列名         | 类型    |
-+-------------+---------+
-| AddressId   | int     |
-| PersonId    | int     |
-| City        | varchar |
-| State       | varchar |
-+-------------+---------+
-AddressId 是上表主键
-
- -

 

- -

编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:

- -

 

- -
FirstName, LastName, City, State
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/config.json" deleted file mode 100644 index ffa3968495ec776c88c407bf83e2f7c464d859f8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-df1a77b6598a4a829594f1c18a098443", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.json" deleted file mode 100644 index 2e5c32976b5ea262a844b81f733a89e8ef455ea2..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "e113ddbf26614ba787c053f1eac7064a", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.md" deleted file mode 100644 index cb5bcb9c72e14e63f7eee1f317293aa4b2ea47b6..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/176.exercises/solution.md" +++ /dev/null @@ -1,55 +0,0 @@ -# 第二高的薪水 - -

编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。

- -
+----+--------+
-| Id | Salary |
-+----+--------+
-| 1  | 100    |
-| 2  | 200    |
-| 3  | 300    |
-+----+--------+
-
- -

例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null

- -
+---------------------+
-| SecondHighestSalary |
-+---------------------+
-| 200                 |
-+---------------------+
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/config.json" deleted file mode 100644 index e39f2436678cb6e8c6afa87c22d6a5372ab00681..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-567a8bb3433749f7895ea6f9b867a492", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.json" deleted file mode 100644 index ad63f8e6912e7163525efa53d2d415f968dfb5a8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "10f8d50084dd4456b37b50bfcbcea5f5", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.md" deleted file mode 100644 index e4aaabed5e0869f972cf60c61201c5c2e7c29dd2..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/181.exercises/solution.md" +++ /dev/null @@ -1,56 +0,0 @@ -# 超过经理收入的员工 - -

Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

- -
+----+-------+--------+-----------+
-| Id | Name  | Salary | ManagerId |
-+----+-------+--------+-----------+
-| 1  | Joe   | 70000  | 3         |
-| 2  | Henry | 80000  | 4         |
-| 3  | Sam   | 60000  | NULL      |
-| 4  | Max   | 90000  | NULL      |
-+----+-------+--------+-----------+
-
- -

给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

- -
+----------+
-| Employee |
-+----------+
-| Joe      |
-+----------+
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/config.json" deleted file mode 100644 index 9be4bb771e0640abb9266616a4bb99c794cd2f2e..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-a47f1a9a549d4c9a9480d19650e4bf82", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.json" deleted file mode 100644 index 4d1ac3a8eeec4ddc798fd9321fa8c1e98add7884..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "401c70b35e71405d937641002e7fa16f", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.md" deleted file mode 100644 index 4f27315eab100ebda7a3f9bfebb51b3f9d89da41..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/182.exercises/solution.md" +++ /dev/null @@ -1,59 +0,0 @@ -# 查找重复的电子邮箱 - -

编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。

- -

示例:

- -
+----+---------+
-| Id | Email   |
-+----+---------+
-| 1  | a@b.com |
-| 2  | c@d.com |
-| 3  | a@b.com |
-+----+---------+
-
- -

根据以上输入,你的查询应返回以下结果:

- -
+---------+
-| Email   |
-+---------+
-| a@b.com |
-+---------+
-
- -

说明:所有电子邮箱都是小写字母。

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/config.json" deleted file mode 100644 index 06838aac8645dd10930fb86e94bb3e90e7bfc23c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-d3083ad3c00e4527bcc57eb560caec93", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.json" deleted file mode 100644 index 3e3b6b2697df1843be1f519374bddc0796bab1a8..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "bf177aad4bae44fdba718cf69c829305", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.md" deleted file mode 100644 index eb3e3e732f2b5f98f0b7469c70554cde13ee9f9c..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/183.exercises/solution.md" +++ /dev/null @@ -1,69 +0,0 @@ -# 从不订购的客户 - -

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

- -

Customers 表:

- -
+----+-------+
-| Id | Name  |
-+----+-------+
-| 1  | Joe   |
-| 2  | Henry |
-| 3  | Sam   |
-| 4  | Max   |
-+----+-------+
-
- -

Orders 表:

- -
+----+------------+
-| Id | CustomerId |
-+----+------------+
-| 1  | 3          |
-| 2  | 1          |
-+----+------------+
-
- -

例如给定上述表格,你的查询应返回:

- -
+-----------+
-| Customers |
-+-----------+
-| Henry     |
-| Max       |
-+-----------+
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/190.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/190.exercises/solution.md" index 0bba34d0004c95088d04d14ea915e876f85b4685..0a4f4361f1b3eed389d7f735742b64e1139a5b54 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/190.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/190.exercises/solution.md" @@ -43,7 +43,19 @@ ## template ```java - +public class Solution { + + public int reverseBits(int n) { + int m = 0; + for (int i = 0; i < 32; i++) { + m <<= 1; + m = m | (n & 1); + n >>= 1; + } + return m; + + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/191.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/191.exercises/solution.md" index 7e89e9eb43a54dbc9fd9417fd700f87ad7feac7d..138c8617f764598da8d35ffe60561f8edb578fef 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/191.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/191.exercises/solution.md" @@ -59,8 +59,19 @@ ## template ```java - - +public class Solution { + + public int hammingWeight(int n) { + int count = 0; + while (n != 0) { + if ((n & 1) == 1) + count++; + n >>>= 1; + } + return count; + + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/config.json" deleted file mode 100644 index c2d558656f7c20b9baf3cda6c65ad8005ee4f7ed..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-84fa09a05894463092b99220adc7965f", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.json" deleted file mode 100644 index 01a9816864b9848dd5bf8e9f43f34e7a48650228..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "fad136416afe46dc8749c0cc00e78f41", - "author": "csdn.net", - "keywords": "shell" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.md" deleted file mode 100644 index ccbaffa6f282634eaeb66be5ded95c92c5806c00..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/193.exercises/solution.md" +++ /dev/null @@ -1,60 +0,0 @@ -# 有效电话号码 - -

给定一个包含电话号码列表(一行一个电话号码)的文本文件 file.txt,写一个单行 bash 脚本输出所有有效的电话号码。

- -

你可以假设一个有效的电话号码必须满足以下两种格式: (xxx) xxx-xxxx 或 xxx-xxx-xxxx。(x 表示一个数字)

- -

你也可以假设每行前后没有多余的空格字符。

- -

 

- -

示例:

- -

假设 file.txt 内容如下:

- -
-987-123-4567
-123 456 7890
-(123) 456-7890
-
- -

你的脚本应当输出下列有效的电话号码:

- -
-987-123-4567
-(123) 456-7890
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/config.json" deleted file mode 100644 index 7ff14b564d7d1675d9e152f08715036400a003c5..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-7957e984ebbb40f28662b4d0696a2348", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.json" deleted file mode 100644 index a719c43b79972de40af8a28f49ec207518f0bb96..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "b5a5f6d91dde427e833e10b231f43695", - "author": "csdn.net", - "keywords": "shell" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.md" deleted file mode 100644 index 62389a0a9499c3e97d079fcfc75f6e5680f97614..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/195.exercises/solution.md" +++ /dev/null @@ -1,62 +0,0 @@ -# 第十行 - -

给定一个文本文件 file.txt,请只打印这个文件中的第十行。

- -

示例:

- -

假设 file.txt 有如下内容:

- -
Line 1
-Line 2
-Line 3
-Line 4
-Line 5
-Line 6
-Line 7
-Line 8
-Line 9
-Line 10
-
- -

你的脚本应当显示第十行:

- -
Line 10
-
- -

说明:
-1. 如果文件少于十行,你应当输出什么?
-2. 至少有三种不同的解法,请尝试尽可能多的方法来解题。

- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/config.json" deleted file mode 100644 index 696435532efd40a668aafe6b22008e000dcc211f..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-3980cf57071844a892f15675b6480b60", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.json" deleted file mode 100644 index 939468945829fa0b79c293570c819b70a0250163..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "f48bad11f13b4817b5a714755e4be4a4", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.md" deleted file mode 100644 index 981c0f3166dab0eb4d6d15bf4081ad743fc22576..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/196.exercises/solution.md" +++ /dev/null @@ -1,66 +0,0 @@ -# 删除重复的电子邮箱 - -

编写一个 SQL 查询,来删除 Person 表中所有重复的电子邮箱,重复的邮箱里只保留 Id 最小 的那个。

- -
+----+------------------+
-| Id | Email            |
-+----+------------------+
-| 1  | john@example.com |
-| 2  | bob@example.com  |
-| 3  | john@example.com |
-+----+------------------+
-Id 是这个表的主键。
-
- -

例如,在运行你的查询语句之后,上面的 Person 表应返回以下几行:

- -
+----+------------------+
-| Id | Email            |
-+----+------------------+
-| 1  | john@example.com |
-| 2  | bob@example.com  |
-+----+------------------+
-
- -

 

- -

提示:

- -
    -
  • 执行 SQL 之后,输出是整个 Person 表。
  • -
  • 使用 delete 语句。
  • -
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/config.json" "b/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/config.json" deleted file mode 100644 index 6357eb8a167a336f03514fd01b1c855eaf4cf093..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/config.json" +++ /dev/null @@ -1,10 +0,0 @@ -{ - "node_id": "dailycode-0417c9bb063543e499cb3d905c95ad6e", - "keywords": [], - "children": [], - "keywords_must": [], - "keywords_forbid": [], - "export": [ - "solution.json" - ] -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.json" "b/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.json" deleted file mode 100644 index b4910a8a8f33c701761df54cab24eb21bd284b3e..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "code_options", - "source": "solution.md", - "exercise_id": "cb4203a77a8b40aca3b7eb1fc3fbe4f9", - "author": "csdn.net", - "keywords": "数据库" -} \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.md" deleted file mode 100644 index a4a85c0bcc80f83fc8a72b30a8d62e3c5ba4e0bf..0000000000000000000000000000000000000000 --- "a/data_source/exercises/\347\256\200\345\215\225/java/197.exercises/solution.md" +++ /dev/null @@ -1,82 +0,0 @@ -# 上升的温度 - -
-
-

Weather

- -
-+---------------+---------+
-| Column Name   | Type    |
-+---------------+---------+
-| id            | int     |
-| recordDate    | date    |
-| temperature   | int     |
-+---------------+---------+
-id 是这个表的主键
-该表包含特定日期的温度信息
- -

 

- -

编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 id

- -

返回结果 不要求顺序

- -

查询结果格式如下例:

- -
-Weather
-+----+------------+-------------+
-| id | recordDate | Temperature |
-+----+------------+-------------+
-| 1  | 2015-01-01 | 10          |
-| 2  | 2015-01-02 | 25          |
-| 3  | 2015-01-03 | 20          |
-| 4  | 2015-01-04 | 30          |
-+----+------------+-------------+
-
-Result table:
-+----+
-| id |
-+----+
-| 2  |
-| 4  |
-+----+
-2015-01-02 的温度比前一天高(10 -> 25)
-2015-01-04 的温度比前一天高(20 -> 30)
-
-
-
- - -## template - -```java - - -``` - -## 答案 - -```java - -``` - -## 选项 - -### A - -```java - -``` - -### B - -```java - -``` - -### C - -```java - -``` \ No newline at end of file diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/202.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/202.exercises/solution.md" index 308b7058d1ab0ba6cde5d07c469ef0cbe84e0b58..852fc68e2f1b86ca3e15cf18df3afef7ae6d8003 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/202.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/202.exercises/solution.md" @@ -45,7 +45,28 @@ ## template ```java - +class Solution { + public boolean isHappy(int n) { + List list = new ArrayList(); + list.add(n); + while (n != 1) { + int temp = 0; + while (n != 0) { + temp += (n % 10) * (n % 10); + n = n / 10; + } + n = temp; + if (list.contains(n)) { + break; + } else { + list.add(n); + } + + } + + return n == 1; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/203.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/203.exercises/solution.md" index 88883b4c296449e9cd365555dc2bfb69650982e3..1eff0ebcd8403af54742ea9cbde4e738b7fd8a73 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/203.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/203.exercises/solution.md" @@ -38,7 +38,31 @@ ## template ```java - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } +} + +class Solution { + public ListNode removeElements(ListNode head, int val) { + ListNode top = new ListNode(0); + top.next = head; + ListNode pre = top; + ListNode temp = head; + while (temp != null) { + if (temp.val == val) + pre.next = temp.next; + else + pre = temp; + temp = temp.next; + } + return top.next; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/204.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/204.exercises/solution.md" index 52ee45d0269701d443721b3389167d6b47971efd..a31da07bb16d32bf42e56dabc0b6ea717439ecac 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/204.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/204.exercises/solution.md" @@ -35,8 +35,24 @@ ## template ```java - - +class Solution { + public int countPrimes(int n) { + int flag = 0; + if (n > 2) + flag = 1; + for (int i = 0; i < n; i++) { + for (int j = 2; j < i; j++) { + if (i % j == 0) { + break; + } + if (j == i - 1) { + flag++; + } + } + } + return flag; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/205.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/205.exercises/solution.md" index f4a32ba0411a32f5effb6a125fa088ef503e3a31..d6099de5ef3412b7b997ace05cc4af05a90647fd 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/205.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/205.exercises/solution.md" @@ -39,7 +39,29 @@ ## template ```java - +class Solution { + public boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) { + return false; + } + Map somorphicMap = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + char key = s.charAt(i); + char value = t.charAt(i); + if (somorphicMap.get(key) != null) { + if (somorphicMap.get(key) != value) { + return false; + } + } else { + if (somorphicMap.containsValue(value)) { + return false; + } + somorphicMap.put(s.charAt(i), t.charAt(i)); + } + } + return true; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/206.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/206.exercises/solution.md" index 652ba61872906e879a69e613b21de59e3329e099..a3dd03ecd035c389f445a52e54141ef0356668ae 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/206.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/206.exercises/solution.md" @@ -45,8 +45,31 @@ ## template ```java +public class ListNode { + int val; + ListNode next; + ListNode(int x) { + val = x; + } +} +class Solution { + public ListNode reverseList(ListNode head) { + ListNode pre = null; + while (head != null) { + + ListNode tmp = head.next; + + head.next = pre; + + pre = head; + + head = tmp; + } + return pre; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/217.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/217.exercises/solution.md" index f407da5596b778cbede2141d028aa4f62f7c3938..c8f0ac610a9b2cca3734a3051f53db1082054d15 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/217.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/217.exercises/solution.md" @@ -28,7 +28,18 @@ ## template ```java - +class Solution { + public boolean containsDuplicate(int[] nums) { + Map map = new HashMap(); + + for (int x : nums) { + if (map.containsKey(x)) + return true; + map.put(x, x); + } + return false; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/219.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/219.exercises/solution.md" index 5f89c74dc8673b6b49e6e1ceaec032929e344509..0cbe36bb7167773f000617d1a54463029386d1e4 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/219.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/219.exercises/solution.md" @@ -23,8 +23,30 @@ ## template ```java - - +public class Solution { + public boolean containsNearbyDuplicate(int[] nums, int k) { + int left = 0; + int right = -1; + HashMap hashMap = new HashMap<>(); + while (left < nums.length) { + if (right + 1 < nums.length && right - left < k) { + right++; + if (hashMap.containsKey(nums[right])) { + return true; + } else { + hashMap.put(nums[right], 1); + } + } else { + hashMap.put(nums[left], hashMap.get(nums[left]) - 1); + if (hashMap.get(nums[left]) == 0) { + hashMap.remove(nums[left]); + } + left++; + } + } + return false; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/225.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/225.exercises/solution.md" index 3c98980b5b5fa5f28e8c768f16551a75184fa148..02768f37366bae9af01cde4ccbace967c43fdf49 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/225.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/225.exercises/solution.md" @@ -58,8 +58,50 @@ myStack.empty(); // 返回 False ## template ```java +class MyStack { + private Queue queue; + /** Initialize your data structure here. */ + public MyStack() { + queue = new LinkedList<>(); + + } + + /** + * 在添加数据的时候进行翻转 + */ + /** Push element x onto stack. */ + public void push(int x) { + Queue temp = new LinkedList<>(); + + while (!queue.isEmpty()) { + + temp.add(queue.poll()); + } + + queue.add(x); + + while (!temp.isEmpty()) { + queue.add(temp.poll()); + } + } + + /** Removes the element on top of the stack and returns that element. */ + public int pop() { + return queue.poll(); + } + + /** Get the top element. */ + public int top() { + return queue.peek(); + } + + /** Returns whether the stack is empty. */ + public boolean empty() { + return queue.isEmpty(); + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/226.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/226.exercises/solution.md" index a6a19b4a7ffb0f5469c76c7332ed859adafe01f9..5018d0699ec3ab89fa5253c474a66c559459741a 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/226.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/226.exercises/solution.md" @@ -29,8 +29,41 @@ ## template ```java - - +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } +} + + +class Solution { + + public TreeNode invertTree(TreeNode root) { + + if (root == null) + return null; + Queue queue = new LinkedList<>(); + queue.offer(root); + while (!queue.isEmpty()) { + TreeNode node = queue.poll(); + TreeNode rightTree = node.right; + node.right = node.left; + node.left = rightTree; + if (node.left != null) { + queue.offer(node.left); + } + if (node.right != null) { + queue.offer(node.right); + } + } + return root; + } + +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/228.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/228.exercises/solution.md" index e1118a85aa5df56e053c927b3011fa02ec10c195..cac3170d2be979f090b6f257c295b0599c8cacfa 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/228.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/228.exercises/solution.md" @@ -73,6 +73,30 @@ ```java +class Solution { + public List summaryRanges(int[] nums) { + List list = new ArrayList<>(); + int pre = 0; + int next = 0; + for (int i = 0; i < nums.length; i++) { + if (i + 1 < nums.length && nums[i + 1] - nums[i] == 1) { + next = i + 1; + } else { + if (next < i) + next = i; + if (pre != next) { + list.add(nums[pre] + "->" + nums[next]); + pre = i + 1; + } + if (pre == next) { + list.add(nums[pre] + ""); + pre = i + 1; + } + } + } + return list; + } +} ``` diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/231.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/231.exercises/solution.md" index 66823ce15d291fa7f3e981da5e4857f06a3159cd..f6e36dcc6772d5ecc3bc8c8de9076600564e4a70 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/231.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/231.exercises/solution.md" @@ -59,8 +59,23 @@ ## template ```java - - +class Solution { + public boolean isPowerOfTwo(int n) { + if (n <= 0) + return false; + return countBit(n) == 1; + } + + public int countBit(int num) { + int count = 0; + while (num != 0) { + count += (num & 1); + num >>= 1; + } + return count; + + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/232.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/232.exercises/solution.md" index 1a92dcaf97020cdea7b6d620ea2b9eabf5fc47e0..1acfb954062e6cd604200da8ef393f478158bef8 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/232.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/232.exercises/solution.md" @@ -65,8 +65,52 @@ myQueue.empty(); // return false ## template ```java - - +class MyQueue { + Stack s1; + Stack s2; + + /** Initialize your data structure here. */ + public MyQueue() { + s1 = new Stack(); + s2 = new Stack(); + } + + /** Push element x to the back of queue. */ + public void push(int x) { + while (!s1.empty()) + s2.push(s1.pop()); + s1.push(x); + while (!s2.empty()) + s1.push(s2.pop()); + return; + } + + /** Removes the element from in front of queue and returns that element. */ + public int pop() { + return s1.pop(); + } + + /** Get the front element. */ + public int peek() { + int ret = s1.pop(); + s1.push(ret); + return ret; + } + + /** Returns whether the queue is empty. */ + public boolean empty() { + return s1.empty(); + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/234.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/234.exercises/solution.md" index 101796813c1997ab8b174feb039d828b340e75f2..07965ad56314eb852036ede7bd78ada4496d5376 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/234.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/234.exercises/solution.md" @@ -35,8 +35,51 @@ ## template ```java - - +public class ListNode { + int val; + ListNode next; + + ListNode(int x) { + val = x; + } + +} + +class Solution { + public boolean isPalindrome(ListNode head) { + + if (head == null || head.next == null) { + return true; + } + + ListNode slow = head; + ListNode quick = head; + + while (quick != null && quick.next != null) { + quick = quick.next.next; + slow = slow.next; + } + ListNode pre = null; + ListNode p = slow; + + while (p != null) { + ListNode temp = p.next; + p.next = pre; + pre = p; + p = temp; + } + + while (pre != null) { + if (pre.val == head.val) { + pre = pre.next; + head = head.next; + } else { + return false; + } + } + return true; + } +} ``` ## 答案 diff --git "a/data_source/exercises/\347\256\200\345\215\225/java/235.exercises/solution.md" "b/data_source/exercises/\347\256\200\345\215\225/java/235.exercises/solution.md" index 3fe98129bed3bb1f3479e52df746a0aaf034111d..9be2cb38cc4a643df42b04403c5e61ff5e79c2fe 100644 --- "a/data_source/exercises/\347\256\200\345\215\225/java/235.exercises/solution.md" +++ "b/data_source/exercises/\347\256\200\345\215\225/java/235.exercises/solution.md" @@ -36,8 +36,29 @@ ## template ```java +public class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode(int x) { + val = x; + } +} +class Solution { + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + + if (p.val < root.val && q.val < root.val) + return lowestCommonAncestor(root.left, p, q); + else if (p.val > root.val && q.val > root.val) + return lowestCommonAncestor(root.right, p, q); + else + return root; + } + +} ``` ## 答案