Skip to content

Commit

Permalink
Merge branch 'youngyangyang04:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyangu authored May 5, 2022
2 parents 0c0e12f + 28a8d2e commit b92cdf0
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@

如果是已工作,备注:姓名-城市-岗位-组队刷题。如果学生,备注:姓名-学校-年级-组队刷题。**备注没有自我介绍不通过哦**

<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20220426233122.png" data-img="1" width="200" height="200"></img></div>

<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/第二企业刷题活码.png" data-img="1" width="200" height="200"></img></div>



Expand All @@ -543,6 +544,7 @@

**来看看就知道了,你会发现相见恨晚!**


<a name="公众号"></a>
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20211026122841.png" data-img="1" width="650" height="500"></img></div>

4 changes: 1 addition & 3 deletions problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {

for fastIndex in 0..<nums.count {
if val != nums[fastIndex] {
if slowIndex != fastIndex {
nums[slowIndex] = nums[fastIndex]
}
slowIndex += 1
slowIndex += 1
}
}
return slowIndex
Expand Down
52 changes: 52 additions & 0 deletions problems/0121.买卖股票的最佳时机.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,36 @@ class Solution:
```

Go:
> 贪心法:
```Go
func maxProfit(prices []int) int {
low := math.MaxInt32
rlt := 0
for i := range prices{
low = min(low, prices[i])
rlt = max(rlt, prices[i]-low)
}

return rlt
}
func min(a, b int) int {
if a < b{
return a
}

return b
}

func max(a, b int) int {
if a > b{
return a
}

return b
}
```
> 动态规划:版本一
```Go
func maxProfit(prices []int) int {
length:=len(prices)
Expand All @@ -338,6 +367,29 @@ func max(a,b int)int {
}
```

> 动态规划:版本二
```Go
func maxProfit(prices []int) int {
dp := [2][2]int{}
dp[0][0] = -prices[0]
dp[0][1] = 0
for i := 1; i < len(prices); i++{
dp[i%2][0] = max(dp[(i-1)%2][0], -prices[i])
dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i])
}

return dp[(len(prices)-1)%2][1]
}

func max(a, b int) int {
if a > b{
return a
}

return b
}
```

JavaScript:

> 动态规划
Expand Down
2 changes: 1 addition & 1 deletion problems/0122.买卖股票的最佳时机II.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ const maxProfit = (prices) => {
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
}

return dp[prices.length -1][0];
return dp[prices.length -1][1];
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ const maxProfit = (prices) => {
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
}

return dp[prices.length -1][0];
return dp[prices.length -1][1];
};

// 方法二:动态规划(滚动数组)
Expand Down
26 changes: 26 additions & 0 deletions problems/0135.分发糖果.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,32 @@ var candy = function(ratings) {
};
```

### TypeScript

```typescript
function candy(ratings: number[]): number {
const candies: number[] = [];
candies[0] = 1;
// 保证右边高分孩子一定比左边低分孩子发更多的糖果
for (let i = 1, length = ratings.length; i < length; i++) {
if (ratings[i] > ratings[i - 1]) {
candies[i] = candies[i - 1] + 1;
} else {
candies[i] = 1;
}
}
// 保证左边高分孩子一定比右边低分孩子发更多的糖果
for (let i = ratings.length - 2; i >= 0; i--) {
if (ratings[i] > ratings[i + 1]) {
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
}
}
return candies.reduce((pre, cur) => pre + cur);
};
```




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
18 changes: 18 additions & 0 deletions problems/0406.根据身高重建队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,24 @@ var reconstructQueue = function(people) {
};
```

### TypeScript

```typescript
function reconstructQueue(people: number[][]): number[][] {
people.sort((a, b) => {
if (a[0] === b[0]) return a[1] - b[1];
return b[0] - a[0];
});
const resArr: number[][] = [];
for (let i = 0, length = people.length; i < length; i++) {
resArr.splice(people[i][1], 0, people[i]);
}
return resArr;
};
```




-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
2 changes: 1 addition & 1 deletion problems/0416.分割等和子集.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

## 01背包问题

背包问题,大家都知道,有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。

**背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。**

Expand Down
15 changes: 7 additions & 8 deletions problems/0503.下一个更大元素II.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,19 @@ JavaScript:
* @return {number[]}
*/
var nextGreaterElements = function (nums) {
// let map = new Map();
const len = nums.length;
let stack = [];
let res = new Array(nums.length).fill(-1);
for (let i = 0; i < nums.length * 2; i++) {
let res = Array(len).fill(-1);
for (let i = 0; i < len * 2; i++) {
while (
stack.length &&
nums[i % nums.length] > nums[stack[stack.length - 1]]
nums[i % len] > nums[stack[stack.length - 1]]
) {
let index = stack.pop();
res[index] = nums[i % nums.length];
const index = stack.pop();
res[index] = nums[i % len];
}
stack.push(i % nums.length);
stack.push(i % len);
}

return res;
};
```
Expand Down
32 changes: 23 additions & 9 deletions problems/0739.每日温度.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,32 +301,46 @@ func dailyTemperatures(num []int) []int {

JavaScript:
```javascript
/**
* @param {number[]} temperatures
* @return {number[]}
*/
// 版本一
var dailyTemperatures = function(temperatures) {
let n = temperatures.length;
let res = new Array(n).fill(0);
let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标
const n = temperatures.length;
const res = Array(n).fill(0);
const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标
stack.push(0);
for (let i = 1; i < n; i++) {
// 栈顶元素
let top = stack[stack.length - 1];
const top = stack[stack.length - 1];
if (temperatures[i] < temperatures[top]) {
stack.push(i);
} else if (temperatures[i] === temperatures[top]) {
stack.push(i);
} else {
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
let top = stack.pop();
const top = stack.pop();
res[top] = i - top;
}
stack.push(i);
}
}
return res;
};


// 版本二
var dailyTemperatures = function(temperatures) {
const n = temperatures.length;
const res = Array(n).fill(0);
const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标
stack.push(0);
for (let i = 1; i < n; i++) {
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
const top = stack.pop();
res[top] = i - top;
}
stack.push(i);
}
return res;
};
```


Expand Down
34 changes: 34 additions & 0 deletions problems/0860.柠檬水找零.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,39 @@ var lemonadeChange = function(bills) {

```

### TypeScript

```typescript
function lemonadeChange(bills: number[]): boolean {
let five: number = 0,
ten: number = 0;
for (let bill of bills) {
switch (bill) {
case 5:
five++;
break;
case 10:
if (five < 1) return false;
five--;
ten++
break;
case 20:
if (ten > 0 && five > 0) {
five--;
ten--;
} else if (five > 2) {
five -= 3;
} else {
return false;
}
break;
}
}
return true;
};
```



-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
25 changes: 21 additions & 4 deletions problems/1047.删除字符串中的所有相邻重复项.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,9 @@ func removeDuplicates(s string) string {

javaScript:

法一:使用栈

```js
/**
* @param {string} s
* @return {string}
*/
var removeDuplicates = function(s) {
const stack = [];
for(const x of s) {
Expand All @@ -267,6 +265,25 @@ var removeDuplicates = function(s) {
};
```

法二:双指针(模拟栈)

```js
// 原地解法(双指针模拟栈)
var removeDuplicates = function(s) {
s = [...s];
let top = -1; // 指向栈顶元素的下标
for(let i = 0; i < s.length; i++) {
if(top === -1 || s[top] !== s[i]) { // top === -1 即空栈
s[++top] = s[i]; // 入栈
} else {
top--; // 推出栈
}
}
s.length = top + 1; // 栈顶元素下标 + 1 为栈的长度
return s.join('');
};
```

TypeScript:

```typescript
Expand Down

0 comments on commit b92cdf0

Please sign in to comment.