mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update
This commit is contained in:
@ -368,6 +368,27 @@ Java:
|
||||
|
||||
Python:
|
||||
|
||||
双指针法
|
||||
```python3
|
||||
class Solution:
|
||||
def trap(self, height: List[int]) -> int:
|
||||
res = 0
|
||||
for i in range(len(height)):
|
||||
if i == 0 or i == len(height)-1: continue
|
||||
lHight = height[i-1]
|
||||
rHight = height[i+1]
|
||||
for j in range(i-1):
|
||||
if height[j] > lHight:
|
||||
lHight = height[j]
|
||||
for k in range(i+2,len(height)):
|
||||
if height[k] > rHight:
|
||||
rHight = height[k]
|
||||
res1 = min(lHight,rHight) - height[i]
|
||||
if res1 > 0:
|
||||
res += res1
|
||||
return res
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
JavaScript:
|
||||
|
@ -188,7 +188,40 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
```golang
|
||||
//贪心算法
|
||||
func maxProfit(prices []int) int {
|
||||
var sum int
|
||||
for i := 1; i < len(prices); i++ {
|
||||
// 累加每次大于0的交易
|
||||
if prices[i]-prices[i-1] > 0 {
|
||||
sum += prices[i]-prices[i-1]
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
```
|
||||
|
||||
```golang
|
||||
//确定售卖点
|
||||
func maxProfit(prices []int) int {
|
||||
var result,buy int
|
||||
prices=append(prices,0)//在price末尾加个0,防止price一直递增
|
||||
/**
|
||||
思路:检查后一个元素是否大于当前元素,如果小于,则表明这是一个售卖点,当前元素的值减去购买时候的值
|
||||
如果不小于,说明后面有更好的售卖点,
|
||||
**/
|
||||
for i:=0;i<len(prices)-1;i++{
|
||||
if prices[i]>prices[i+1]{
|
||||
result+=prices[i]-prices[buy]
|
||||
buy=i+1
|
||||
}else if prices[buy]>prices[i]{//更改最低购买点
|
||||
buy=i
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
|
@ -408,6 +408,65 @@ func reverse(b *[]byte, left, right int) {
|
||||
|
||||
|
||||
|
||||
javaScript:
|
||||
```js
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {string}
|
||||
*/
|
||||
var reverseWords = function(s) {
|
||||
// 字符串转数组
|
||||
const strArr = Array.from(s);
|
||||
// 移除多余空格
|
||||
removeExtraSpaces(strArr);
|
||||
// 翻转
|
||||
reverse(strArr, 0, strArr.length - 1);
|
||||
|
||||
let start = 0;
|
||||
|
||||
for(let i = 0; i <= strArr.length; i++) {
|
||||
if (strArr[i] === ' ' || i === strArr.length) {
|
||||
// 翻转单词
|
||||
reverse(strArr, start, i - 1);
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return strArr.join('');
|
||||
};
|
||||
|
||||
// 删除多余空格
|
||||
function removeExtraSpaces(strArr) {
|
||||
let slowIndex = 0;
|
||||
let fastIndex = 0;
|
||||
|
||||
while(fastIndex < strArr.length) {
|
||||
// 移除开始位置和重复的空格
|
||||
if (strArr[fastIndex] === ' ' && (fastIndex === 0 || strArr[fastIndex - 1] === ' ')) {
|
||||
fastIndex++;
|
||||
} else {
|
||||
strArr[slowIndex++] = strArr[fastIndex++];
|
||||
}
|
||||
}
|
||||
|
||||
// 移除末尾空格
|
||||
strArr.length = strArr[slowIndex - 1] === ' ' ? slowIndex - 1 : slowIndex;
|
||||
}
|
||||
|
||||
// 翻转从 start 到 end 的字符
|
||||
function reverse(strArr, start, end) {
|
||||
let left = start;
|
||||
let right = end;
|
||||
|
||||
while(left < right) {
|
||||
// 交换
|
||||
[strArr[left], strArr[right]] = [strArr[right], strArr[left]];
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -273,8 +273,8 @@ class Solution {
|
||||
int[] res = new int[n - k + 1];
|
||||
int idx = 0;
|
||||
for(int i = 0; i < n; i++) {
|
||||
// 根据题意,i为nums下标,是要在[i - k + 1, k] 中选到最大值,只需要保证两点
|
||||
// 1.队列头结点需要在[i - k + 1, k]范围内,不符合则要弹出
|
||||
// 根据题意,i为nums下标,是要在[i - k + 1, i] 中选到最大值,只需要保证两点
|
||||
// 1.队列头结点需要在[i - k + 1, i]范围内,不符合则要弹出
|
||||
while(!deque.isEmpty() && deque.peek() < i - k + 1){
|
||||
deque.poll();
|
||||
}
|
||||
|
@ -151,7 +151,24 @@ class Solution:
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
```golang
|
||||
func wiggleMaxLength(nums []int) int {
|
||||
var count,preDiff,curDiff int
|
||||
count=1
|
||||
if len(nums)<2{
|
||||
return count
|
||||
}
|
||||
for i:=0;i<len(nums)-1;i++{
|
||||
curDiff=nums[i+1]-nums[i]
|
||||
//如果有正有负则更新下标值||或者只有前一个元素为0(针对两个不等元素的序列也视作摆动序列,且摆动长度为2)
|
||||
if (curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0){
|
||||
preDiff=curDiff
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
|
@ -146,6 +146,23 @@ class Solution:
|
||||
return res
|
||||
```
|
||||
Go:
|
||||
```golang
|
||||
//排序后,局部最优
|
||||
func findContentChildren(g []int, s []int) int {
|
||||
sort.Ints(g)
|
||||
sort.Ints(s)
|
||||
|
||||
// 从小到大
|
||||
child := 0
|
||||
for sIdx := 0; child < len(g) && sIdx < len(s); sIdx++ {
|
||||
if s[sIdx] >= g[child] {//如果饼干的大小大于或等于孩子的为空则给与,否则不给予,继续寻找选一个饼干是否符合
|
||||
child++
|
||||
}
|
||||
}
|
||||
|
||||
return child
|
||||
}
|
||||
|
||||
|
||||
Javascript:
|
||||
```Javascript
|
||||
|
@ -206,7 +206,13 @@ public class TreeNode {
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class TreeNode:
|
||||
def __init__(self, value):
|
||||
self.value = value
|
||||
self.left = None
|
||||
self.right = None
|
||||
```
|
||||
|
||||
Go:
|
||||
```
|
||||
|
@ -199,6 +199,91 @@ func replaceSpace(s string) string {
|
||||
|
||||
|
||||
|
||||
|
||||
python:
|
||||
```python
|
||||
class Solution(object):
|
||||
def replaceSpace(self, s):
|
||||
"""
|
||||
:type s: str
|
||||
:rtype: str
|
||||
"""
|
||||
list_s = list(s)
|
||||
|
||||
# 记录原本字符串的长度
|
||||
original_end = len(s)
|
||||
|
||||
# 将空格改成%20 使得字符串总长增长 2n,n为原本空格数量。
|
||||
# 所以记录空格数量就可以得到目标字符串的长度
|
||||
n_space = 0
|
||||
for ss in s:
|
||||
if ss == ' ':
|
||||
n_space += 1
|
||||
|
||||
list_s += ['0'] * 2 * n_space
|
||||
|
||||
# 设置左右指针位置
|
||||
left, right = original_end - 1, len(list_s) - 1
|
||||
|
||||
# 循环直至左指针越界
|
||||
while left >= 0:
|
||||
if list_s[left] == ' ':
|
||||
list_s[right] = '0'
|
||||
list_s[right - 1] = '2'
|
||||
list_s[right - 2] = '%'
|
||||
right -= 3
|
||||
else:
|
||||
list_s[right] = list_s[left]
|
||||
right -= 1
|
||||
|
||||
left -= 1
|
||||
|
||||
# 将list变回str,输出
|
||||
s = ''.join(list_s)
|
||||
return s
|
||||
|
||||
```
|
||||
|
||||
|
||||
javaScript:
|
||||
```js
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {string}
|
||||
*/
|
||||
var replaceSpace = function(s) {
|
||||
// 字符串转为数组
|
||||
const strArr = Array.from(s);
|
||||
let count = 0;
|
||||
|
||||
// 计算空格数量
|
||||
for(let i = 0; i < strArr.length; i++) {
|
||||
if (strArr[i] === ' ') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
let left = strArr.length - 1;
|
||||
let right = strArr.length + count * 2 - 1;
|
||||
|
||||
while(left >= 0) {
|
||||
if (strArr[left] === ' ') {
|
||||
strArr[right--] = '0';
|
||||
strArr[right--] = '2';
|
||||
strArr[right--] = '%';
|
||||
left--;
|
||||
} else {
|
||||
strArr[right--] = strArr[left--];
|
||||
}
|
||||
}
|
||||
|
||||
// 数组转字符串
|
||||
return strArr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user