mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' of https://github.com/kok-s0s/leetcode-master
This commit is contained in:
@ -240,6 +240,25 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```go
|
||||
func canCompleteCircuit(gas []int, cost []int) int {
|
||||
curSum := 0
|
||||
totalSum := 0
|
||||
start := 0
|
||||
for i := 0; i < len(gas); i++ {
|
||||
curSum += gas[i] - cost[i]
|
||||
totalSum += gas[i] - cost[i]
|
||||
if curSum < 0 {
|
||||
start = i+1
|
||||
curSum = 0
|
||||
}
|
||||
}
|
||||
if totalSum < 0 {
|
||||
return -1
|
||||
}
|
||||
return start
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
|
@ -109,7 +109,7 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
时间复杂度:$O(n)$
|
||||
时间复杂度:$O(n)$
|
||||
空间复杂度:$O(1)$
|
||||
|
||||
**一些录友会疑惑为什么时间复杂度是O(n)**。
|
||||
@ -118,8 +118,8 @@ public:
|
||||
|
||||
## 相关题目推荐
|
||||
|
||||
* 904.水果成篮
|
||||
* 76.最小覆盖子串
|
||||
* [904.水果成篮](https://leetcode-cn.com/problems/fruit-into-baskets/)
|
||||
* [76.最小覆盖子串](https://leetcode-cn.com/problems/minimum-window-substring/)
|
||||
|
||||
|
||||
|
||||
|
@ -265,6 +265,54 @@ class Solution:
|
||||
else: return root
|
||||
```
|
||||
Go:
|
||||
> BSL法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//利用BSL的性质(前序遍历有序)
|
||||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
if root==nil{return nil}
|
||||
if root.Val>p.Val&&root.Val>q.Val{//当前节点的值大于给定的值,则说明满足条件的在左边
|
||||
return lowestCommonAncestor(root.Left,p,q)
|
||||
}else if root.Val<p.Val&&root.Val<q.Val{//当前节点的值小于各点的值,则说明满足条件的在右边
|
||||
return lowestCommonAncestor(root.Right,p,q)
|
||||
}else {return root}//当前节点的值在给定值的中间(或者等于),即为最深的祖先
|
||||
}
|
||||
```
|
||||
|
||||
> 普通法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//递归会将值层层返回
|
||||
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
|
||||
//终止条件
|
||||
if root==nil||root.Val==p.Val||root.Val==q.Val{return root}//最后为空或者找到一个值时,就返回这个值
|
||||
//后序遍历
|
||||
findLeft:=lowestCommonAncestor(root.Left,p,q)
|
||||
findRight:=lowestCommonAncestor(root.Right,p,q)
|
||||
//处理单层逻辑
|
||||
if findLeft!=nil&&findRight!=nil{return root}//说明在root节点的两边
|
||||
if findLeft==nil{//左边没找到,就说明在右边找到了
|
||||
return findRight
|
||||
}else {return findLeft}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -228,7 +228,27 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
var eraseOverlapIntervals = function(intervals) {
|
||||
intervals.sort((a, b) => {
|
||||
return a[1] - b[1]
|
||||
})
|
||||
|
||||
let count = 1
|
||||
let end = intervals[0][1]
|
||||
|
||||
for(let i = 1; i < intervals.length; i++) {
|
||||
let interval = intervals[i]
|
||||
if(interval[0] >= right) {
|
||||
end = interval[1]
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
||||
return intervals.length - count
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -428,7 +428,100 @@ class Solution:
|
||||
return self.res
|
||||
```
|
||||
Go:
|
||||
暴力法(非BSL)
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func findMode(root *TreeNode) []int {
|
||||
var history map[int]int
|
||||
var maxValue int
|
||||
var maxIndex int
|
||||
var result []int
|
||||
history=make(map[int]int)
|
||||
traversal(root,history)
|
||||
for k,value:=range history{
|
||||
if value>maxValue{
|
||||
maxValue=value
|
||||
maxIndex=k
|
||||
}
|
||||
}
|
||||
for k,value:=range history{
|
||||
if value==history[maxIndex]{
|
||||
result=append(result,k)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
func traversal(root *TreeNode,history map[int]int){
|
||||
if root.Left!=nil{
|
||||
traversal(root.Left,history)
|
||||
}
|
||||
if value,ok:=history[root.Val];ok{
|
||||
history[root.Val]=value+1
|
||||
}else{
|
||||
history[root.Val]=1
|
||||
}
|
||||
if root.Right!=nil{
|
||||
traversal(root.Right,history)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
计数法BSL(此代码在执行代码里能执行,但提交后报错,不知为何,思路是对的)
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
var count,maxCount int //统计计数
|
||||
func findMode(root *TreeNode) []int {
|
||||
var result []int
|
||||
var pre *TreeNode //前指针
|
||||
if root.Left==nil&&root.Right==nil{
|
||||
result=append(result,root.Val)
|
||||
return result
|
||||
}
|
||||
traversal(root,&result,pre)
|
||||
return result
|
||||
}
|
||||
func traversal(root *TreeNode,result *[]int,pre *TreeNode){//遍历统计
|
||||
//如果BSL中序遍历相邻的两个节点值相同,则统计频率;如果不相同,依据BSL中序遍历排好序的性质,重新计数
|
||||
if pre==nil{
|
||||
count=1
|
||||
}else if pre.Val==root.Val{
|
||||
count++
|
||||
}else {
|
||||
count=1
|
||||
}
|
||||
//如果统计的频率等于最大频率,则加入结果集;如果统计的频率大于最大频率,更新最大频率且重新将结果加入新的结果集中
|
||||
if count==maxCount{
|
||||
*result=append(*result,root.Val)
|
||||
}else if count>maxCount{
|
||||
maxCount=count//重新赋值maxCount
|
||||
*result=[]int{}//清空result中的内容
|
||||
*result=append(*result,root.Val)
|
||||
}
|
||||
pre=root//保存上一个的节点
|
||||
if root.Left!=nil{
|
||||
traversal(root.Left,result,pre)
|
||||
}
|
||||
if root.Right!=nil{
|
||||
traversal(root.Right,result,pre)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -128,7 +128,26 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
var partitionLabels = function(s) {
|
||||
let hash = {}
|
||||
for(let i = 0; i < s.length; i++) {
|
||||
hash[s[i]] = i
|
||||
}
|
||||
let result = []
|
||||
let left = 0
|
||||
let right = 0
|
||||
for(let i = 0; i < s.length; i++) {
|
||||
right = Math.max(right, hash[s[i]])
|
||||
if(right === i) {
|
||||
result.push(right - left + 1)
|
||||
left = i + 1
|
||||
}
|
||||
}
|
||||
return result
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -138,6 +138,30 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```Go
|
||||
func largestSumAfterKNegations(nums []int, K int) int {
|
||||
sort.Slice(nums, func(i, j int) bool {
|
||||
return math.Abs(float64(nums[i])) > math.Abs(float64(nums[j]))
|
||||
})
|
||||
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if K > 0 && nums[i] < 0 {
|
||||
nums[i] = -nums[i]
|
||||
K--
|
||||
}
|
||||
}
|
||||
|
||||
if K%2 == 1 {
|
||||
nums[len(nums)-1] = -nums[len(nums)-1]
|
||||
}
|
||||
|
||||
result := 0
|
||||
for i := 0; i < len(nums); i++ {
|
||||
result += nums[i]
|
||||
}
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Javascript:
|
||||
|
@ -221,12 +221,12 @@ class Solution:
|
||||
Go:
|
||||
|
||||
前序遍历:
|
||||
```
|
||||
```go
|
||||
func PreorderTraversal(root *TreeNode) (res []int) {
|
||||
var traversal func(node *TreeNode)
|
||||
traversal = func(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
return
|
||||
}
|
||||
res = append(res,node.Val)
|
||||
traversal(node.Left)
|
||||
@ -239,12 +239,12 @@ func PreorderTraversal(root *TreeNode) (res []int) {
|
||||
```
|
||||
中序遍历:
|
||||
|
||||
```
|
||||
```go
|
||||
func InorderTraversal(root *TreeNode) (res []int) {
|
||||
var traversal func(node *TreeNode)
|
||||
traversal = func(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
return
|
||||
}
|
||||
traversal(node.Left)
|
||||
res = append(res,node.Val)
|
||||
@ -256,12 +256,12 @@ func InorderTraversal(root *TreeNode) (res []int) {
|
||||
```
|
||||
后序遍历:
|
||||
|
||||
```
|
||||
```go
|
||||
func PostorderTraversal(root *TreeNode) (res []int) {
|
||||
var traversal func(node *TreeNode)
|
||||
traversal = func(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
return
|
||||
}
|
||||
traversal(node.Left)
|
||||
traversal(node.Right)
|
||||
|
@ -5,6 +5,7 @@
|
||||
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
|
||||
</p>
|
||||
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
# 动态规划:关于01背包问题,你该了解这些!(滚动数组)
|
||||
|
||||
昨天[动态规划:关于01背包问题,你该了解这些!](https://mp.weixin.qq.com/s/FwIiPPmR18_AJO5eiidT6w)中是用二维dp数组来讲解01背包。
|
||||
@ -35,7 +36,7 @@
|
||||
|
||||
**其实可以发现如果把dp[i - 1]那一层拷贝到dp[i]上,表达式完全可以是:dp[i][j] = max(dp[i][j], dp[i][j - weight[i]] + value[i]);**
|
||||
|
||||
**于其把dp[i - 1]这一层拷贝到dp[i]上,不如只用一个一维数组了**,只用dp[j](一维数组,也可以理解是一个滚动数组)。
|
||||
**与其把dp[i - 1]这一层拷贝到dp[i]上,不如只用一个一维数组了**,只用dp[j](一维数组,也可以理解是一个滚动数组)。
|
||||
|
||||
这就是滚动数组的由来,需要满足的条件是上一层可以重复利用,直接拷贝到当前层。
|
||||
|
||||
@ -214,7 +215,7 @@ int main() {
|
||||
Java:
|
||||
|
||||
```java
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
int[] weight = {1, 3, 4};
|
||||
int[] value = {15, 20, 30};
|
||||
int bagWight = 4;
|
||||
@ -242,7 +243,24 @@ Java:
|
||||
|
||||
|
||||
Python:
|
||||
```python
|
||||
def test_1_wei_bag_problem():
|
||||
weight = [1, 3, 4]
|
||||
value = [15, 20, 30]
|
||||
bag_weight = 4
|
||||
# 初始化: 全为0
|
||||
dp = [0] * (bag_weight + 1)
|
||||
|
||||
# 先遍历物品, 再遍历背包容量
|
||||
for i in range(len(weight)):
|
||||
for j in range(bag_weight, weight[i] - 1, -1):
|
||||
# 递归公式
|
||||
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
|
||||
|
||||
print(dp)
|
||||
|
||||
test_1_wei_bag_problem()
|
||||
```
|
||||
|
||||
Go:
|
||||
```go
|
||||
|
Reference in New Issue
Block a user