mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -197,7 +197,23 @@ var removeElement = (nums, val) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function removeElement(nums: number[], val: number): number {
|
||||
let slowIndex: number = 0, fastIndex: number = 0;
|
||||
while (fastIndex < nums.length) {
|
||||
if (nums[fastIndex] !== val) {
|
||||
nums[slowIndex++] = nums[fastIndex];
|
||||
}
|
||||
fastIndex++;
|
||||
}
|
||||
return slowIndex;
|
||||
};
|
||||
```
|
||||
|
||||
Ruby:
|
||||
|
||||
```ruby
|
||||
def remove_element(nums, val)
|
||||
i = 0
|
||||
|
@ -649,6 +649,41 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
//前缀表(不减一)Java实现
|
||||
public int strStr(String haystack, String needle) {
|
||||
if (needle.length() == 0) return 0;
|
||||
int[] next = new int[needle.length()];
|
||||
getNext(next, needle);
|
||||
|
||||
int j = 0;
|
||||
for (int i = 0; i < haystack.length(); i++) {
|
||||
while (j > 0 && needle.charAt(j) != haystack.charAt(i))
|
||||
j = next[j - 1];
|
||||
if (needle.charAt(j) == haystack.charAt(i))
|
||||
j++;
|
||||
if (j == needle.length())
|
||||
return i - needle.length() + 1;
|
||||
}
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
private void getNext(int[] next, String s) {
|
||||
int j = 0;
|
||||
next[0] = 0;
|
||||
for (int i = 1; i < s.length(); i++) {
|
||||
while (j > 0 && s.charAt(j) != s.charAt(i))
|
||||
j = next[j - 1];
|
||||
if (s.charAt(j) == s.charAt(i))
|
||||
j++;
|
||||
next[i] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Python3:
|
||||
|
||||
```python
|
||||
|
@ -422,13 +422,13 @@ class Solution:
|
||||
def backtrack(n,k,startIndex):
|
||||
if len(path) == k:
|
||||
res.append(path[:])
|
||||
return
|
||||
return
|
||||
for i in range(startIndex,n - (k - len(path)) + 2): #优化的地方
|
||||
path.append(i) #处理节点
|
||||
backtrack(n,k,i+1) #递归
|
||||
path.pop() #回溯,撤销处理的节点
|
||||
backtrack(n,k,1)
|
||||
return res
|
||||
backtrack(n,k,1)
|
||||
return res
|
||||
```
|
||||
|
||||
|
||||
|
@ -182,13 +182,13 @@ class Solution:
|
||||
def backtrack(n,k,startIndex):
|
||||
if len(path) == k:
|
||||
res.append(path[:])
|
||||
return
|
||||
return
|
||||
for i in range(startIndex,n-(k-len(path))+2): #优化的地方
|
||||
path.append(i) #处理节点
|
||||
backtrack(n,k,i+1) #递归
|
||||
path.pop() #回溯,撤销处理的节点
|
||||
backtrack(n,k,1)
|
||||
return res
|
||||
backtrack(n,k,1)
|
||||
return res
|
||||
```
|
||||
Go:
|
||||
```Go
|
||||
|
@ -107,7 +107,7 @@ public:
|
||||
};
|
||||
```
|
||||
|
||||
时间复杂度:$O(n)$
|
||||
时间复杂度:$O(n)$
|
||||
空间复杂度:$O(1)$
|
||||
|
||||
**一些录友会疑惑为什么时间复杂度是$O(n)$**。
|
||||
@ -121,7 +121,6 @@ public:
|
||||
|
||||
|
||||
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
@ -214,6 +213,28 @@ var minSubArrayLen = function(target, nums) {
|
||||
};
|
||||
```
|
||||
|
||||
Typescript:
|
||||
|
||||
```typescript
|
||||
function minSubArrayLen(target: number, nums: number[]): number {
|
||||
let left: number = 0, right: number = 0;
|
||||
let res: number = nums.length + 1;
|
||||
let sum: number = 0;
|
||||
while (right < nums.length) {
|
||||
sum += nums[right];
|
||||
if (sum >= target) {
|
||||
// 不断移动左指针,直到不能再缩小为止
|
||||
while (sum - nums[left] >= target) {
|
||||
sum -= nums[left++];
|
||||
}
|
||||
res = Math.min(res, right - left + 1);
|
||||
}
|
||||
right++;
|
||||
}
|
||||
return res === nums.length + 1 ? 0 : res;
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
@ -291,5 +312,23 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Ruby:
|
||||
|
||||
```ruby
|
||||
def min_sub_array_len(target, nums)
|
||||
res = Float::INFINITY # 无穷大
|
||||
i, sum = 0, 0
|
||||
nums.length.times do |j|
|
||||
sum += nums[j]
|
||||
while sum >= target
|
||||
res = [res, j - i + 1].min
|
||||
sum -= nums[i]
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
res == Float::INFINITY ? 0 : res
|
||||
end
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -323,7 +323,6 @@ class Solution:
|
||||
self.backtracking(k, n, i + 1)
|
||||
self.path.pop()
|
||||
self.sum_now -= i
|
||||
return
|
||||
```
|
||||
|
||||
## Go
|
||||
|
@ -93,7 +93,7 @@ dp[i][3] = dp[i - 1][2];
|
||||
综上分析,递推代码如下:
|
||||
|
||||
```CPP
|
||||
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i];
|
||||
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3], dp[i - 1][1]) - prices[i]);
|
||||
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
|
||||
dp[i][2] = dp[i - 1][0] + prices[i];
|
||||
dp[i][3] = dp[i - 1][2];
|
||||
|
@ -142,7 +142,7 @@ class Solution {
|
||||
|
||||
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
|
||||
// 根据map的value值正序排,相当于一个小顶堆
|
||||
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
|
||||
PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>((o1, o2) -> o2.getValue() - o1.getValue());
|
||||
for (Map.Entry<Integer, Integer> entry : entries) {
|
||||
queue.offer(entry);
|
||||
if (queue.size() > k) {
|
||||
|
@ -154,6 +154,8 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
> 动态规划:
|
||||
```java
|
||||
/**
|
||||
* 1.dp[i] 代表当前下标最大连续值
|
||||
@ -180,6 +182,25 @@ Java:
|
||||
}
|
||||
```
|
||||
|
||||
> 贪心法:
|
||||
|
||||
```Java
|
||||
public static int findLengthOfLCIS(int[] nums) {
|
||||
if (nums.length == 0) return 0;
|
||||
int res = 1; // 连续子序列最少也是1
|
||||
int count = 1;
|
||||
for (int i = 0; i < nums.length - 1; i++) {
|
||||
if (nums[i + 1] > nums[i]) { // 连续记录
|
||||
count++;
|
||||
} else { // 不连续,count从头开始
|
||||
count = 1;
|
||||
}
|
||||
if (count > res) res = count;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
> 动态规划:
|
||||
|
@ -119,7 +119,7 @@ C++代码如下:
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> dailyTemperatures(vector<int>& T) {
|
||||
// 递减栈
|
||||
// 递增栈
|
||||
stack<int> st;
|
||||
vector<int> result(T.size(), 0);
|
||||
st.push(0);
|
||||
@ -150,7 +150,7 @@ public:
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> dailyTemperatures(vector<int>& T) {
|
||||
stack<int> st; // 递减栈
|
||||
stack<int> st; // 递增栈
|
||||
vector<int> result(T.size(), 0);
|
||||
for (int i = 0; i < T.size(); i++) {
|
||||
while (!st.empty() && T[i] > T[st.top()]) { // 注意栈不能为空
|
||||
@ -178,7 +178,7 @@ public:
|
||||
Java:
|
||||
```java
|
||||
/**
|
||||
* 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到笑
|
||||
* 单调栈,栈内顺序要么从大到小 要么从小到大,本题从大到小
|
||||
* <p>
|
||||
* 入站元素要和当前栈内栈首元素进行比较
|
||||
* 若大于栈首则 则与元素下标做差
|
||||
|
@ -221,6 +221,35 @@ var sortedSquares = function(nums) {
|
||||
};
|
||||
```
|
||||
|
||||
Typescript:
|
||||
|
||||
双指针法:
|
||||
|
||||
```typescript
|
||||
function sortedSquares(nums: number[]): number[] {
|
||||
let left: number = 0, right: number = nums.length - 1;
|
||||
let resArr: number[] = new Array(nums.length);
|
||||
let resArrIndex: number = resArr.length - 1;
|
||||
while (left <= right) {
|
||||
if (Math.abs(nums[left]) < Math.abs(nums[right])) {
|
||||
resArr[resArrIndex] = nums[right--] ** 2;
|
||||
} else {
|
||||
resArr[resArrIndex] = nums[left++] ** 2;
|
||||
}
|
||||
resArrIndex--;
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
骚操作法(暴力思路):
|
||||
|
||||
```typescript
|
||||
function sortedSquares(nums: number[]): number[] {
|
||||
return nums.map(i => i * i).sort((a, b) => a - b);
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
Reference in New Issue
Block a user