Merge branch 'youngyangyang04:master' into master

This commit is contained in:
gaoyangu
2022-05-05 14:55:49 +08:00
committed by GitHub
12 changed files with 188 additions and 28 deletions

View File

@ -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>
@ -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>

View File

@ -281,9 +281,7 @@ 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
}
}

View File

@ -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)
@ -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:
> 动态规划

View File

@ -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];
};
```

View File

@ -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];
};
// 方法二:动态规划(滚动数组)

View File

@ -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>

View File

@ -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>

View File

@ -50,7 +50,7 @@
## 01背包问题
背包问题大家都知道有N件物品和一个最多能重量为W 的背包。第i件物品的重量是weight[i]得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
背包问题大家都知道有N件物品和一个最多能重量为W 的背包。第i件物品的重量是weight[i]得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
**背包问题有多种背包方式常见的有01背包、完全背包、多重背包、分组背包和混合背包等等。**

View File

@ -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;
};
```

View File

@ -301,25 +301,22 @@ 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);
@ -327,6 +324,23 @@ var dailyTemperatures = function(temperatures) {
}
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;
};
```

View File

@ -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>

View File

@ -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) {
@ -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