mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'master' into patch-26
This commit is contained in:
@ -202,6 +202,23 @@ func isValid(s string) bool {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Ruby:
|
||||||
|
```ruby
|
||||||
|
def is_valid(strs)
|
||||||
|
symbol_map = {')' => '(', '}' => '{', ']' => '['}
|
||||||
|
stack = []
|
||||||
|
strs.size.times {|i|
|
||||||
|
c = strs[i]
|
||||||
|
if symbol_map.has_key?(c)
|
||||||
|
top_e = stack.shift
|
||||||
|
return false if symbol_map[c] != top_e
|
||||||
|
else
|
||||||
|
stack.unshift(c)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
stack.empty?
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -123,6 +123,24 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int removeElement(int[] nums, int val) {
|
||||||
|
|
||||||
|
// 快慢指针
|
||||||
|
int fastIndex = 0;
|
||||||
|
int slowIndex;
|
||||||
|
for (slowIndex = 0; fastIndex < nums.length; fastIndex++) {
|
||||||
|
if (nums[fastIndex] != val) {
|
||||||
|
nums[slowIndex] = nums[fastIndex];
|
||||||
|
slowIndex++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return slowIndex;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -172,4 +190,4 @@ var removeElement = (nums, val) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -565,6 +565,54 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 基于窗口滑动的算法
|
||||||
|
* <p>
|
||||||
|
* 时间复杂度:O(m*n)
|
||||||
|
* 空间复杂度:O(1)
|
||||||
|
* 注:n为haystack的长度,m为needle的长度
|
||||||
|
*/
|
||||||
|
public int strStr(String haystack, String needle) {
|
||||||
|
int m = needle.length();
|
||||||
|
// 当 needle 是空字符串时我们应当返回 0
|
||||||
|
if (m == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int n = haystack.length();
|
||||||
|
if (n < m) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
|
while (i < n - m + 1) {
|
||||||
|
// 找到首字母相等
|
||||||
|
while (i < n && haystack.charAt(i) != needle.charAt(j)) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (i == n) {// 没有首字母相等的
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// 遍历后续字符,判断是否相等
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
while (i < n && j < m && haystack.charAt(i) == needle.charAt(j)) {
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j == m) {// 找到
|
||||||
|
return i - j;
|
||||||
|
} else {// 未找到
|
||||||
|
i -= j - 1;
|
||||||
|
j = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// 方法一
|
// 方法一
|
||||||
class Solution {
|
class Solution {
|
||||||
|
@ -107,7 +107,19 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def canJump(self, nums: List[int]) -> bool:
|
||||||
|
cover = 0
|
||||||
|
if len(nums) == 1: return True
|
||||||
|
i = 0
|
||||||
|
# python不支持动态修改for循环中变量,使用while循环代替
|
||||||
|
while i <= cover:
|
||||||
|
cover = max(i + nums[i], cover)
|
||||||
|
if cover >= len(nums) - 1: return True
|
||||||
|
i += 1
|
||||||
|
return False
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -137,7 +137,35 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int[][] merge(int[][] intervals) {
|
||||||
|
List<int[]> res = new LinkedList<>();
|
||||||
|
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
if (o1[0] != o2[0]) {
|
||||||
|
return Integer.compare(o1[0],o2[0]);
|
||||||
|
} else {
|
||||||
|
return Integer.compare(o1[1],o2[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int start = intervals[0][0];
|
||||||
|
for (int i = 1; i < intervals.length; i++) {
|
||||||
|
if (intervals[i][0] > intervals[i - 1][1]) {
|
||||||
|
res.add(new int[]{start, intervals[i - 1][1]});
|
||||||
|
start = intervals[i][0];
|
||||||
|
} else {
|
||||||
|
intervals[i][1] = Math.max(intervals[i][1], intervals[i - 1][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.add(new int[]{start, intervals[intervals.length - 1][1]});
|
||||||
|
return res.toArray(new int[res.size()][]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -151,4 +179,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -127,7 +127,23 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int climbStairs(int n) {
|
||||||
|
int[] dp = new int[n + 1];
|
||||||
|
int[] weight = {1,2};
|
||||||
|
dp[0] = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i <= n; i++) {
|
||||||
|
for (int j = 0; j < weight.length; j++) {
|
||||||
|
if (i >= weight[j]) dp[i] += dp[i - weight[j]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -141,4 +157,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -254,6 +254,19 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public boolean isValidBST(TreeNode root) {
|
||||||
|
return isValidBST_2(root, Long.MIN_VALUE, Long.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValidBST_2 (TreeNode root, long lo, long hi) {
|
||||||
|
if (root == null) return true;
|
||||||
|
if (root.val >= hi || root.val <= lo) return false;
|
||||||
|
return isValidBST_2(root.left,lo,root.val) && isValidBST_2(root.right,root.val,hi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
@ -286,4 +299,4 @@ func isBST(root *TreeNode, min, max int) bool {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -254,7 +254,94 @@ public:
|
|||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
public class N0101 {
|
||||||
|
/**
|
||||||
|
* 解法1:DFS,递归。
|
||||||
|
*/
|
||||||
|
public boolean isSymmetric2(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return compare(root.left, root.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean compare(TreeNode left, TreeNode right) {
|
||||||
|
if (left == null && right == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (left != null && right == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (left == null && right != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left.val == right.val) {
|
||||||
|
return compare(left.left, right.right) && compare(left.right, right.left);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解法2:DFS,迭代
|
||||||
|
*/
|
||||||
|
public boolean isSymmetric3(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!equal(root.left, root.right)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Deque<TreeNode> st = new LinkedList<>();
|
||||||
|
|
||||||
|
st.push(root.right);
|
||||||
|
st.push(root.left);
|
||||||
|
|
||||||
|
TreeNode curR = root.right;
|
||||||
|
TreeNode curL = root.left;
|
||||||
|
|
||||||
|
while (!st.isEmpty()) {
|
||||||
|
curL = st.pop();
|
||||||
|
curR = st.pop();
|
||||||
|
|
||||||
|
// 前序,处理
|
||||||
|
if (!equal(curL, curR)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curR != null && curL != null) {
|
||||||
|
st.push(curL.right);
|
||||||
|
st.push(curR.left);
|
||||||
|
st.push(curR.right);
|
||||||
|
st.push(curL.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean equal(TreeNode l, TreeNode r) {
|
||||||
|
if (l == null && r == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (l != null && r == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (l == null && r != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (l.val == r.val) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -283,4 +370,4 @@ const check = (leftPtr, rightPtr) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -420,54 +420,236 @@ public:
|
|||||||
Java:
|
Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
|
// 102.二叉树的层序遍历
|
||||||
class Solution {
|
class Solution {
|
||||||
public List<List<Integer>> resList=new ArrayList<List<Integer>>();
|
public List<List<Integer>> resList = new ArrayList<List<Integer>>();
|
||||||
|
|
||||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||||
//checkFun01(root,0);
|
//checkFun01(root,0);
|
||||||
checkFun02(root);
|
checkFun02(root);
|
||||||
|
|
||||||
return resList;
|
return resList;
|
||||||
}
|
}
|
||||||
|
|
||||||
//DFS--递归方式
|
//DFS--递归方式
|
||||||
public void checkFun01(TreeNode node,Integer deep){
|
public void checkFun01(TreeNode node, Integer deep) {
|
||||||
if(node==null) return;
|
if (node == null) return;
|
||||||
deep++;
|
deep++;
|
||||||
|
|
||||||
if(resList.size()<deep){
|
if (resList.size() < deep) {
|
||||||
//当层级增加时,list的Item也增加,利用list的索引值进行层级界定
|
//当层级增加时,list的Item也增加,利用list的索引值进行层级界定
|
||||||
List<Integer> item=new ArrayList<Integer>();
|
List<Integer> item = new ArrayList<Integer>();
|
||||||
resList.add(item);
|
resList.add(item);
|
||||||
}
|
}
|
||||||
resList.get(deep-1).add(node.val);
|
resList.get(deep - 1).add(node.val);
|
||||||
|
|
||||||
checkFun01(node.left,deep);
|
checkFun01(node.left, deep);
|
||||||
checkFun01(node.right,deep);
|
checkFun01(node.right, deep);
|
||||||
}
|
}
|
||||||
|
|
||||||
//BFS--迭代方式--借助队列
|
//BFS--迭代方式--借助队列
|
||||||
public void checkFun02(TreeNode node){
|
public void checkFun02(TreeNode node) {
|
||||||
if(node==null) return;
|
if (node == null) return;
|
||||||
Queue<TreeNode> que=new LinkedList<TreeNode>();
|
Queue<TreeNode> que = new LinkedList<TreeNode>();
|
||||||
que.offer(node);
|
que.offer(node);
|
||||||
|
|
||||||
while(!que.isEmpty()){
|
while (!que.isEmpty()) {
|
||||||
List<Integer> itemList=new ArrayList<Integer>();
|
List<Integer> itemList = new ArrayList<Integer>();
|
||||||
int len=que.size();
|
int len = que.size();
|
||||||
|
|
||||||
while(len>0){
|
while (len > 0) {
|
||||||
TreeNode tmpNode=que.poll();
|
TreeNode tmpNode = que.poll();
|
||||||
itemList.add(tmpNode.val);
|
itemList.add(tmpNode.val);
|
||||||
|
|
||||||
if(tmpNode.left!=null) que.offer(tmpNode.left);
|
if (tmpNode.left != null) que.offer(tmpNode.left);
|
||||||
if(tmpNode.right!=null) que.offer(tmpNode.right);
|
if (tmpNode.right != null) que.offer(tmpNode.right);
|
||||||
len--;
|
len--;
|
||||||
}
|
}
|
||||||
|
|
||||||
resList.add(itemList);
|
resList.add(itemList);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 107. 二叉树的层序遍历 II
|
||||||
|
public class N0107 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解法:队列,迭代。
|
||||||
|
* 层序遍历,再翻转数组即可。
|
||||||
|
*/
|
||||||
|
public List<List<Integer>> solution1(TreeNode root) {
|
||||||
|
List<List<Integer>> list = new ArrayList<>();
|
||||||
|
Deque<TreeNode> que = new LinkedList<>();
|
||||||
|
|
||||||
|
if (root == null) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
que.offerLast(root);
|
||||||
|
while (!que.isEmpty()) {
|
||||||
|
List<Integer> levelList = new ArrayList<>();
|
||||||
|
|
||||||
|
int levelSize = que.size();
|
||||||
|
for (int i = 0; i < levelSize; i++) {
|
||||||
|
TreeNode peek = que.peekFirst();
|
||||||
|
levelList.add(que.pollFirst().val);
|
||||||
|
|
||||||
|
if (peek.left != null) {
|
||||||
|
que.offerLast(peek.left);
|
||||||
|
}
|
||||||
|
if (peek.right != null) {
|
||||||
|
que.offerLast(peek.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(levelList);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
for (int i = list.size() - 1; i >= 0; i-- ) {
|
||||||
|
result.add(list.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 199.二叉树的右视图
|
||||||
|
public class N0199 {
|
||||||
|
/**
|
||||||
|
* 解法:队列,迭代。
|
||||||
|
* 每次返回每层的最后一个字段即可。
|
||||||
|
*
|
||||||
|
* 小优化:每层右孩子先入队。代码略。
|
||||||
|
*/
|
||||||
|
public List<Integer> rightSideView(TreeNode root) {
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
Deque<TreeNode> que = new LinkedList<>();
|
||||||
|
|
||||||
|
if (root == null) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
que.offerLast(root);
|
||||||
|
while (!que.isEmpty()) {
|
||||||
|
int levelSize = que.size();
|
||||||
|
|
||||||
|
for (int i = 0; i < levelSize; i++) {
|
||||||
|
TreeNode poll = que.pollFirst();
|
||||||
|
|
||||||
|
if (poll.left != null) {
|
||||||
|
que.addLast(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
que.addLast(poll.right);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == levelSize - 1) {
|
||||||
|
list.add(poll.val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 637. 二叉树的层平均值
|
||||||
|
public class N0637 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解法:队列,迭代。
|
||||||
|
* 每次返回每层的最后一个字段即可。
|
||||||
|
*/
|
||||||
|
public List<Double> averageOfLevels(TreeNode root) {
|
||||||
|
List<Double> list = new ArrayList<>();
|
||||||
|
Deque<TreeNode> que = new LinkedList<>();
|
||||||
|
|
||||||
|
if (root == null) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
que.offerLast(root);
|
||||||
|
while (!que.isEmpty()) {
|
||||||
|
TreeNode peek = que.peekFirst();
|
||||||
|
|
||||||
|
int levelSize = que.size();
|
||||||
|
double levelSum = 0.0;
|
||||||
|
for (int i = 0; i < levelSize; i++) {
|
||||||
|
TreeNode poll = que.pollFirst();
|
||||||
|
|
||||||
|
levelSum += poll.val;
|
||||||
|
|
||||||
|
if (poll.left != null) {
|
||||||
|
que.addLast(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
que.addLast(poll.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(levelSum / levelSize);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 429. N 叉树的层序遍历
|
||||||
|
public class N0429 {
|
||||||
|
/**
|
||||||
|
* 解法1:队列,迭代。
|
||||||
|
*/
|
||||||
|
public List<List<Integer>> levelOrder(Node root) {
|
||||||
|
List<List<Integer>> list = new ArrayList<>();
|
||||||
|
Deque<Node> que = new LinkedList<>();
|
||||||
|
|
||||||
|
if (root == null) {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
que.offerLast(root);
|
||||||
|
while (!que.isEmpty()) {
|
||||||
|
int levelSize = que.size();
|
||||||
|
List<Integer> levelList = new ArrayList<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < levelSize; i++) {
|
||||||
|
Node poll = que.pollFirst();
|
||||||
|
|
||||||
|
levelList.add(poll.val);
|
||||||
|
|
||||||
|
List<Node> children = poll.children;
|
||||||
|
if (children == null || children.size() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (Node child : children) {
|
||||||
|
if (child != null) {
|
||||||
|
que.offerLast(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list.add(levelList);
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
public int val;
|
||||||
|
public List<Node> children;
|
||||||
|
|
||||||
|
public Node() {}
|
||||||
|
|
||||||
|
public Node(int _val) {
|
||||||
|
val = _val;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node(int _val, List<Node> _children) {
|
||||||
|
val = _val;
|
||||||
|
children = _children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -232,6 +232,52 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 递归法
|
||||||
|
*/
|
||||||
|
public int maxDepth(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int leftDepth = maxDepth(root.left);
|
||||||
|
int rightDepth = maxDepth(root.right);
|
||||||
|
return Math.max(leftDepth, rightDepth) + 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 迭代法,使用层序遍历
|
||||||
|
*/
|
||||||
|
public int maxDepth(TreeNode root) {
|
||||||
|
if(root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Deque<TreeNode> deque = new LinkedList<>();
|
||||||
|
deque.offer(root);
|
||||||
|
int depth = 0;
|
||||||
|
while (!deque.isEmpty()) {
|
||||||
|
int size = deque.size();
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode poll = deque.poll();
|
||||||
|
if (poll.left != null) {
|
||||||
|
deque.offer(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
deque.offer(poll.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -239,10 +285,16 @@ Python:
|
|||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
|
||||||
|
JavaScript
|
||||||
|
```javascript
|
||||||
|
var maxDepth = function(root) {
|
||||||
|
if (!root) return root
|
||||||
|
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right))
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -582,7 +582,40 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode buildTree(int[] inorder, int[] postorder) {
|
||||||
|
return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length);
|
||||||
|
}
|
||||||
|
public TreeNode buildTree1(int[] inorder, int inLeft, int inRight,
|
||||||
|
int[] postorder, int postLeft, int postRight) {
|
||||||
|
// 没有元素了
|
||||||
|
if (inRight - inLeft < 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 只有一个元素了
|
||||||
|
if (inRight - inLeft == 1) {
|
||||||
|
return new TreeNode(inorder[inLeft]);
|
||||||
|
}
|
||||||
|
// 后序数组postorder里最后一个即为根结点
|
||||||
|
int rootVal = postorder[postRight - 1];
|
||||||
|
TreeNode root = new TreeNode(rootVal);
|
||||||
|
int rootIndex = 0;
|
||||||
|
// 根据根结点的值找到该值在中序数组inorder里的位置
|
||||||
|
for (int i = inLeft; i < inRight; i++) {
|
||||||
|
if (inorder[i] == rootVal) {
|
||||||
|
rootIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 根据rootIndex划分左右子树
|
||||||
|
root.left = buildTree1(inorder, inLeft, rootIndex,
|
||||||
|
postorder, postLeft, postLeft + (rootIndex - inLeft));
|
||||||
|
root.right = buildTree1(inorder, rootIndex + 1, inRight,
|
||||||
|
postorder, postLeft + (rootIndex - inLeft), postRight - 1);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -596,4 +629,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -244,4 +244,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -355,6 +355,146 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 递归法
|
||||||
|
*/
|
||||||
|
public boolean isBalanced(TreeNode root) {
|
||||||
|
return getHeight(root) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getHeight(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int leftHeight = getHeight(root.left);
|
||||||
|
if (leftHeight == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int rightHeight = getHeight(root.right);
|
||||||
|
if (rightHeight == -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
// 左右子树高度差大于1,return -1表示已经不是平衡树了
|
||||||
|
if (Math.abs(leftHeight - rightHeight) > 1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return Math.max(leftHeight, rightHeight) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 迭代法,效率较低,计算高度时会重复遍历
|
||||||
|
* 时间复杂度:O(n^2)
|
||||||
|
*/
|
||||||
|
public boolean isBalanced(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
|
TreeNode pre = null;
|
||||||
|
while (root!= null || !stack.isEmpty()) {
|
||||||
|
while (root != null) {
|
||||||
|
stack.push(root);
|
||||||
|
root = root.left;
|
||||||
|
}
|
||||||
|
TreeNode inNode = stack.peek();
|
||||||
|
// 右结点为null或已经遍历过
|
||||||
|
if (inNode.right == null || inNode.right == pre) {
|
||||||
|
// 比较左右子树的高度差,输出
|
||||||
|
if (Math.abs(getHeight(inNode.left) - getHeight(inNode.right)) > 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
stack.pop();
|
||||||
|
pre = inNode;
|
||||||
|
root = null;// 当前结点下,没有要遍历的结点了
|
||||||
|
} else {
|
||||||
|
root = inNode.right;// 右结点还没遍历,遍历右结点
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 层序遍历,求结点的高度
|
||||||
|
*/
|
||||||
|
public int getHeight(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Deque<TreeNode> deque = new LinkedList<>();
|
||||||
|
deque.offer(root);
|
||||||
|
int depth = 0;
|
||||||
|
while (!deque.isEmpty()) {
|
||||||
|
int size = deque.size();
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode poll = deque.poll();
|
||||||
|
if (poll.left != null) {
|
||||||
|
deque.offer(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
deque.offer(poll.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 优化迭代法,针对暴力迭代法的getHeight方法做优化,利用TreeNode.val来保存当前结点的高度,这样就不会有重复遍历
|
||||||
|
* 获取高度算法时间复杂度可以降到O(1),总的时间复杂度降为O(n)。
|
||||||
|
* <p>
|
||||||
|
* 时间复杂度:O(n)
|
||||||
|
*/
|
||||||
|
public boolean isBalanced(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
|
TreeNode pre = null;
|
||||||
|
while (root != null || !stack.isEmpty()) {
|
||||||
|
while (root != null) {
|
||||||
|
stack.push(root);
|
||||||
|
root = root.left;
|
||||||
|
}
|
||||||
|
TreeNode inNode = stack.peek();
|
||||||
|
// 右结点为null或已经遍历过
|
||||||
|
if (inNode.right == null || inNode.right == pre) {
|
||||||
|
// 输出
|
||||||
|
if (Math.abs(getHeight(inNode.left) - getHeight(inNode.right)) > 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
stack.pop();
|
||||||
|
pre = inNode;
|
||||||
|
root = null;// 当前结点下,没有要遍历的结点了
|
||||||
|
} else {
|
||||||
|
root = inNode.right;// 右结点还没遍历,遍历右结点
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 求结点的高度
|
||||||
|
*/
|
||||||
|
public int getHeight(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int leftHeight = root.left != null ? root.left.val : 0;
|
||||||
|
int rightHeight = root.right != null ? root.right.val : 0;
|
||||||
|
int height = Math.max(leftHeight, rightHeight) + 1;
|
||||||
|
root.val = height;// 用TreeNode.val来保存当前结点的高度
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// LeetCode题解链接:https://leetcode-cn.com/problems/balanced-binary-tree/solution/110-ping-heng-er-cha-shu-di-gui-fa-bao-l-yqr3/
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -401,4 +541,4 @@ func abs(a int)int{
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -195,6 +195,64 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 递归法,相比求MaxDepth要复杂点
|
||||||
|
* 因为最小深度是从根节点到最近**叶子节点**的最短路径上的节点数量
|
||||||
|
*/
|
||||||
|
public int minDepth(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int leftDepth = minDepth(root.left);
|
||||||
|
int rightDepth = minDepth(root.right);
|
||||||
|
if (root.left == null) {
|
||||||
|
return rightDepth + 1;
|
||||||
|
}
|
||||||
|
if (root.right == null) {
|
||||||
|
return leftDepth + 1;
|
||||||
|
}
|
||||||
|
// 左右结点都不为null
|
||||||
|
return Math.min(leftDepth, rightDepth) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 迭代法,层序遍历
|
||||||
|
*/
|
||||||
|
public int minDepth(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
Deque<TreeNode> deque = new LinkedList<>();
|
||||||
|
deque.offer(root);
|
||||||
|
int depth = 0;
|
||||||
|
while (!deque.isEmpty()) {
|
||||||
|
int size = deque.size();
|
||||||
|
depth++;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode poll = deque.poll();
|
||||||
|
if (poll.left == null && poll.right == null) {
|
||||||
|
// 是叶子结点,直接返回depth,因为从上往下遍历,所以该值就是最小值
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
if (poll.left != null) {
|
||||||
|
deque.offer(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
deque.offer(poll.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
递归法:
|
递归法:
|
||||||
@ -250,4 +308,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
@ -305,7 +305,34 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public boolean hasPathSum(TreeNode root, int targetSum) {
|
||||||
|
if (root == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
targetSum -= root.val;
|
||||||
|
// 叶子结点
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
return targetSum == 0;
|
||||||
|
}
|
||||||
|
if (root.left != null) {
|
||||||
|
boolean left = hasPathSum(root.left, targetSum);
|
||||||
|
if (left) {// 已经找到
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (root.right != null) {
|
||||||
|
boolean right = hasPathSum(root.right, targetSum);
|
||||||
|
if (right) {// 已经找到
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -387,4 +414,4 @@ let pathSum = function (root, targetSum) {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -135,10 +135,33 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int maxProfit(int[] prices) {
|
||||||
|
int sum = 0;
|
||||||
|
int profit = 0;
|
||||||
|
int buy = prices[0];
|
||||||
|
for (int i = 1; i < prices.length; i++) {
|
||||||
|
profit = prices[i] - buy;
|
||||||
|
if (profit > 0) {
|
||||||
|
sum += profit;
|
||||||
|
}
|
||||||
|
buy = prices[i];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxProfit(self, prices: List[int]) -> int:
|
||||||
|
result = 0
|
||||||
|
for i in range(1, len(prices)):
|
||||||
|
result += max(prices[i] - prices[i - 1], 0)
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
@ -149,4 +172,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -199,7 +199,28 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||||
|
int sum = 0;
|
||||||
|
int min = 0;
|
||||||
|
for (int i = 0; i < gas.length; i++) {
|
||||||
|
sum += (gas[i] - cost[i]);
|
||||||
|
min = Math.min(sum, min);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sum < 0) return -1;
|
||||||
|
if (min >= 0) return 0;
|
||||||
|
|
||||||
|
for (int i = gas.length - 1; i > 0; i--) {
|
||||||
|
min += (gas[i] - cost[i]);
|
||||||
|
if (min >= 0) return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -213,4 +234,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -130,7 +130,35 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int candy(int[] ratings) {
|
||||||
|
int[] candy = new int[ratings.length];
|
||||||
|
for (int i = 0; i < candy.length; i++) {
|
||||||
|
candy[i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < ratings.length; i++) {
|
||||||
|
if (ratings[i] > ratings[i - 1]) {
|
||||||
|
candy[i] = candy[i - 1] + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = ratings.length - 2; i >= 0; i--) {
|
||||||
|
if (ratings[i] > ratings[i + 1]) {
|
||||||
|
candy[i] = Math.max(candy[i],candy[i + 1] + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < candy.length; i++) {
|
||||||
|
count += candy[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -144,4 +172,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -232,7 +232,23 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public boolean wordBreak(String s, List<String> wordDict) {
|
||||||
|
boolean[] valid = new boolean[s.length() + 1];
|
||||||
|
valid[0] = true;
|
||||||
|
for (int i = 1; i <= s.length(); i++) {
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
if (wordDict.contains(s.substring(j,i)) && valid[j]) {
|
||||||
|
valid[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return valid[s.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -246,4 +262,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -186,6 +186,29 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
public class Solution {
|
||||||
|
public ListNode detectCycle(ListNode head) {
|
||||||
|
ListNode slow = head;
|
||||||
|
ListNode fast = head;
|
||||||
|
while (fast != null && fast.next != null) {
|
||||||
|
slow = slow.next;
|
||||||
|
fast = fast.next.next;
|
||||||
|
if (slow == fast) {// 有环
|
||||||
|
ListNode index1 = fast;
|
||||||
|
ListNode index2 = head;
|
||||||
|
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
|
||||||
|
while (index1 != index2) {
|
||||||
|
index1 = index1.next;
|
||||||
|
index2 = index2.next;
|
||||||
|
}
|
||||||
|
return index1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
@ -213,6 +213,74 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 不使用Java内置方法实现
|
||||||
|
* <p>
|
||||||
|
* 1.去除首尾以及中间多余空格
|
||||||
|
* 2.反转整个字符串
|
||||||
|
* 3.反转各个单词
|
||||||
|
*/
|
||||||
|
public String reverseWords(String s) {
|
||||||
|
// System.out.println("ReverseWords.reverseWords2() called with: s = [" + s + "]");
|
||||||
|
// 1.去除首尾以及中间多余空格
|
||||||
|
StringBuilder sb = removeSpace(s);
|
||||||
|
// 2.反转整个字符串
|
||||||
|
reverseString(sb, 0, sb.length() - 1);
|
||||||
|
// 3.反转各个单词
|
||||||
|
reverseEachWord(sb);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private StringBuilder removeSpace(String s) {
|
||||||
|
// System.out.println("ReverseWords.removeSpace() called with: s = [" + s + "]");
|
||||||
|
int start = 0;
|
||||||
|
int end = s.length() - 1;
|
||||||
|
while (s.charAt(start) == ' ') start++;
|
||||||
|
while (s.charAt(end) == ' ') end--;
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
while (start <= end) {
|
||||||
|
char c = s.charAt(start);
|
||||||
|
if (c != ' ' || sb.charAt(sb.length() - 1) != ' ') {
|
||||||
|
sb.append(c);
|
||||||
|
}
|
||||||
|
start++;
|
||||||
|
}
|
||||||
|
// System.out.println("ReverseWords.removeSpace returned: sb = [" + sb + "]");
|
||||||
|
return sb;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反转字符串指定区间[start, end]的字符
|
||||||
|
*/
|
||||||
|
public void reverseString(StringBuilder sb, int start, int end) {
|
||||||
|
// System.out.println("ReverseWords.reverseString() called with: sb = [" + sb + "], start = [" + start + "], end = [" + end + "]");
|
||||||
|
while (start < end) {
|
||||||
|
char temp = sb.charAt(start);
|
||||||
|
sb.setCharAt(start, sb.charAt(end));
|
||||||
|
sb.setCharAt(end, temp);
|
||||||
|
start++;
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
// System.out.println("ReverseWords.reverseString returned: sb = [" + sb + "]");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void reverseEachWord(StringBuilder sb) {
|
||||||
|
int start = 0;
|
||||||
|
int end = 1;
|
||||||
|
int n = sb.length();
|
||||||
|
while (start < n) {
|
||||||
|
while (end < n && sb.charAt(end) != ' ') {
|
||||||
|
end++;
|
||||||
|
}
|
||||||
|
reverseString(sb, start, end - 1);
|
||||||
|
start = end + 1;
|
||||||
|
end = start + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -102,7 +102,45 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
// 双指针
|
||||||
|
class Solution {
|
||||||
|
public ListNode reverseList(ListNode head) {
|
||||||
|
ListNode prev = null;
|
||||||
|
ListNode cur = head;
|
||||||
|
ListNode temp = null;
|
||||||
|
while (cur != null) {
|
||||||
|
temp = cur.next;// 保存下一个节点
|
||||||
|
cur.next = prev;
|
||||||
|
prev = cur;
|
||||||
|
cur = temp;
|
||||||
|
}
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 递归
|
||||||
|
class Solution {
|
||||||
|
public ListNode reverseList(ListNode head) {
|
||||||
|
return reverse(null, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ListNode reverse(ListNode prev, ListNode cur) {
|
||||||
|
if (cur == null) {
|
||||||
|
return prev;
|
||||||
|
}
|
||||||
|
ListNode temp = null;
|
||||||
|
temp = cur.next;// 先保存下一个节点
|
||||||
|
cur.next = prev;// 反转
|
||||||
|
// 更新prev、cur位置
|
||||||
|
prev = cur;
|
||||||
|
cur = temp;
|
||||||
|
return reverse(prev, cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -116,4 +154,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -148,7 +148,25 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
// 滑动窗口
|
||||||
|
public int minSubArrayLen(int s, int[] nums) {
|
||||||
|
int left = 0;
|
||||||
|
int sum = 0;
|
||||||
|
int result = Integer.MAX_VALUE;
|
||||||
|
for (int right = 0; right < nums.length; right++) {
|
||||||
|
sum += nums[right];
|
||||||
|
while (sum >= s) {
|
||||||
|
result = Math.min(result, right - left + 1);
|
||||||
|
sum -= nums[left++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result == Integer.MAX_VALUE ? 0 : result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -177,4 +195,4 @@ var minSubArrayLen = (target, nums) => {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -227,7 +227,39 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> res = new ArrayList<>();
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<List<Integer>> combinationSum3(int k, int n) {
|
||||||
|
res.clear();
|
||||||
|
list.clear();
|
||||||
|
backtracking(k, n, 9);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backtracking(int k, int n, int maxNum) {
|
||||||
|
if (k == 0 && n == 0) {
|
||||||
|
res.add(new ArrayList<>(list));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 因为不能重复,并且单个数字最大值是maxNum,所以sum最大值为
|
||||||
|
// (maxNum + (maxNum - 1) + ... + (maxNum - k + 1)) == k * maxNum - k*(k - 1) / 2
|
||||||
|
if (maxNum == 0
|
||||||
|
|| n > k * maxNum - k * (k - 1) / 2
|
||||||
|
|| n < (1 + k) * k / 2) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
list.add(maxNum);
|
||||||
|
backtracking(k - 1, n - maxNum, maxNum - 1);
|
||||||
|
list.remove(list.size() - 1);
|
||||||
|
backtracking(k, n, maxNum - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -241,4 +273,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -194,7 +194,49 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
// 通用递归解法
|
||||||
|
public int countNodes(TreeNode root) {
|
||||||
|
if(root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return countNodes(root.left) + countNodes(root.right) + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 针对完全二叉树的解法
|
||||||
|
*
|
||||||
|
* 满二叉树的结点数为:2^depth - 1
|
||||||
|
*/
|
||||||
|
public int countNodes(TreeNode root) {
|
||||||
|
if(root == null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int leftDepth = getDepth(root.left);
|
||||||
|
int rightDepth = getDepth(root.right);
|
||||||
|
if (leftDepth == rightDepth) {// 左子树是满二叉树
|
||||||
|
// 2^leftDepth其实是 (2^leftDepth - 1) + 1 ,左子树 + 根结点
|
||||||
|
return (1 << leftDepth) + countNodes(root.right);
|
||||||
|
} else {// 右子树是满二叉树
|
||||||
|
return (1 << rightDepth) + countNodes(root.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getDepth(TreeNode root) {
|
||||||
|
int depth = 0;
|
||||||
|
while (root != null) {
|
||||||
|
root = root.left;
|
||||||
|
depth++;
|
||||||
|
}
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -208,4 +250,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -204,6 +204,29 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 前后序遍历都可以
|
||||||
|
* 中序不行,因为先左孩子交换孩子,再根交换孩子(做完后,右孩子已经变成了原来的左孩子),再右孩子交换孩子(此时其实是对原来的左孩子做交换)
|
||||||
|
*/
|
||||||
|
public TreeNode invertTree(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
invertTree(root.left);
|
||||||
|
invertTree(root.right);
|
||||||
|
swapChildren(root);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void swapChildren(TreeNode root) {
|
||||||
|
TreeNode tmp = root.left;
|
||||||
|
root.left = root.right;
|
||||||
|
root.right = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -230,4 +253,4 @@ func invertTree(root *TreeNode) *TreeNode {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -29,7 +29,7 @@
|
|||||||
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
|
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
|
||||||
输出: 2
|
输出: 2
|
||||||
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
|
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。
|
||||||
|
|
||||||
|
|
||||||
说明:
|
说明:
|
||||||
|
|
||||||
@ -229,7 +229,22 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
|
while (true) {
|
||||||
|
if (root.val > p.val && root.val > q.val) {
|
||||||
|
root = root.left;
|
||||||
|
} else if (root.val < p.val && root.val < q.val) {
|
||||||
|
root = root.right;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -243,4 +258,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -224,6 +224,43 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
|
return lowestCommonAncestor1(root, p, q);
|
||||||
|
|
||||||
|
}
|
||||||
|
public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
|
if (root == null || root == p || root == q) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
TreeNode left = lowestCommonAncestor1(root.left, p, q);
|
||||||
|
TreeNode right = lowestCommonAncestor1(root.right, p, q);
|
||||||
|
if (left != null && right != null) {// 左右子树分别找到了,说明此时的root就是要求的结果
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if (left == null) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 代码精简版
|
||||||
|
class Solution {
|
||||||
|
TreeNode pre;
|
||||||
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
|
if (root == null || root.val == p.val ||root.val == q.val) return root;
|
||||||
|
TreeNode left = lowestCommonAncestor(root.left,p,q);
|
||||||
|
TreeNode right = lowestCommonAncestor(root.right,p,q);
|
||||||
|
if (left != null && right != null) return root;
|
||||||
|
else if (left == null && right != null) return right;
|
||||||
|
else if (left != null && right == null) return left;
|
||||||
|
else return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -237,4 +274,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -88,6 +88,7 @@ Java:
|
|||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
public boolean isAnagram(String s, String t) {
|
public boolean isAnagram(String s, String t) {
|
||||||
|
|
||||||
int[] record = new int[26];
|
int[] record = new int[26];
|
||||||
for (char c : s.toCharArray()) {
|
for (char c : s.toCharArray()) {
|
||||||
record[c - 'a'] += 1;
|
record[c - 'a'] += 1;
|
||||||
@ -138,4 +139,4 @@ func isAnagram(s string, t string) bool {
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -280,9 +280,48 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
/**
|
||||||
|
* 递归法
|
||||||
|
*/
|
||||||
|
public List<String> binaryTreePaths(TreeNode root) {
|
||||||
|
List<String> res = new ArrayList<>();
|
||||||
|
if (root == null) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
List<Integer> paths = new ArrayList<>();
|
||||||
|
traversal(root, paths, res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void traversal(TreeNode root, List<Integer> paths, List<String> res) {
|
||||||
|
paths.add(root.val);
|
||||||
|
// 叶子结点
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
// 输出
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < paths.size() - 1; i++) {
|
||||||
|
sb.append(paths.get(i)).append("->");
|
||||||
|
}
|
||||||
|
sb.append(paths.get(paths.size() - 1));
|
||||||
|
res.add(sb.toString());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root.left != null) {
|
||||||
|
traversal(root.left, paths, res);
|
||||||
|
paths.remove(paths.size() - 1);// 回溯
|
||||||
|
}
|
||||||
|
if (root.right != null) {
|
||||||
|
traversal(root.right, paths, res);
|
||||||
|
paths.remove(paths.size() - 1);// 回溯
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -296,4 +335,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -133,6 +133,32 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public int[] topKFrequent(int[] nums, int k) {
|
||||||
|
int[] result = new int[k];
|
||||||
|
HashMap<Integer, Integer> map = new HashMap<>();
|
||||||
|
for (int num : nums) {
|
||||||
|
map.put(num, map.getOrDefault(num, 0) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
|
||||||
|
// 根据map的value值正序排,相当于一个小顶堆
|
||||||
|
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
|
||||||
|
for (Map.Entry<Integer, Integer> entry : entries) {
|
||||||
|
queue.offer(entry);
|
||||||
|
if (queue.size() > k) {
|
||||||
|
queue.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = k - 1; i >= 0; i--) {
|
||||||
|
result[i] = queue.poll().getKey();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
@ -147,6 +147,7 @@ C++测试用例有超过两个树相加超过int的数据,所以需要在if里
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
class Solution {
|
class Solution {
|
||||||
public int combinationSum4(int[] nums, int target) {
|
public int combinationSum4(int[] nums, int target) {
|
||||||
@ -163,10 +164,23 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def combinationSum4(self, nums, target):
|
||||||
|
dp = [0] * (target + 1)
|
||||||
|
dp[0] = 1
|
||||||
|
|
||||||
|
for i in range(1, target+1):
|
||||||
|
for j in nums:
|
||||||
|
if i >= j:
|
||||||
|
dp[i] += dp[i - j]
|
||||||
|
|
||||||
|
return dp[-1]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
@ -177,4 +191,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -204,7 +204,6 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
|
||||||
@ -217,5 +216,6 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
|
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||||
|
|
||||||
|
@ -185,7 +185,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int[][] reconstructQueue(int[][] people) {
|
||||||
|
Arrays.sort(people, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
if (o1[0] != o2[0]) {
|
||||||
|
return Integer.compare(o2[0],o1[0]);
|
||||||
|
} else {
|
||||||
|
return Integer.compare(o1[1],o2[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
LinkedList<int[]> que = new LinkedList<>();
|
||||||
|
|
||||||
|
for (int[] p : people) {
|
||||||
|
que.add(p[1],p);
|
||||||
|
}
|
||||||
|
|
||||||
|
return que.toArray(new int[people.length][]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -199,4 +222,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -182,7 +182,34 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int eraseOverlapIntervals(int[][] intervals) {
|
||||||
|
if (intervals.length < 2) return 0;
|
||||||
|
Arrays.sort(intervals, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
if (o1[0] != o2[0]) {
|
||||||
|
return Integer.compare(o1[1],o2[1]);
|
||||||
|
} else {
|
||||||
|
return Integer.compare(o2[0],o1[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
int edge = intervals[0][1];
|
||||||
|
for (int i = 1; i < intervals.length; i++) {
|
||||||
|
if (intervals[i][0] < edge) {
|
||||||
|
count++;
|
||||||
|
} else {
|
||||||
|
edge = intervals[i][1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -196,4 +223,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -251,7 +251,34 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode deleteNode(TreeNode root, int key) {
|
||||||
|
root = delete(root,key);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private TreeNode delete(TreeNode root, int key) {
|
||||||
|
if (root == null) return null;
|
||||||
|
|
||||||
|
if (root.val > key) {
|
||||||
|
root.left = delete(root.left,key);
|
||||||
|
} else if (root.val < key) {
|
||||||
|
root.right = delete(root.right,key);
|
||||||
|
} else {
|
||||||
|
if (root.left == null) return root.right;
|
||||||
|
if (root.right == null) return root.left;
|
||||||
|
TreeNode tmp = root.right;
|
||||||
|
while (tmp.left != null) {
|
||||||
|
tmp = tmp.left;
|
||||||
|
}
|
||||||
|
root.val = tmp.val;
|
||||||
|
root.right = delete(root.right,tmp.val);
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -303,4 +330,4 @@ func deleteNode1(root *TreeNode)*TreeNode{
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -139,7 +139,32 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int findMinArrowShots(int[][] points) {
|
||||||
|
Arrays.sort(points, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
if (o1[0] != o2[0]) {
|
||||||
|
return Integer.compare(o1[0],o2[0]);
|
||||||
|
} else {
|
||||||
|
return Integer.compare(o1[0],o2[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
int count = 1;
|
||||||
|
for (int i = 1; i < points.length; i++) {
|
||||||
|
if (points[i][0] > points[i - 1][1]) {
|
||||||
|
count++;
|
||||||
|
} else {
|
||||||
|
points[i][1] = Math.min(points[i][1],points[i - 1][1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -153,4 +178,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -30,7 +30,7 @@
|
|||||||
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
|
你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
|
||||||
你拥有的饼干数量和尺寸都足以让所有孩子满足。
|
你拥有的饼干数量和尺寸都足以让所有孩子满足。
|
||||||
所以你应该输出2.
|
所以你应该输出2.
|
||||||
|
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
* 1 <= g.length <= 3 * 10^4
|
* 1 <= g.length <= 3 * 10^4
|
||||||
@ -115,7 +115,23 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int findContentChildren(int[] g, int[] s) {
|
||||||
|
Arrays.sort(g);
|
||||||
|
Arrays.sort(s);
|
||||||
|
int start = 0;
|
||||||
|
int count = 0;
|
||||||
|
for (int i = 0; i < s.length && start < g.length; i++) {
|
||||||
|
if (s[i] >= g[start]) {
|
||||||
|
start++;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -129,4 +145,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -200,6 +200,32 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
private List<Integer> path = new ArrayList<>();
|
||||||
|
private List<List<Integer>> res = new ArrayList<>();
|
||||||
|
public List<List<Integer>> findSubsequences(int[] nums) {
|
||||||
|
backtracking(nums,0);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void backtracking (int[] nums, int start) {
|
||||||
|
if (path.size() > 1) {
|
||||||
|
res.add(new ArrayList<>(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] used = new int[201];
|
||||||
|
for (int i = start; i < nums.length; i++) {
|
||||||
|
if (!path.isEmpty() && nums[i] < path.get(path.size() - 1) ||
|
||||||
|
(used[nums[i] + 100] == 1)) continue;
|
||||||
|
used[nums[i] + 100] = 1;
|
||||||
|
path.add(nums[i]);
|
||||||
|
backtracking(nums, i + 1);
|
||||||
|
path.remove(path.size() - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
@ -214,4 +240,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -241,7 +241,24 @@ dp[j] += dp[j - nums[i]];
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int findTargetSumWays(int[] nums, int target) {
|
||||||
|
int sum = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) sum += nums[i];
|
||||||
|
if ((target + sum) % 2 != 0) return 0;
|
||||||
|
int size = (target + sum) / 2;
|
||||||
|
int[] dp = new int[size + 1];
|
||||||
|
dp[0] = 1;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
for (int j = size; j >= nums[i]; j--) {
|
||||||
|
dp[j] += dp[j - nums[i]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[size];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -255,4 +272,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -345,6 +345,53 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
ArrayList<Integer> resList;
|
||||||
|
int maxCount;
|
||||||
|
int count;
|
||||||
|
TreeNode pre;
|
||||||
|
|
||||||
|
public int[] findMode(TreeNode root) {
|
||||||
|
resList = new ArrayList<>();
|
||||||
|
maxCount = 0;
|
||||||
|
count = 0;
|
||||||
|
pre = null;
|
||||||
|
findMode1(root);
|
||||||
|
int[] res = new int[resList.size()];
|
||||||
|
for (int i = 0; i < resList.size(); i++) {
|
||||||
|
res[i] = resList.get(i);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void findMode1(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
findMode1(root.left);
|
||||||
|
|
||||||
|
int rootValue = root.val;
|
||||||
|
// 计数
|
||||||
|
if (pre == null || rootValue != pre.val) {
|
||||||
|
count = 1;
|
||||||
|
} else {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
// 更新结果以及maxCount
|
||||||
|
if (count > maxCount) {
|
||||||
|
resList.clear();
|
||||||
|
resList.add(rootValue);
|
||||||
|
maxCount = count;
|
||||||
|
} else if (count == maxCount) {
|
||||||
|
resList.add(rootValue);
|
||||||
|
}
|
||||||
|
pre = root;
|
||||||
|
|
||||||
|
findMode1(root.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -358,4 +405,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -218,6 +218,60 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 递归法
|
||||||
|
class Solution {
|
||||||
|
private int Deep = -1;
|
||||||
|
private int value = 0;
|
||||||
|
public int findBottomLeftValue(TreeNode root) {
|
||||||
|
value = root.val;
|
||||||
|
findLeftValue(root,0);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void findLeftValue (TreeNode root,int deep) {
|
||||||
|
if (root == null) return;
|
||||||
|
if (root.left == null && root.right == null) {
|
||||||
|
if (deep > Deep) {
|
||||||
|
value = root.val;
|
||||||
|
Deep = deep;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (root.left != null) findLeftValue(root.left,deep + 1);
|
||||||
|
if (root.right != null) findLeftValue(root.right,deep + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
//迭代法
|
||||||
|
class Solution {
|
||||||
|
|
||||||
|
public int findBottomLeftValue(TreeNode root) {
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
queue.offer(root);
|
||||||
|
int res = 0;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode poll = queue.poll();
|
||||||
|
if (i == 0) {
|
||||||
|
res = poll.val;
|
||||||
|
}
|
||||||
|
if (poll.left != null) {
|
||||||
|
queue.offer(poll.left);
|
||||||
|
}
|
||||||
|
if (poll.right != null) {
|
||||||
|
queue.offer(poll.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -231,4 +285,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -148,6 +148,25 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
public class Solution {
|
||||||
|
public int longestPalindromeSubseq(String s) {
|
||||||
|
int len = s.length();
|
||||||
|
int[][] dp = new int[len + 1][len + 1];
|
||||||
|
for (int i = len - 1; i >= 0; i--) { // 从后往前遍历 保证情况不漏
|
||||||
|
dp[i][i] = 1; // 初始化
|
||||||
|
for (int j = i + 1; j < len; j++) {
|
||||||
|
if (s.charAt(i) == s.charAt(j)) {
|
||||||
|
dp[i][j] = dp[i + 1][j - 1] + 2;
|
||||||
|
} else {
|
||||||
|
dp[i][j] = Math.max(dp[i + 1][j], Math.max(dp[i][j], dp[i][j - 1]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[0][len - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
@ -152,6 +152,29 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
TreeNode pre;// 记录上一个遍历的结点
|
||||||
|
int result = Integer.MAX_VALUE;
|
||||||
|
public int getMinimumDifference(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
// 左
|
||||||
|
int left = getMinimumDifference(root.left);
|
||||||
|
|
||||||
|
// 中
|
||||||
|
if (pre != null) {
|
||||||
|
result = Math.min(left, root.val - pre.val);
|
||||||
|
}
|
||||||
|
pre = root;
|
||||||
|
// 右
|
||||||
|
int right = getMinimumDifference(root.right);
|
||||||
|
result = Math.min(right, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -165,4 +188,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -173,7 +173,27 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
int sum;
|
||||||
|
public TreeNode convertBST(TreeNode root) {
|
||||||
|
sum = 0;
|
||||||
|
convertBST1(root);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按右中左顺序遍历,累加即可
|
||||||
|
public void convertBST1(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
convertBST1(root.right);
|
||||||
|
sum += root.val;
|
||||||
|
root.val = sum;
|
||||||
|
convertBST1(root.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -187,4 +207,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -257,6 +257,59 @@ public:
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
// 递归
|
||||||
|
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
|
||||||
|
if (root1 == null) return root2;
|
||||||
|
if (root2 == null) return root1;
|
||||||
|
|
||||||
|
TreeNode newRoot = new TreeNode(root1.val + root2.val);
|
||||||
|
newRoot.left = mergeTrees(root1.left,root2.left);
|
||||||
|
newRoot.right = mergeTrees(root1.right,root2.right);
|
||||||
|
return newRoot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
// 迭代
|
||||||
|
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
|
||||||
|
if (root1 == null) {
|
||||||
|
return root2;
|
||||||
|
}
|
||||||
|
if (root2 == null) {
|
||||||
|
return root1;
|
||||||
|
}
|
||||||
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
|
stack.push(root2);
|
||||||
|
stack.push(root1);
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
TreeNode node1 = stack.pop();
|
||||||
|
TreeNode node2 = stack.pop();
|
||||||
|
node1.val += node2.val;
|
||||||
|
if (node2.right != null && node1.right != null) {
|
||||||
|
stack.push(node2.right);
|
||||||
|
stack.push(node1.right);
|
||||||
|
} else {
|
||||||
|
if (node1.right == null) {
|
||||||
|
node1.right = node2.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (node2.left != null && node1.left != null) {
|
||||||
|
stack.push(node2.left);
|
||||||
|
stack.push(node1.left);
|
||||||
|
} else {
|
||||||
|
if (node1.left == null) {
|
||||||
|
node1.left = node2.left;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return root1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -270,4 +323,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -225,6 +225,35 @@ root->right = traversal(nums, maxValueIndex + 1, right);
|
|||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode constructMaximumBinaryTree(int[] nums) {
|
||||||
|
return constructMaximumBinaryTree1(nums, 0, nums.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode constructMaximumBinaryTree1(int[] nums, int leftIndex, int rightIndex) {
|
||||||
|
if (rightIndex - leftIndex < 1) {// 没有元素了
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (rightIndex - leftIndex == 1) {// 只有一个元素
|
||||||
|
return new TreeNode(nums[leftIndex]);
|
||||||
|
}
|
||||||
|
int maxIndex = leftIndex;// 最大值所在位置
|
||||||
|
int maxVal = nums[maxIndex];// 最大值
|
||||||
|
for (int i = leftIndex + 1; i < rightIndex; i++) {
|
||||||
|
if (nums[i] > maxVal){
|
||||||
|
maxVal = nums[i];
|
||||||
|
maxIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TreeNode root = new TreeNode(maxVal);
|
||||||
|
// 根据maxIndex划分左右子树
|
||||||
|
root.left = constructMaximumBinaryTree1(nums, leftIndex, maxIndex);
|
||||||
|
root.right = constructMaximumBinaryTree1(nums, maxIndex + 1, rightIndex);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -238,4 +267,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -242,7 +242,30 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode trimBST(TreeNode root, int low, int high) {
|
||||||
|
root = trim(root,low,high);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TreeNode trim(TreeNode root,int low, int high) {
|
||||||
|
if (root == null ) return null;
|
||||||
|
if (root.val < low) {
|
||||||
|
return trim(root.right,low,high);
|
||||||
|
}
|
||||||
|
if (root.val > high) {
|
||||||
|
return trim(root.left,low,high);
|
||||||
|
}
|
||||||
|
|
||||||
|
root.left = trim(root.left,low,high);
|
||||||
|
root.right = trim(root.right,low,high);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -256,4 +279,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -140,12 +140,99 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
// 递归,普通二叉树
|
||||||
|
public TreeNode searchBST(TreeNode root, int val) {
|
||||||
|
if (root == null || root.val == val) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
TreeNode left = searchBST(root.left, val);
|
||||||
|
if (left != null) {
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
return searchBST(root.right, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
// 递归,利用二叉搜索树特点,优化
|
||||||
|
public TreeNode searchBST(TreeNode root, int val) {
|
||||||
|
if (root == null || root.val == val) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
if (val < root.val) {
|
||||||
|
return searchBST(root.left, val);
|
||||||
|
} else {
|
||||||
|
return searchBST(root.right, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
// 迭代,普通二叉树
|
||||||
|
public TreeNode searchBST(TreeNode root, int val) {
|
||||||
|
if (root == null || root.val == val) {
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
Stack<TreeNode> stack = new Stack<>();
|
||||||
|
stack.push(root);
|
||||||
|
while (!stack.isEmpty()) {
|
||||||
|
TreeNode pop = stack.pop();
|
||||||
|
if (pop.val == val) {
|
||||||
|
return pop;
|
||||||
|
}
|
||||||
|
if (pop.right != null) {
|
||||||
|
stack.push(pop.right);
|
||||||
|
}
|
||||||
|
if (pop.left != null) {
|
||||||
|
stack.push(pop.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
// 迭代,利用二叉搜索树特点,优化,可以不需要栈
|
||||||
|
public TreeNode searchBST(TreeNode root, int val) {
|
||||||
|
while (root != null)
|
||||||
|
if (val < root.val) root = root.left;
|
||||||
|
else if (val > root.val) root = root.right;
|
||||||
|
else return root;
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
递归法:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||||
|
if root is None:
|
||||||
|
return None
|
||||||
|
if val < root.val: return self.searchBST(root.left, val)
|
||||||
|
elif val > root.val: return self.searchBST(root.right, val)
|
||||||
|
else: return root
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||||
|
while root is not None:
|
||||||
|
if val < root.val: root = root.left
|
||||||
|
elif val > root.val: root = root.right
|
||||||
|
else: return root
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
@ -156,4 +243,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -16,7 +16,7 @@
|
|||||||
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。
|
注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
|
|
||||||
* 给定的树上的节点数介于 0 和 10^4 之间
|
* 给定的树上的节点数介于 0 和 10^4 之间
|
||||||
@ -206,12 +206,69 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode insertIntoBST(TreeNode root, int val) {
|
||||||
|
if (root == null) return new TreeNode(val);
|
||||||
|
TreeNode newRoot = root;
|
||||||
|
TreeNode pre = root;
|
||||||
|
while (root != null) {
|
||||||
|
pre = root;
|
||||||
|
if (root.val > val) {
|
||||||
|
root = root.left;
|
||||||
|
} else if (root.val < val) {
|
||||||
|
root = root.right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pre.val > val) {
|
||||||
|
pre.left = new TreeNode(val);
|
||||||
|
} else {
|
||||||
|
pre.right = new TreeNode(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newRoot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
递归法
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public TreeNode insertIntoBST(TreeNode root, int val) {
|
||||||
|
return buildTree(root, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode buildTree(TreeNode root, int val){
|
||||||
|
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
|
||||||
|
return new TreeNode(val);
|
||||||
|
if (root.val < val){
|
||||||
|
root.right = buildTree(root.right, val); // 递归创建右子树
|
||||||
|
}else if (root.val > val){
|
||||||
|
root.left = buildTree(root.left, val); // 递归创建左子树
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
递归法
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
||||||
|
if root is None:
|
||||||
|
return TreeNode(val) # 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
|
||||||
|
if root.val < val:
|
||||||
|
root.right = self.insertIntoBST(root.right, val) # 递归创建右子树
|
||||||
|
if root.val > val:
|
||||||
|
root.left = self.insertIntoBST(root.left, val) # 递归创建左子树
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
@ -222,4 +279,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -23,7 +23,7 @@
|
|||||||
输入: nums = [-1,0,3,5,9,12], target = 2
|
输入: nums = [-1,0,3,5,9,12], target = 2
|
||||||
输出: -1
|
输出: -1
|
||||||
解释: 2 不存在 nums 中因此返回 -1
|
解释: 2 不存在 nums 中因此返回 -1
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
|
|
||||||
* 你可以假设 nums 中的所有元素是不重复的。
|
* 你可以假设 nums 中的所有元素是不重复的。
|
||||||
@ -146,11 +146,50 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
(版本一)左闭右闭区间
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int search(int[] nums, int target) {
|
||||||
|
int left = 0, right = nums.length - 1;
|
||||||
|
while (left <= right) {
|
||||||
|
int mid = left + ((right - left) >> 1);
|
||||||
|
if (nums[mid] == target)
|
||||||
|
return mid;
|
||||||
|
else if (nums[mid] < target)
|
||||||
|
left = mid + 1;
|
||||||
|
else if (nums[mid] > target)
|
||||||
|
right = mid - 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
(版本二)左闭右开区间
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int search(int[] nums, int target) {
|
||||||
|
int left = 0, right = nums.length;
|
||||||
|
while (left < right) {
|
||||||
|
int mid = left + ((right - left) >> 1);
|
||||||
|
if (nums[mid] == target)
|
||||||
|
return mid;
|
||||||
|
else if (nums[mid] < target)
|
||||||
|
left = mid + 1;
|
||||||
|
else if (nums[mid] > target)
|
||||||
|
right = mid;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
def search(self, nums: List[int], target: int) -> int:
|
def search(self, nums: List[int], target: int) -> int:
|
||||||
@ -178,4 +217,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -156,7 +156,23 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int maxProfit(int[] prices, int fee) {
|
||||||
|
int buy = prices[0] + fee;
|
||||||
|
int sum = 0;
|
||||||
|
for (int p : prices) {
|
||||||
|
if (p + fee < buy) {
|
||||||
|
buy = p + fee;
|
||||||
|
} else if (p > buy){
|
||||||
|
sum += p - buy;
|
||||||
|
buy = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -125,6 +125,24 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int monotoneIncreasingDigits(int N) {
|
||||||
|
String[] strings = (N + "").split("");
|
||||||
|
int start = strings.length;
|
||||||
|
for (int i = strings.length - 1; i > 0; i--) {
|
||||||
|
if (Integer.parseInt(strings[i]) < Integer.parseInt(strings[i - 1])) {
|
||||||
|
strings[i - 1] = (Integer.parseInt(strings[i - 1]) - 1) + "";
|
||||||
|
start = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = start; i < strings.length; i++) {
|
||||||
|
strings[i] = "9";
|
||||||
|
}
|
||||||
|
return Integer.parseInt(String.join("",strings));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
@ -139,4 +157,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -84,7 +84,28 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public List<Integer> partitionLabels(String S) {
|
||||||
|
List<Integer> list = new LinkedList<>();
|
||||||
|
int[] edge = new int[123];
|
||||||
|
char[] chars = S.toCharArray();
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
edge[chars[i] - 0] = i;
|
||||||
|
}
|
||||||
|
int idx = 0;
|
||||||
|
int last = -1;
|
||||||
|
for (int i = 0; i < chars.length; i++) {
|
||||||
|
idx = Math.max(idx,edge[chars[i] - 0]);
|
||||||
|
if (i == idx) {
|
||||||
|
list.add(i - last);
|
||||||
|
last = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
@ -127,7 +127,33 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public boolean lemonadeChange(int[] bills) {
|
||||||
|
int cash_5 = 0;
|
||||||
|
int cash_10 = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < bills.length; i++) {
|
||||||
|
if (bills[i] == 5) {
|
||||||
|
cash_5++;
|
||||||
|
} else if (bills[i] == 10) {
|
||||||
|
cash_5--;
|
||||||
|
cash_10++;
|
||||||
|
} else if (bills[i] == 20) {
|
||||||
|
if (cash_10 > 0) {
|
||||||
|
cash_10--;
|
||||||
|
cash_5--;
|
||||||
|
} else {
|
||||||
|
cash_5 -= 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cash_5 < 0 || cash_10 < 0) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -141,4 +167,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -316,6 +316,33 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
private int count = 0;
|
||||||
|
public int minCameraCover(TreeNode root) {
|
||||||
|
if (trval(root) == 0) count++;
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int trval(TreeNode root) {
|
||||||
|
if (root == null) return -1;
|
||||||
|
|
||||||
|
int left = trval(root.left);
|
||||||
|
int right = trval(root.right);
|
||||||
|
|
||||||
|
if (left == 0 || right == 0) {
|
||||||
|
count++;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (left == 2 || right == 2) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
@ -330,4 +357,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -29,7 +29,7 @@
|
|||||||
输入:A = [2,-3,-1,5,-4], K = 2
|
输入:A = [2,-3,-1,5,-4], K = 2
|
||||||
输出:13
|
输出:13
|
||||||
解释:选择索引 (1, 4) ,然后 A 变为 [2,3,-1,5,4]。
|
解释:选择索引 (1, 4) ,然后 A 变为 [2,3,-1,5,4]。
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
|
|
||||||
* 1 <= A.length <= 10000
|
* 1 <= A.length <= 10000
|
||||||
@ -99,7 +99,29 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int largestSumAfterKNegations(int[] A, int K) {
|
||||||
|
if (A.length == 1) return A[0];
|
||||||
|
Arrays.sort(A);
|
||||||
|
int sum = 0;
|
||||||
|
int idx = 0;
|
||||||
|
for (int i = 0; i < K; i++) {
|
||||||
|
if (i < A.length - 1 && A[idx] < 0) {
|
||||||
|
A[idx] = -A[idx];
|
||||||
|
if (A[idx] >= Math.abs(A[idx + 1])) idx++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
A[idx] = -A[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < A.length; i++) {
|
||||||
|
sum += A[i];
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
@ -113,4 +135,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
@ -31,7 +31,7 @@
|
|||||||
输入:text1 = "abc", text2 = "def"
|
输入:text1 = "abc", text2 = "def"
|
||||||
输出:0
|
输出:0
|
||||||
解释:两个字符串没有公共子序列,返回 0。
|
解释:两个字符串没有公共子序列,返回 0。
|
||||||
|
|
||||||
提示:
|
提示:
|
||||||
* 1 <= text1.length <= 1000
|
* 1 <= text1.length <= 1000
|
||||||
* 1 <= text2.length <= 1000
|
* 1 <= text2.length <= 1000
|
||||||
@ -126,12 +126,44 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
public int longestCommonSubsequence(String text1, String text2) {
|
||||||
|
int[][] dp = new int[text1.length() + 1][text2.length() + 1]; // 先对dp数组做初始化操作
|
||||||
|
for (int i = 1 ; i <= text1.length() ; i++) {
|
||||||
|
char char1 = text1.charAt(i - 1);
|
||||||
|
for (int j = 1; j <= text2.length(); j++) {
|
||||||
|
char char2 = text2.charAt(j - 1);
|
||||||
|
if (char1 == char2) { // 开始列出状态转移方程
|
||||||
|
dp[i][j] = dp[i - 1][j - 1] + 1;
|
||||||
|
} else {
|
||||||
|
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[text1.length()][text2.length()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
|
||||||
|
len1, len2 = len(text1)+1, len(text2)+1
|
||||||
|
dp = [[0 for _ in range(len1)] for _ in range(len2)] # 先对dp数组做初始化操作
|
||||||
|
for i in range(1, len2):
|
||||||
|
for j in range(1, len1): # 开始列出状态转移方程
|
||||||
|
if text1[j-1] == text2[i-1]:
|
||||||
|
dp[i][j] = dp[i-1][j-1]+1
|
||||||
|
else:
|
||||||
|
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
|
||||||
|
return dp[-1][-1]
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
@ -142,4 +174,4 @@ Go:
|
|||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
Reference in New Issue
Block a user