Skip to content

Commit 879fffc

Browse files
committed
添加二进制题
1 parent 54ba14f commit 879fffc

File tree

2 files changed

+312
-4
lines changed

2 files changed

+312
-4
lines changed

数据结构篇/二进制.md

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
JS二进制基础知识: [二进制](https://wangdoc.com/javascript/operators/bit.html)
44

5-
##### 136.只出现一次的数字 [single-number](https://leetcode-cn.com/problems/single-number/)
5+
##### [136.只出现一次的数字](https://leetcode-cn.com/problems/single-number/)
66

77
给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
88

@@ -23,7 +23,7 @@ var singleNumber = function(nums) {
2323
};
2424
```
2525

26-
##### 137.只出现一次的数字 II [single-number-ii](https://leetcode-cn.com/problems/single-number-ii/)
26+
##### [137.只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/)
2727

2828
给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次。找出那个只出现了一次的元素。
2929

@@ -41,3 +41,119 @@ var singleNumber = function(nums) {
4141
};
4242
```
4343

44+
##### [260. 只出现一次的数字 III](https://leetcode-cn.com/problems/single-number-iii/)
45+
46+
```js
47+
/*
48+
* 执行用时:560 ms, 在所有 JavaScript 提交中击败了8.67%的用户 😂,indexof这么耗时
49+
* 内存消耗:38.4 MB, 在所有 JavaScript 提交中击败了91.67%的用户
50+
*/
51+
var singleNumber = function (nums) {
52+
const res = []
53+
nums.forEach(num => {
54+
if (nums.indexOf(num) === nums.lastIndexOf(num)) {
55+
res.push(num)
56+
}
57+
})
58+
return res
59+
};
60+
```
61+
62+
```js
63+
var singleNumber = function (nums) {
64+
let ret = 0;
65+
for (let n of nums) {
66+
ret ^= n;
67+
}
68+
let div = 1;
69+
while ((div & ret) === 0) {
70+
div <<= 1;
71+
}
72+
let a = 0, b = 0;
73+
for (let n of nums) {
74+
if ((div & n) !== 0) {
75+
a ^= n;
76+
} else {
77+
b ^= n;
78+
}
79+
}
80+
return [a, b];
81+
}
82+
```
83+
84+
##### [191. 位1的个数](https://leetcode-cn.com/problems/number-of-1-bits/)
85+
86+
```js
87+
var hammingWeight = function(n) {
88+
let res = 0
89+
while(n){
90+
n = n & (n-1)
91+
res++
92+
}
93+
return res
94+
};
95+
```
96+
97+
##### [338. 比特位计数](https://leetcode-cn.com/problems/counting-bits/)
98+
99+
给定一个非负整数 **num**。对于 **0 ≤ i ≤ num** 范围中的每个数字 **i** ,计算其二进制数中的 1 的数目并将它们作为数组返回。
100+
101+
```js
102+
var countBits = function (num) {
103+
const res = []
104+
for (let i = 0; i <= num; i++) {
105+
let n = i
106+
let times = 0
107+
while (n) {
108+
n = n & (n - 1)
109+
times++
110+
}
111+
res.push(times)
112+
}
113+
return res
114+
};
115+
```
116+
117+
##### [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/)
118+
119+
颠倒给定的 32 位无符号整数的二进制位。
120+
121+
```js
122+
var reverseBits = function (n) {
123+
let rev = 0;
124+
for (let i = 0; i < 32 && n > 0; ++i) {
125+
rev |= (n & 1) << (31 - i);
126+
n >>>= 1;
127+
}
128+
return rev >>> 0;
129+
};
130+
```
131+
132+
##### [201. 数字范围按位与](https://leetcode-cn.com/problems/bitwise-and-of-numbers-range/)
133+
134+
给你两个整数 `left``right` ,表示区间 `[left, right]` ,返回此区间内所有数字 **按位与** 的结果(包含 `left``right` 端点)。
135+
136+
```js
137+
var rangeBitwiseAnd = function (left, right) {
138+
let count = 0
139+
while (left !== right) {
140+
left >>= 1
141+
right >>= 1
142+
count++
143+
}
144+
return left << count
145+
};
146+
```
147+
148+
## 总结
149+
150+
151+
152+
## 练习
153+
154+
- [single-number](https://leetcode-cn.com/problems/single-number/)
155+
- [single-number-ii](https://leetcode-cn.com/problems/single-number-ii/)
156+
- [single-number-iii](https://leetcode-cn.com/problems/single-number-iii/)
157+
- [number-of-1-bits](https://leetcode-cn.com/problems/number-of-1-bits/)
158+
- [counting-bits](https://leetcode-cn.com/problems/counting-bits/)
159+
- [reverse-bits](https://leetcode-cn.com/problems/reverse-bits/)

数据结构篇/栈和队列.md

Lines changed: 194 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var decodeString = function (s) {
133133
##### [94. 二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
134134

135135
```js
136-
var inorderTraversal = function (root) { //递归写法
136+
var inorderTraversal = function (root) { //递归
137137
if (root === null) {
138138
return []
139139
}
@@ -151,8 +151,200 @@ var inorderTraversal = function (root) { //递归写法
151151
```
152152

153153
```js
154-
154+
var inorderTraversal = function (root) {
155+
const stack = []
156+
const res = []
157+
let current = root
158+
while (stack.length > 0 || current) {
159+
while (current) {
160+
stack.push(current)
161+
current = current.left
162+
}
163+
current = stack.pop()
164+
res.push(current.val)
165+
current = current.right
166+
}
167+
return res
168+
};
155169
```
156170

157171
##### [133.克隆图](https://leetcode-cn.com/problems/clone-graph/)
158172

173+
给你无向 **[连通](https://baike.baidu.com/item/连通图/6460995?fr=aladdin)** 图中一个节点的引用,请你返回该图的 [**深拷贝**](https://baike.baidu.com/item/深拷贝/22785317?fr=aladdin)(克隆)。
174+
175+
```js
176+
var cloneGraph = function (node) {
177+
let visited = new Map();
178+
return (function clone(node, visited) {
179+
if (!node) {
180+
return null
181+
}
182+
if (visited.has(node)) {
183+
return visited.get(node)
184+
}
185+
const newNode = new Node(node.val)
186+
visited.set(node, newNode)
187+
for (let i = 0; i < node.neighbors.length; i++) {
188+
newNode.neighbors[i] = clone(node.neighbors[i], visited)
189+
}
190+
return newNode
191+
})(node, visited)
192+
};
193+
```
194+
195+
##### [200. 岛屿数量](https://leetcode-cn.com/problems/number-of-islands/)
196+
197+
给你一个由 `'1'`(陆地)和 `'0'`(水)组成的的二维网格,请你计算网格中岛屿的数量。
198+
199+
```js
200+
var numIslands = function (grid) {
201+
const nr = grid.length
202+
const nc = grid[0].length
203+
let resNum = 0
204+
for (let i = 0; i < nr; i++) {
205+
for (let j = 0; j < nc; j++) {
206+
if (grid[i][j] === '1') {
207+
resNum++
208+
dfs(grid, i, j)
209+
}
210+
}
211+
}
212+
return resNum
213+
};
214+
215+
const dfs = (grid, row, col) => {
216+
const gr = grid.length
217+
const gc = grid[0].length
218+
if (row < 0 || col < 0 || row >= gr || col >= gc || grid[row][col] === '0') {
219+
return 0
220+
}
221+
grid[row][col] = '0'
222+
dfs(grid, row + 1, col)
223+
dfs(grid, row - 1, col)
224+
dfs(grid, row, col + 1)
225+
dfs(grid, row, col - 1)
226+
}
227+
```
228+
229+
[84. 柱状图中最大的矩形](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/)
230+
231+
```js
232+
233+
```
234+
235+
### Queue 队列
236+
237+
##### [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks/)
238+
239+
```js
240+
/**
241+
* Initialize your data structure here.
242+
*/
243+
var MyQueue = function () {
244+
this.stack1 = []
245+
this.stack2 = []
246+
};
247+
248+
/**
249+
* Push element x to the back of queue.
250+
* @param {number} x
251+
* @return {void}
252+
*/
253+
MyQueue.prototype.push = function (x) {
254+
this.stack1.push(x)
255+
};
256+
257+
/**
258+
* Removes the element from in front of queue and returns that element.
259+
* @return {number}
260+
*/
261+
MyQueue.prototype.pop = function () {
262+
if (this.stack2.length > 0) {
263+
return this.stack2.pop()
264+
} else {
265+
while (this.stack1.length > 0) {
266+
this.stack2.push(this.stack1.pop())
267+
}
268+
return this.stack2.pop()
269+
}
270+
};
271+
272+
/**
273+
* Get the front element.
274+
* @return {number}
275+
*/
276+
MyQueue.prototype.peek = function () {
277+
if (this.stack2.length > 0) {
278+
return this.stack2[this.stack2.length - 1]
279+
} else {
280+
while (this.stack1.length > 0) {
281+
this.stack2.push(this.stack1.pop())
282+
}
283+
return this.stack2[this.stack2.length - 1]
284+
}
285+
};
286+
287+
/**
288+
* Returns whether the queue is empty.
289+
* @return {boolean}
290+
*/
291+
MyQueue.prototype.empty = function () {
292+
if (this.stack1.length === 0 && this.stack2.length === 0) {
293+
return true
294+
}
295+
return false
296+
};
297+
```
298+
299+
##### [542. 01 矩阵](https://leetcode-cn.com/problems/01-matrix/)
300+
301+
```js
302+
var updateMatrix = function (matrix) {
303+
const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]]
304+
const m = matrix.length, n = matrix[0].length
305+
const dist = new Array(m).fill(new Array(n).fill(0))
306+
const seen = new Array(m).fill(new Array(n).fill(false))
307+
const queue = []
308+
for (let i = 0; i < m; i++) {
309+
for (let j = 0; j < n; j++) {
310+
if (matrix[i][j] === 0) {
311+
queue.push([i, j])
312+
seen[i][j] = true
313+
}
314+
}
315+
}
316+
while (queue.length > 0) {
317+
const [i, j] = queue.shift()
318+
for (let d = 0; d < 4; d++) {
319+
const ni = i + dirs[d][0]
320+
const nj = j + dirs[d][1]
321+
if (ni >= 0 && ni < m && nj >= 0 && nj < n && !seen[ni][nj]) {
322+
dist[ni][nj] = dist[i][j] + 1
323+
queue.push([ni, nj])
324+
seen[ni][nj] = true
325+
}
326+
}
327+
}
328+
return dist
329+
};
330+
```
331+
332+
## 总结
333+
334+
- 熟悉栈的使用场景
335+
- 后入先出,保存临时值
336+
- 利用栈 DFS 深度搜索
337+
- 熟悉队列的使用场景
338+
- 利用队列 BFS 广度搜索
339+
340+
## 练习
341+
342+
- [min-stack](https://leetcode-cn.com/problems/min-stack/)
343+
- [evaluate-reverse-polish-notation](https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/)
344+
- [decode-string](https://leetcode-cn.com/problems/decode-string/)
345+
- [binary-tree-inorder-traversal](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)
346+
- [clone-graph](https://leetcode-cn.com/problems/clone-graph/)
347+
- [number-of-islands](https://leetcode-cn.com/problems/number-of-islands/)
348+
- [largest-rectangle-in-histogram](https://leetcode-cn.com/problems/largest-rectangle-in-histogram/)
349+
- [implement-queue-using-stacks](https://leetcode-cn.com/problems/implement-queue-using-stacks/)
350+
- [01-matrix](https://leetcode-cn.com/problems/01-matrix/)

0 commit comments

Comments
 (0)