Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cebcb95

Browse files
update 7 leetcode
1 parent 14098ba commit cebcb95

7 files changed

+415
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @lc app=leetcode id=145 lang=javascript
3+
*
4+
* [145] Binary Tree Postorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* 迭代循环的方式
18+
* @param {TreeNode} root
19+
* @return {number[]}
20+
*/
21+
var postorderTraversal = function(root) {
22+
if (!root) {
23+
return [];
24+
}
25+
let queues = [];
26+
let res = [];
27+
let prev = null;
28+
29+
while (queues.length || root) {
30+
// 把左侧的全部推入
31+
while (root !== null) {
32+
queues.push(root);
33+
root = root.left;
34+
}
35+
36+
// 后序遍历,一定用到的是栈的思路
37+
root = queues.pop();
38+
// 遍历到末端了
39+
if (root.right == null || root.right == prev) {
40+
res.push(root.val);
41+
prev = root;
42+
root = null;
43+
} else {
44+
// 否则则继续添加入右子树
45+
queues.push(root);
46+
root = root.right;
47+
}
48+
}
49+
return res;
50+
};
51+
52+
/**
53+
* 递归的方式
54+
* @param {TreeNode} root
55+
* @return {number[]}
56+
*/
57+
var postorderTraversal2 = function(root) {
58+
let res = [];
59+
function walkTree(root) {
60+
if (!root) {
61+
return;
62+
}
63+
root.left && walkTree(root.left);
64+
root.right && walkTree(root.right);
65+
res.push(root.val);
66+
}
67+
walkTree(root);
68+
return res;
69+
};
70+
// @lc code=end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode id=1486 lang=javascript
3+
*
4+
* [1486] XOR Operation in an Array
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} n
10+
* @param {number} start
11+
* @return {number}
12+
*/
13+
var xorOperation = function(n, start) {
14+
let i = 0;
15+
let sum = start;
16+
while (i <= n - 1) {
17+
if (i !== 0) {
18+
sum = sum ^ (start + 2 * i);
19+
}
20+
i++;
21+
}
22+
return sum;
23+
};
24+
// @lc code=end

‎leetcode/202.happy-number.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=202 lang=javascript
3+
*
4+
* [202] Happy Number
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 双指针,龟兔赛跑
10+
* @param {number} n
11+
* @return {boolean}
12+
*/
13+
var isHappy = function(n) {
14+
// 根据规律分析可得,最终的值要么会进入一个循环,要么最终会返回 1
15+
function goNext(n) {
16+
let sum = 0;
17+
while (n > 0) {
18+
const d = n % 10;
19+
n = Math.floor(n / 10);
20+
sum += d * d;
21+
}
22+
return sum;
23+
}
24+
let slow = n;
25+
let fast = goNext(n);
26+
// 快慢指针查看是否进入循环
27+
while (slow !== fast && fast !== 1) {
28+
console.info('slow', slow);
29+
console.info('fast', fast);
30+
slow = goNext(slow);
31+
fast = goNext(goNext(fast));
32+
}
33+
return fast === 1;
34+
};
35+
isHappy(19);
36+
// @lc code=end

‎leetcode/283.move-zeroes.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @lc app=leetcode id=283 lang=javascript
3+
*
4+
* [283] Move Zeroes
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 双指针的思路
10+
* @param {number[]} nums
11+
* @return {void} Do not return anything, modify nums in-place instead.
12+
*/
13+
var moveZeroes = function(nums) {
14+
let left = right = 0;
15+
let len = nums.length;
16+
17+
while (right < len) {
18+
// 左指针仅在为 0 的情况下,与右边的数交换 ❌ 错的
19+
// 右指针在不为 0 的情况下,和左指针交换,这样保证了数都是相对的顺序没有变,而且在转换过程中, 0 自然去了右边
20+
if (nums[right] !== 0) {
21+
[nums[right], nums[left]] = [nums[left], nums[right]];
22+
left++;
23+
}
24+
// 右指针始终在遍历
25+
right++;
26+
}
27+
return nums;
28+
};
29+
30+
var moveZeroes2 = function(nums) {
31+
let i = 0;
32+
let sum = 0;
33+
let c = 0;
34+
35+
// 保存 0 的数量用来当终止条件
36+
nums.forEach((s) => {
37+
if (s === 0) {
38+
sum++;
39+
}
40+
});
41+
42+
// 是 0 就推到数组末尾去
43+
while (c < sum) {
44+
if (nums[i] === 0) {
45+
let p = nums.splice(i, 1);
46+
nums.push(p);
47+
c++;
48+
} else {
49+
i++;
50+
}
51+
}
52+
return nums;
53+
};
54+
// @lc code=end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode id=821 lang=javascript
3+
*
4+
* [821] Shortest Distance to a Character
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {character} c
11+
* @return {number[]}
12+
*/
13+
var shortestToChar = function(s, c) {
14+
let prev = s.length - 1;
15+
let res = [];
16+
let left = 0;
17+
18+
// 从左往右遍历,保存一份结果
19+
while (left < s.length) {
20+
if (s.charAt(left) === c) {
21+
prev = left;
22+
}
23+
res[left] = Math.abs(left - prev);
24+
left++;
25+
}
26+
27+
// 再从右往左遍历,保存另一份结果并比较
28+
let right = s.length - 1;
29+
while (right >= 0) {
30+
if (s.charAt(right) === c) {
31+
prev = right;
32+
}
33+
34+
res[right] = Math.min(Math.abs(right - prev), res[right]);
35+
right--;
36+
}
37+
return res;
38+
};
39+
// @lc code=end
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* @lc app=leetcode id=844 lang=javascript
3+
*
4+
* [844] Backspace String Compare
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* 暴力法,直接循环得出退格后的字符串再比较
10+
* @param {string} s
11+
* @param {string} t
12+
* @return {boolean}
13+
*/
14+
var backspaceCompare = function(s, t) {
15+
let left = s.length - 1;
16+
let right = t.length - 1;
17+
let skipS = 0;
18+
let skipT = 0;
19+
20+
while (left >= 0 || right >= 0) {
21+
// 通过算法取出当前需要比较的字符串索引
22+
while (left >= 0) {
23+
if (s.charAt(left) === '#') {
24+
skipS++;
25+
left--;
26+
} else if (skipS > 0) {
27+
skipS--;
28+
left--;
29+
} else {
30+
break;
31+
}
32+
}
33+
34+
while (right >= 0) {
35+
if (t.charAt(right) === '#') {
36+
skipT++;
37+
right--;
38+
} else if (skipT > 0) {
39+
skipT--;
40+
right--;
41+
} else {
42+
break;
43+
}
44+
}
45+
46+
// 直接比较对应的字符串是否相等
47+
if (left >= 0 && right >= 0) {
48+
if (s[left] !== t[right]) {
49+
return false;
50+
}
51+
} else {
52+
// 进入这个条件则说明两边长度不一致
53+
if (left >= 0 || right >= 0) {
54+
return false;
55+
}
56+
}
57+
58+
left--;
59+
right--;
60+
}
61+
62+
return true;
63+
};
64+
65+
/**
66+
* 暴力法,直接循环得出退格后的字符串再比较
67+
* @param {string} s
68+
* @param {string} t
69+
* @return {boolean}
70+
*/
71+
var backspaceCompare2 = function(s, t) {
72+
let left = s.length - 1;
73+
let right = t.length - 1;
74+
let leftVal = '';
75+
let rightVal = '';
76+
let backNum = 0;
77+
78+
while (left >= 0) {
79+
if (s[left] === '#') {
80+
backNum++;
81+
} else {
82+
if (backNum) {
83+
backNum--;
84+
} else {
85+
leftVal = `${s[left]}${leftVal}`;
86+
}
87+
}
88+
89+
left--;
90+
}
91+
backNum = 0;
92+
while (right >= 0) {
93+
if (t[right] === '#') {
94+
backNum++;
95+
} else {
96+
if (backNum) {
97+
backNum--;
98+
} else {
99+
rightVal = `${t[right]}${rightVal}`;
100+
}
101+
}
102+
103+
right--;
104+
}
105+
console.info(leftVal, rightVal);
106+
return leftVal === rightVal;
107+
};
108+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /