mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -109,6 +109,9 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n)
|
||||||
|
* 空间复杂度: O(n)
|
||||||
|
|
||||||
## 总结
|
## 总结
|
||||||
|
|
||||||
本题其实有四个重点:
|
本题其实有四个重点:
|
||||||
|
@ -83,6 +83,10 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n^2)
|
||||||
|
* 空间复杂度: O(n),额外的 set 开销
|
||||||
|
|
||||||
|
|
||||||
## 双指针
|
## 双指针
|
||||||
|
|
||||||
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
|
**其实这道题目使用哈希法并不十分合适**,因为在去重的操作中有很多细节需要注意,在面试中很难直接写出没有bug的代码。
|
||||||
@ -158,6 +162,10 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n^2)
|
||||||
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
|
|
||||||
## 去重逻辑的思考
|
## 去重逻辑的思考
|
||||||
|
|
||||||
### a的去重
|
### a的去重
|
||||||
|
@ -121,6 +121,10 @@ public:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n^3)
|
||||||
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
|
|
||||||
## 补充
|
## 补充
|
||||||
|
|
||||||
二级剪枝的部分:
|
二级剪枝的部分:
|
||||||
|
@ -178,16 +178,16 @@ var canJump = function(nums) {
|
|||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
fn max(a: usize, b: usize) -> usize {
|
|
||||||
if a > b { a } else { b }
|
|
||||||
}
|
|
||||||
pub fn can_jump(nums: Vec<i32>) -> bool {
|
pub fn can_jump(nums: Vec<i32>) -> bool {
|
||||||
let mut cover = 0;
|
if nums.len() == 1 {
|
||||||
if (nums.len() == 1) { return true; }
|
return true;
|
||||||
let mut i = 0;
|
}
|
||||||
|
let (mut i, mut cover) = (0, 0);
|
||||||
while i <= cover {
|
while i <= cover {
|
||||||
cover = Self::max(i + nums[i] as usize, cover);
|
cover = (i + nums[i] as usize).max(cover);
|
||||||
if cover >= nums.len() - 1 { return true; }
|
if cover >= nums.len() - 1 {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
|
@ -128,12 +128,12 @@ Java:
|
|||||||
class Solution {
|
class Solution {
|
||||||
public int climbStairs(int n) {
|
public int climbStairs(int n) {
|
||||||
int[] dp = new int[n + 1];
|
int[] dp = new int[n + 1];
|
||||||
int[] weight = {1,2};
|
int m = 2;
|
||||||
dp[0] = 1;
|
dp[0] = 1;
|
||||||
|
|
||||||
for (int i = 0; i <= n; i++) {
|
for (int i = 1; i <= n; i++) { // 遍历背包
|
||||||
for (int j = 0; j < weight.length; j++) {
|
for (int j = 1; j <= m; j++) { //遍历物品
|
||||||
if (i >= weight[j]) dp[i] += dp[i - weight[j]];
|
if (i >= j) dp[i] += dp[i - j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,3 +227,4 @@ function climbStairs(n: number): number {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -275,6 +275,7 @@ const maxProfit = (prices) => {
|
|||||||
|
|
||||||
### TypeScript:
|
### TypeScript:
|
||||||
|
|
||||||
|
贪心
|
||||||
```typescript
|
```typescript
|
||||||
function maxProfit(prices: number[]): number {
|
function maxProfit(prices: number[]): number {
|
||||||
let resProfit: number = 0;
|
let resProfit: number = 0;
|
||||||
@ -285,6 +286,21 @@ function maxProfit(prices: number[]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
动态规划
|
||||||
|
```typescript
|
||||||
|
function maxProfit(prices: number[]): number {
|
||||||
|
const dp = Array(prices.length)
|
||||||
|
.fill(0)
|
||||||
|
.map(() => Array(2).fill(0))
|
||||||
|
dp[0][0] = -prices[0]
|
||||||
|
for (let i = 1; i < prices.length; i++) {
|
||||||
|
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i])
|
||||||
|
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i])
|
||||||
|
}
|
||||||
|
return dp[prices.length - 1][1]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
贪心:
|
贪心:
|
||||||
|
@ -516,6 +516,48 @@ class Solution:
|
|||||||
return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions,
|
return s[:ps] + s[ps:][::-1] # Must do the last step, because the last word is omit though the pointers are on the correct positions,
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution: # 使用双指针法移除空格
|
||||||
|
def reverseWords(self, s: str) -> str:
|
||||||
|
|
||||||
|
def removeextraspace(s):
|
||||||
|
start = 0; end = len(s)-1
|
||||||
|
while s[start]==' ':
|
||||||
|
start+=1
|
||||||
|
while s[end]==' ':
|
||||||
|
end-=1
|
||||||
|
news = list(s[start:end+1])
|
||||||
|
slow = fast = 0
|
||||||
|
while fast<len(news):
|
||||||
|
while fast>0 and news[fast]==news[fast-1]==' ':
|
||||||
|
fast+=1
|
||||||
|
news[slow]=news[fast]
|
||||||
|
slow+=1; fast+=1
|
||||||
|
#return "".join(news[:slow])
|
||||||
|
return news[:slow]
|
||||||
|
|
||||||
|
def reversestr(s):
|
||||||
|
left,right = 0,len(s)-1
|
||||||
|
news = list(s)
|
||||||
|
while left<right:
|
||||||
|
news[left],news[right] = news[right],news[left]
|
||||||
|
left+=1; right-=1
|
||||||
|
#return "".join(news)
|
||||||
|
return news
|
||||||
|
|
||||||
|
news = removeextraspace(s)
|
||||||
|
news.append(' ')
|
||||||
|
fast=slow=0
|
||||||
|
#print(news)
|
||||||
|
while fast<len(news):
|
||||||
|
while news[fast]!=' ':
|
||||||
|
fast+=1
|
||||||
|
news[slow:fast] = reversestr(news[slow:fast])
|
||||||
|
# print(news[slow:fast])
|
||||||
|
fast=slow=fast+1
|
||||||
|
news2 = reversestr(news[:-1])
|
||||||
|
return ''.join(news2)
|
||||||
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -75,6 +75,8 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(logn)
|
||||||
|
* 空间复杂度: O(logn)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,6 +85,9 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n)
|
||||||
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
@ -72,6 +72,9 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(mn)
|
||||||
|
* 空间复杂度: O(n)
|
||||||
|
|
||||||
## 拓展
|
## 拓展
|
||||||
|
|
||||||
那有同学可能问了,遇到哈希问题我直接都用set不就得了,用什么数组啊。
|
那有同学可能问了,遇到哈希问题我直接都用set不就得了,用什么数组啊。
|
||||||
@ -110,6 +113,8 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(m + n)
|
||||||
|
* 空间复杂度: O(n)
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
|
@ -39,8 +39,6 @@ canConstruct("aa", "aab") -> true
|
|||||||
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
那么第一个思路其实就是暴力枚举了,两层for循环,不断去寻找,代码如下:
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
// 时间复杂度: O(n^2)
|
|
||||||
// 空间复杂度:O(1)
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool canConstruct(string ransomNote, string magazine) {
|
bool canConstruct(string ransomNote, string magazine) {
|
||||||
@ -62,6 +60,9 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n^2)
|
||||||
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
这里时间复杂度是比较高的,而且里面还有一个字符串删除也就是erase的操作,也是费时的,当然这段代码也可以过这道题。
|
||||||
|
|
||||||
|
|
||||||
@ -78,8 +79,6 @@ public:
|
|||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
```CPP
|
```CPP
|
||||||
// 时间复杂度: O(n)
|
|
||||||
// 空间复杂度:O(1)
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool canConstruct(string ransomNote, string magazine) {
|
bool canConstruct(string ransomNote, string magazine) {
|
||||||
@ -105,6 +104,10 @@ public:
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n)
|
||||||
|
* 空间复杂度: O(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
@ -83,6 +83,9 @@ public:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* 时间复杂度: O(n^2)
|
||||||
|
* 空间复杂度: O(n^2),最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -133,6 +133,11 @@ public:
|
|||||||
LinkedNode* tmp = cur->next;
|
LinkedNode* tmp = cur->next;
|
||||||
cur->next = cur->next->next;
|
cur->next = cur->next->next;
|
||||||
delete tmp;
|
delete tmp;
|
||||||
|
//delete命令指示释放了tmp指针原本所指的那部分内存,
|
||||||
|
//被delete后的指针tmp的值(地址)并非就是NULL,而是随机值。也就是被delete后,
|
||||||
|
//如果不再加上一句tmp=nullptr,tmp会成为乱指的野指针
|
||||||
|
//如果之后的程序不小心使用了tmp,会指向难以预想的内存空间
|
||||||
|
tmp=nullptr;
|
||||||
_size--;
|
_size--;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1450,3 +1455,4 @@ impl MyLinkedList {
|
|||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
@ -284,6 +284,33 @@ func min(a, b int) int {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
``` GO
|
||||||
|
第二种思路: dp[i]表示从i层起跳所需要支付的最小费用
|
||||||
|
递推公式:
|
||||||
|
i<n :dp[i] = min(dp[i-1],dp[i-2])+cost[i]
|
||||||
|
i==n:dp[i] = min(dp[i-1],dp[i-2]) (登顶)
|
||||||
|
|
||||||
|
func minCostClimbingStairs(cost []int) int {
|
||||||
|
n := len(cost)
|
||||||
|
dp := make([]int, n+1)
|
||||||
|
dp[0], dp[1] = cost[0], cost[1]
|
||||||
|
for i := 2; i <= n; i++ {
|
||||||
|
if i < n {
|
||||||
|
dp[i] = min(dp[i-1], dp[i-2]) + cost[i]
|
||||||
|
} else {
|
||||||
|
dp[i] = min(dp[i-1], dp[i-2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[n]
|
||||||
|
}
|
||||||
|
func min(a, b int) int {
|
||||||
|
if a < b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
```Javascript
|
```Javascript
|
||||||
|
@ -231,23 +231,18 @@ var largestSumAfterKNegations = function(nums, k) {
|
|||||||
|
|
||||||
```Rust
|
```Rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn largest_sum_after_k_negations(nums: Vec<i32>, k: i32) -> i32 {
|
pub fn largest_sum_after_k_negations(mut nums: Vec<i32>, mut k: i32) -> i32 {
|
||||||
let mut nums = nums;
|
nums.sort_by_key(|b| std::cmp::Reverse(b.abs()));
|
||||||
let mut k = k;
|
for v in nums.iter_mut() {
|
||||||
let len = nums.len();
|
if *v < 0 && k > 0 {
|
||||||
nums.sort_by(|a, b| b.abs().cmp(&a.abs()));
|
*v *= -1;
|
||||||
for i in 0..len {
|
|
||||||
if nums[i] < 0 && k > 0 {
|
|
||||||
nums[i] *= -1;
|
|
||||||
k -= 1;
|
k -= 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if k % 2 == 1 { nums[len - 1] *= -1; }
|
if k % 2 == 1 {
|
||||||
let mut result = 0;
|
*nums.last_mut().unwrap() *= -1;
|
||||||
for num in nums {
|
|
||||||
result += num;
|
|
||||||
}
|
}
|
||||||
result
|
nums.iter().sum()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user