Skip to content

Commit d256f92

Browse files
author
luzhipeng
committed
feat: 更新三道候选题目
1 parent 810af2a commit d256f92

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

134.gas-station.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var canCompleteCircuit = function(gas, cost) {
4141

4242
for(let i = 0; i < n; i++) {
4343
total += gas[i];
44-
total -= cost[i]
44+
total -= cost[i];
4545

4646
remain += gas[i];
4747
remain -= cost[i];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @lc app=leetcode id=448 lang=javascript
3+
*
4+
* [448] Find All Numbers Disappeared in an Array
5+
*/
6+
/**
7+
* @param {number[]} nums
8+
* @return {number[]}
9+
*/
10+
var findDisappearedNumbers = function(nums) {
11+
const res = [];
12+
let cur = 0;
13+
for(let i = 0; i < nums.length; i++) {
14+
res[nums[i]] = true;
15+
}
16+
17+
for(let i = 0; i < nums.length; i++) {
18+
if (res[i + 1] === void 0) {
19+
res[cur++] = i + 1;
20+
}
21+
}
22+
23+
res.length = cur;
24+
25+
return res;
26+
};
27+

739.daily-temperatures.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @lc app=leetcode id=739 lang=javascript
3+
*
4+
* [739] Daily Temperatures
5+
*/
6+
/**
7+
* @param {number[]} T
8+
* @return {number[]}
9+
*/
10+
var dailyTemperatures = function(T) {
11+
// // 暴力 时间复杂度O(n^2), 空间复杂度O(1)
12+
// const res = [];
13+
// for(let i = 0; i < T.length; i++) {
14+
// res[i] = 0;
15+
// for(let j = i; j < T.length; j++) {
16+
// if (T[j] > T[i]) {
17+
// res[i] = j - i;
18+
// break;
19+
// }
20+
// }
21+
// }
22+
23+
// return res;
24+
25+
// 递增栈/递减栈
26+
// 这里我们需要用到递减栈
27+
// 时间复杂度O(n), 空间复杂度O(n)
28+
// 典型的空间换时间
29+
const stack = [];
30+
const res = [];
31+
32+
for (let i = 0; i < T.length; i++) {
33+
res[i] = 0;
34+
while (stack.length !== 0 && T[i] > T[stack[stack.length - 1]]) {
35+
const peek = stack.pop();
36+
res[peek] = i - peek;
37+
}
38+
stack.push(i);
39+
}
40+
41+
return res;
42+
};

0 commit comments

Comments
 (0)