Merge branch 'youngyangyang04:master' into master

This commit is contained in:
梦华
2022-05-06 20:49:26 +08:00
committed by GitHub
19 changed files with 499 additions and 33 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,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

View File

@ -244,6 +244,60 @@ var maxSubArray = function(nums) {
};
```
### C:
贪心:
```c
int maxSubArray(int* nums, int numsSize){
int maxVal = INT_MIN;
int subArrSum = 0;
int i;
for(i = 0; i < numsSize; ++i) {
subArrSum += nums[i];
// 若当前局部和大于之前的最大结果,对结果进行更新
maxVal = subArrSum > maxVal ? subArrSum : maxVal;
// 若当前局部和为负对结果无益。则从nums[i+1]开始应重新计算。
subArrSum = subArrSum < 0 ? 0 : subArrSum;
}
return maxVal;
}
```
动态规划:
```c
/**
* 解题思路:动态规划:
* 1. dp数组dp[i]表示从0到i的子序列中最大序列和的值
* 2. 递推公式dp[i] = max(dp[i-1] + nums[i], nums[i])
若dp[i-1]<0对最后结果无益。dp[i]则为nums[i]。
* 3. dp数组初始化dp[0]的最大子数组和为nums[0]
* 4. 推导顺序:从前往后遍历
*/
#define max(a, b) (((a) > (b)) ? (a) : (b))
int maxSubArray(int* nums, int numsSize){
int dp[numsSize];
// dp[0]最大子数组和为nums[0]
dp[0] = nums[0];
// 若numsSize为1应直接返回nums[0]
int subArrSum = nums[0];
int i;
for(i = 1; i < numsSize; ++i) {
dp[i] = max(dp[i - 1] + nums[i], nums[i]);
// 若dp[i]大于之前记录的最大值,进行更新
if(dp[i] > subArrSum)
subArrSum = dp[i];
}
return subArrSum;
}
```
### TypeScript
**贪心**
@ -281,5 +335,6 @@ function maxSubArray(nums: number[]): number {
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -154,6 +154,30 @@ var canJump = function(nums) {
};
```
### C
```c
#define max(a, b) (((a) > (b)) ? (a) : (b))
bool canJump(int* nums, int numsSize){
int cover = 0;
int i;
// 只可能获取cover范围中的步数所以i<=cover
for(i = 0; i <= cover; ++i) {
// 更新cover为从i出发能到达的最大值/cover的值中较大值
cover = max(i + nums[i], cover);
// 若更新后cover可以到达最后的元素返回true
if(cover >= numsSize - 1)
return true;
}
return false;
}
```
### TypeScript
```typescript

View File

@ -246,11 +246,11 @@ var generateMatrix = function(n) {
res[row][col] = count++;
}
// 下行从右到左(左闭右开)
for (; col > startX; col--) {
for (; col > startY; col--) {
res[row][col] = count++;
}
// 左列做下到上(左闭右开)
for (; row > startY; row--) {
for (; row > startX; row--) {
res[row][col] = count++;
}

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];
};
```
@ -281,7 +281,7 @@ function maxProfit(prices: number[]): number {
```
C:
贪心:
```c
int maxProfit(int* prices, int pricesSize){
int result = 0;
@ -296,5 +296,27 @@ int maxProfit(int* prices, int pricesSize){
}
```
动态规划:
```c
#define max(a, b) (((a) > (b)) ? (a) : (b))
int maxProfit(int* prices, int pricesSize){
int dp[pricesSize][2];
dp[0][0] = 0 - prices[0];
dp[0][1] = 0;
int i;
for(i = 1; i < pricesSize; ++i) {
// dp[i][0]为i-1天持股的钱数/在第i天用i-1天的钱买入的最大值。
// 若i-1天持股且第i天买入股票比i-1天持股时更亏说明应在i-1天时持股
dp[i][0] = max(dp[i-1][0], dp[i-1][1] - prices[i]);
//dp[i][1]为i-1天不持股钱数/在第i天卖出所持股票dp[i-1][0] + prices[i]的最大值
dp[i][1] = max(dp[i-1][1], dp[i-1][0] + prices[i]);
}
// 返回在最后一天不持股时的钱数(将股票卖出后钱最大化)
return dp[pricesSize - 1][1];
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

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

@ -235,7 +235,7 @@ class Solution {
return index;
}
}
```
```
### Python
```python
@ -364,7 +364,50 @@ var canCompleteCircuit = function(gas, cost) {
};
```
### TypeScript
**暴力法:**
```typescript
function canCompleteCircuit(gas: number[], cost: number[]): number {
for (let i = 0, length = gas.length; i < length; i++) {
let curSum: number = 0;
let index: number = i;
while (curSum >= 0 && index < i + length) {
let tempIndex: number = index % length;
curSum += gas[tempIndex] - cost[tempIndex];
index++;
}
if (index === i + length && curSum >= 0) return i;
}
return -1;
};
```
**解法二:**
```typescript
function canCompleteCircuit(gas: number[], cost: number[]): number {
let total: number = 0;
let curGas: number = 0;
let tempDiff: number = 0;
let resIndex: number = 0;
for (let i = 0, length = gas.length; i < length; i++) {
tempDiff = gas[i] - cost[i];
total += tempDiff;
curGas += tempDiff;
if (curGas < 0) {
resIndex = i + 1;
curGas = 0;
}
}
if (total < 0) return -1;
return resIndex;
};
```
### C
```c
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
int curSum = 0;

View File

@ -258,5 +258,73 @@ var candy = function(ratings) {
```
### C
```c
#define max(a, b) (((a) > (b)) ? (a) : (b))
int *initCandyArr(int size) {
int *candyArr = (int*)malloc(sizeof(int) * size);
int i;
for(i = 0; i < size; ++i)
candyArr[i] = 1;
return candyArr;
}
int candy(int* ratings, int ratingsSize){
// 初始化数组,每个小孩开始至少有一颗糖
int *candyArr = initCandyArr(ratingsSize);
int i;
// 先判断右边是否比左边评分高。若是,右边孩子的糖果为左边孩子+1candyArr[i] = candyArr[i - 1] + 1)
for(i = 1; i < ratingsSize; ++i) {
if(ratings[i] > ratings[i - 1])
candyArr[i] = candyArr[i - 1] + 1;
}
// 再判断左边评分是否比右边高。
// 若是,左边孩子糖果为右边孩子糖果+1/自己所持糖果最大值。(若糖果已经比右孩子+1多则不需要更多糖果
// 举例ratings为[1, 2, 3, 1]。此时评分为3的孩子在判断右边比左边大后为3虽然它比最末尾的1(ratings[3])大但是candyArr[3]为1。所以不必更新candyArr[2]
for(i = ratingsSize - 2; i >= 0; --i) {
if(ratings[i] > ratings[i + 1])
candyArr[i] = max(candyArr[i], candyArr[i + 1] + 1);
}
// 求出糖果之和
int result = 0;
for(i = 0; i < ratingsSize; ++i) {
result += candyArr[i];
}
return result;
}
```
### 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

@ -336,7 +336,33 @@ class Solution:
return pre
```
### Go
```go
# 方法三 分割链表
func reorderList(head *ListNode) {
var slow=head
var fast=head
for fast!=nil&&fast.Next!=nil{
slow=slow.Next
fast=fast.Next.Next
} //双指针将链表分为左右两部分
var right =new(ListNode)
for slow!=nil{
temp:=slow.Next
slow.Next=right.Next
right.Next=slow
slow=temp
} //翻转链表右半部分
right=right.Next //right为反转后得右半部分
h:=head
for right.Next!=nil{
temp:=right.Next
right.Next=h.Next
h.Next=right
h=h.Next.Next
right=temp
} //将左右两部分重新组合
}
```
### JavaScript
```javascript

View File

@ -298,6 +298,35 @@ var wiggleMaxLength = function(nums) {
};
```
### C
**贪心**
```c
int wiggleMaxLength(int* nums, int numsSize){
if(numsSize <= 1)
return numsSize;
int length = 1;
int preDiff , curDiff;
preDiff = curDiff = 0;
for(int i = 0; i < numsSize - 1; ++i) {
// 计算当前i元素与i+1元素差值
curDiff = nums[i+1] - nums[i];
// 若preDiff与curDiff符号不符则子序列长度+1。更新preDiff的符号
// 若preDiff与curDiff符号一致当前i元素为连续升序/连续降序子序列的中间元素。不被记录入长度
// 注当preDiff为0时curDiff为正或为负都属于符号不同
if((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) {
preDiff = curDiff;
length++;
}
}
return length;
}
```
### TypeScript
**贪心**

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

@ -211,5 +211,70 @@ var largestSumAfterKNegations = function(nums, k) {
};
```
### C
```c
#define abs(a) (((a) > 0) ? (a) : (-(a)))
// 对数组求和
int sum(int *nums, int numsSize) {
int sum = 0;
int i;
for(i = 0; i < numsSize; ++i) {
sum += nums[i];
}
return sum;
}
int cmp(const void* v1, const void* v2) {
return abs(*(int*)v2) - abs(*(int*)v1);
}
int largestSumAfterKNegations(int* nums, int numsSize, int k){
qsort(nums, numsSize, sizeof(int), cmp);
int i;
for(i = 0; i < numsSize; ++i) {
// 遍历数组,若当前元素<0则将当前元素转变k--
if(nums[i] < 0 && k > 0) {
nums[i] *= -1;
--k;
}
}
// 若遍历完数组后k还有剩余此时所有元素应均为正则将绝对值最小的元素nums[numsSize - 1]变为负
if(k % 2 == 1)
nums[numsSize - 1] *= -1;
return sum(nums, numsSize);
}
```
### TypeScript
```typescript
function largestSumAfterKNegations(nums: number[], k: number): number {
nums.sort((a, b) => Math.abs(b) - Math.abs(a));
let curIndex: number = 0;
const length = nums.length;
while (curIndex < length && k > 0) {
if (nums[curIndex] < 0) {
nums[curIndex] *= -1;
k--;
}
curIndex++;
}
while (k > 0) {
nums[length - 1] *= -1;
k--;
}
return nums.reduce((pre, cur) => pre + cur, 0);
};
```
-----------------------
<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