mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -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>
|
<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>
|
<div align="center"><img src="https://code-thinking-1253855093.file.myqcloud.com/pics/20211026122841.png" data-img="1" width="650" height="500"></img></div>
|
||||||
|
|
||||||
|
@ -281,9 +281,7 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
|
|||||||
|
|
||||||
for fastIndex in 0..<nums.count {
|
for fastIndex in 0..<nums.count {
|
||||||
if val != nums[fastIndex] {
|
if val != nums[fastIndex] {
|
||||||
if slowIndex != fastIndex {
|
|
||||||
nums[slowIndex] = nums[fastIndex]
|
nums[slowIndex] = nums[fastIndex]
|
||||||
}
|
|
||||||
slowIndex += 1
|
slowIndex += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
### 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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
@ -246,11 +246,11 @@ var generateMatrix = function(n) {
|
|||||||
res[row][col] = count++;
|
res[row][col] = count++;
|
||||||
}
|
}
|
||||||
// 下行从右到左(左闭右开)
|
// 下行从右到左(左闭右开)
|
||||||
for (; col > startX; col--) {
|
for (; col > startY; col--) {
|
||||||
res[row][col] = count++;
|
res[row][col] = count++;
|
||||||
}
|
}
|
||||||
// 左列做下到上(左闭右开)
|
// 左列做下到上(左闭右开)
|
||||||
for (; row > startY; row--) {
|
for (; row > startX; row--) {
|
||||||
res[row][col] = count++;
|
res[row][col] = count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,7 +311,36 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
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
|
```Go
|
||||||
func maxProfit(prices []int) int {
|
func maxProfit(prices []int) int {
|
||||||
length:=len(prices)
|
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:
|
JavaScript:
|
||||||
|
|
||||||
> 动态规划
|
> 动态规划
|
||||||
|
@ -264,7 +264,7 @@ const maxProfit = (prices) => {
|
|||||||
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
|
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:
|
||||||
|
贪心:
|
||||||
```c
|
```c
|
||||||
int maxProfit(int* prices, int pricesSize){
|
int maxProfit(int* prices, int pricesSize){
|
||||||
int result = 0;
|
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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -276,7 +276,7 @@ const maxProfit = (prices) => {
|
|||||||
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
|
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];
|
||||||
};
|
};
|
||||||
|
|
||||||
// 方法二:动态规划(滚动数组)
|
// 方法二:动态规划(滚动数组)
|
||||||
|
@ -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
|
||||||
|
|
||||||
```c
|
```c
|
||||||
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
|
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
|
||||||
int curSum = 0;
|
int curSum = 0;
|
||||||
|
@ -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;
|
||||||
|
// 先判断右边是否比左边评分高。若是,右边孩子的糖果为左边孩子+1(candyArr[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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -336,7 +336,33 @@ class Solution:
|
|||||||
return pre
|
return pre
|
||||||
```
|
```
|
||||||
### Go
|
### 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
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
|
@ -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
|
### TypeScript
|
||||||
|
|
||||||
**贪心**
|
**贪心**
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -50,7 +50,7 @@
|
|||||||
|
|
||||||
## 01背包问题
|
## 01背包问题
|
||||||
|
|
||||||
背包问题,大家都知道,有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
|
背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
|
||||||
|
|
||||||
**背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。**
|
**背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。**
|
||||||
|
|
||||||
|
@ -166,20 +166,19 @@ JavaScript:
|
|||||||
* @return {number[]}
|
* @return {number[]}
|
||||||
*/
|
*/
|
||||||
var nextGreaterElements = function (nums) {
|
var nextGreaterElements = function (nums) {
|
||||||
// let map = new Map();
|
const len = nums.length;
|
||||||
let stack = [];
|
let stack = [];
|
||||||
let res = new Array(nums.length).fill(-1);
|
let res = Array(len).fill(-1);
|
||||||
for (let i = 0; i < nums.length * 2; i++) {
|
for (let i = 0; i < len * 2; i++) {
|
||||||
while (
|
while (
|
||||||
stack.length &&
|
stack.length &&
|
||||||
nums[i % nums.length] > nums[stack[stack.length - 1]]
|
nums[i % len] > nums[stack[stack.length - 1]]
|
||||||
) {
|
) {
|
||||||
let index = stack.pop();
|
const index = stack.pop();
|
||||||
res[index] = nums[i % nums.length];
|
res[index] = nums[i % len];
|
||||||
}
|
}
|
||||||
stack.push(i % nums.length);
|
stack.push(i % len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
@ -301,25 +301,22 @@ func dailyTemperatures(num []int) []int {
|
|||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
```javascript
|
```javascript
|
||||||
/**
|
// 版本一
|
||||||
* @param {number[]} temperatures
|
|
||||||
* @return {number[]}
|
|
||||||
*/
|
|
||||||
var dailyTemperatures = function(temperatures) {
|
var dailyTemperatures = function(temperatures) {
|
||||||
let n = temperatures.length;
|
const n = temperatures.length;
|
||||||
let res = new Array(n).fill(0);
|
const res = Array(n).fill(0);
|
||||||
let stack = []; // 递减栈:用于存储元素右面第一个比他大的元素下标
|
const stack = []; // 递增栈:用于存储元素右面第一个比他大的元素下标
|
||||||
stack.push(0);
|
stack.push(0);
|
||||||
for (let i = 1; i < n; i++) {
|
for (let i = 1; i < n; i++) {
|
||||||
// 栈顶元素
|
// 栈顶元素
|
||||||
let top = stack[stack.length - 1];
|
const top = stack[stack.length - 1];
|
||||||
if (temperatures[i] < temperatures[top]) {
|
if (temperatures[i] < temperatures[top]) {
|
||||||
stack.push(i);
|
stack.push(i);
|
||||||
} else if (temperatures[i] === temperatures[top]) {
|
} else if (temperatures[i] === temperatures[top]) {
|
||||||
stack.push(i);
|
stack.push(i);
|
||||||
} else {
|
} else {
|
||||||
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
|
while (stack.length && temperatures[i] > temperatures[stack[stack.length - 1]]) {
|
||||||
let top = stack.pop();
|
const top = stack.pop();
|
||||||
res[top] = i - top;
|
res[top] = i - top;
|
||||||
}
|
}
|
||||||
stack.push(i);
|
stack.push(i);
|
||||||
@ -327,6 +324,23 @@ var dailyTemperatures = function(temperatures) {
|
|||||||
}
|
}
|
||||||
return res;
|
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;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -250,11 +250,9 @@ func removeDuplicates(s string) string {
|
|||||||
|
|
||||||
javaScript:
|
javaScript:
|
||||||
|
|
||||||
|
法一:使用栈
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
|
||||||
* @param {string} s
|
|
||||||
* @return {string}
|
|
||||||
*/
|
|
||||||
var removeDuplicates = function(s) {
|
var removeDuplicates = function(s) {
|
||||||
const stack = [];
|
const stack = [];
|
||||||
for(const x of s) {
|
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:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
Reference in New Issue
Block a user