Skip to content

Commit 942bd65

Browse files
2 parents d975db2 + c87d709 commit 942bd65

24 files changed

+1174
-24
lines changed

problems/0019.删除链表的倒数第N个节点.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,28 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
135135
}
136136
```
137137

138+
JavaScript:
139+
140+
```js
141+
/**
142+
* @param {ListNode} head
143+
* @param {number} n
144+
* @return {ListNode}
145+
*/
146+
var removeNthFromEnd = function(head, n) {
147+
let ret = new ListNode(0, head),
148+
slow = fast = ret;
149+
while(n--) fast = fast.next;
150+
if(!fast) return ret.next;
151+
while (fast.next) {
152+
fast = fast.next;
153+
slow = slow.next
154+
};
155+
slow.next = slow.next.next;
156+
return ret.next;
157+
};
158+
```
159+
138160
-----------------------
139161
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
140162
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0045.跳跃游戏II.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,26 @@ class Solution:
193193
```
194194

195195
Go:
196+
```Go
197+
func jump(nums []int) int {
198+
dp:=make([]int ,len(nums))
199+
dp[0]=0
200+
201+
for i:=1;i<len(nums);i++{
202+
dp[i]=i
203+
for j:=0;j<i;j++{
204+
if nums[j]+j>i{
205+
dp[i]=min(dp[j]+1,dp[i])
206+
}
207+
}
208+
}
209+
return dp[len(nums)-1]
210+
}
211+
/*
212+
dp[i]表示从起点到当前位置的最小跳跃次数
213+
dp[i]=min(dp[j]+1,dp[i]) 表示从j位置用一步跳跃到当前位置,这个j位置可能有很多个,却最小一个就可以
214+
*/
215+
```
196216

197217

198218

problems/0055.跳跃游戏.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ class Solution:
122122
```
123123

124124
Go:
125+
```Go
126+
func canJUmp(nums []int) bool {
127+
if len(nums)<=1{
128+
return true
129+
}
130+
dp:=make([]bool,len(nums))
131+
dp[0]=true
132+
for i:=1;i<len(nums);i++{
133+
for j:=i-1;j>=0;j--{
134+
if dp[j]&&nums[j]+j>=i{
135+
dp[i]=true
136+
break
137+
}
138+
}
139+
}
140+
return dp[len(nums)-1]
141+
}
142+
```
125143

126144

127145

problems/0077.组合.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,45 @@ class Solution {
370370

371371

372372
Python:
373-
374-
373+
```python
374+
class Solution:
375+
result: List[List[int]] = []
376+
path: List[int] = []
377+
def combine(self, n: int, k: int) -> List[List[int]]:
378+
self.result = []
379+
self.combineHelper(n, k, 1)
380+
return self.result
381+
382+
def combineHelper(self, n: int, k: int, startIndex: int):
383+
if (l := len(self.path)) == k:
384+
self.result.append(self.path.copy())
385+
return
386+
for i in range(startIndex, n - (k - l) + 2):
387+
self.path.append(i)
388+
self.combineHelper(n, k, i + 1)
389+
self.path.pop()
390+
```
391+
javascript
392+
```javascript
393+
let result = []
394+
let path = []
395+
var combine = function(n, k) {
396+
result = []
397+
combineHelper(n, k, 1)
398+
return result
399+
};
400+
const combineHelper = (n, k, startIndex) => {
401+
if (path.length === k) {
402+
result.push([...path])
403+
return
404+
}
405+
for (let i = startIndex; i <= n - (k - path.length) + 1; ++i) {
406+
path.push(i)
407+
combineHelper(n, k, i + 1)
408+
path.pop()
409+
}
410+
}
411+
```
375412
Go:
376413
```Go
377414
var res [][]int

problems/0098.验证二叉搜索树.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,26 @@ class Solution {
336336
```
337337

338338
Python:
339-
340-
339+
```python
340+
# Definition for a binary tree node.
341+
# class TreeNode:
342+
# def __init__(self, val=0, left=None, right=None):
343+
# self.val = val
344+
# self.left = left
345+
# self.right = right
346+
//递归法
347+
class Solution:
348+
def isValidBST(self, root: TreeNode) -> bool:
349+
res = [] //把二叉搜索树按中序遍历写成list
350+
def buildalist(root):
351+
if not root: return
352+
buildalist(root.left) //
353+
res.append(root.val) //
354+
buildalist(root.right) //
355+
return res
356+
buildalist(root)
357+
return res == sorted(res) and len(set(res)) == len(res) //检查list里的数有没有重复元素,以及是否按从小到大排列
358+
```
341359
Go:
342360
```Go
343361
import "math"
@@ -365,4 +383,4 @@ func isBST(root *TreeNode, min, max int) bool {
365383
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
366384
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
367385
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
368-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
386+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0111.二叉树的最小深度.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,53 @@ class Solution:
302302
Go:
303303

304304

305+
JavaScript:
306+
307+
递归法:
308+
309+
```javascript
310+
/**
311+
* @param {TreeNode} root
312+
* @return {number}
313+
*/
314+
var minDepth1 = function(root) {
315+
if(!root) return 0;
316+
// 到叶子节点 返回 1
317+
if(!root.left && !root.right) return 1;
318+
// 只有右节点时 递归右节点
319+
if(!root.left) return 1 + minDepth(root.right);、
320+
// 只有左节点时 递归左节点
321+
if(!root.right) return 1 + minDepth(root.left);
322+
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
323+
};
324+
```
325+
326+
迭代法:
327+
328+
```javascript
329+
/**
330+
* @param {TreeNode} root
331+
* @return {number}
332+
*/
333+
var minDepth = function(root) {
334+
if(!root) return 0;
335+
const queue = [root];
336+
let dep = 0;
337+
while(true) {
338+
let size = queue.length;
339+
dep++;
340+
while(size--){
341+
const node = queue.shift();
342+
// 到第一个叶子节点 返回 当前深度
343+
if(!node.left && !node.right) return dep;
344+
node.left && queue.push(node.left);
345+
node.right && queue.push(node.right);
346+
}
347+
}
348+
};
349+
```
350+
351+
305352

306353

307354
-----------------------

problems/0139.单词拆分.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,30 @@ Python:
254254

255255

256256
Go:
257-
257+
```Go
258+
func wordBreak(s string,wordDict []string) bool {
259+
wordDictSet:=make(map[string]bool)
260+
for _,w:=range wordDict{
261+
wordDictSet[w]=true
262+
}
263+
dp:=make([]bool,len(s)+1)
264+
dp[0]=true
265+
for i:=1;i<=len(s);i++{
266+
for j:=0;j<i;j++{
267+
if dp[j]&& wordDictSet[s[j:i]]{
268+
dp[i]=true
269+
break
270+
}
271+
}
272+
}
273+
return dp[len(s)]
274+
}
275+
```
258276

259277

260278

261279
-----------------------
262280
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
263281
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
264282
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
265-
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
283+
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>

problems/0142.环形链表II.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,50 @@ Go:
258258
}
259259
```
260260

261+
javaScript
262+
263+
```js
264+
// 两种循环实现方式
265+
/**
266+
* @param {ListNode} head
267+
* @return {ListNode}
268+
*/
269+
// 先判断是否是环形链表
270+
var detectCycle = function(head) {
271+
if(!head || !head.next) return null;
272+
let slow =head.next, fast = head.next.next;
273+
while(fast && fast.next && fast!== slow) {
274+
slow = slow.next;
275+
fast = fast.next.next;
276+
}
277+
if(!fast || !fast.next ) return null;
278+
slow = head;
279+
while (fast !== slow) {
280+
slow = slow.next;
281+
fast = fast.next;
282+
}
283+
return slow;
284+
};
285+
286+
var detectCycle = function(head) {
287+
if(!head || !head.next) return null;
288+
let slow =head.next, fast = head.next.next;
289+
while(fast && fast.next) {
290+
slow = slow.next;
291+
fast = fast.next.next;
292+
if(fast == slow) {
293+
slow = head;
294+
while (fast !== slow) {
295+
slow = slow.next;
296+
fast = fast.next;
297+
}
298+
return slow;
299+
}
300+
}
301+
return null;
302+
};
303+
```
304+
261305

262306
-----------------------
263307
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0202.快乐数.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,40 @@ Python:
112112

113113
Go:
114114

115+
javaScript:
116+
117+
```js
118+
function getN(n) {
119+
if (n == 1 || n == 0) return n;
120+
let res = 0;
121+
while (n) {
122+
res += (n % 10) * (n % 10);
123+
n = parseInt(n / 10);
124+
}
125+
return res;
126+
}
127+
128+
var isHappy = function(n) {
129+
const sumSet = new Set();
130+
while (n != 1 && !sumSet.has(n)) {
131+
sumSet.add(n);
132+
n = getN(n);
133+
}
134+
return n == 1;
135+
};
136+
137+
// 使用环形链表的思想 说明出现闭环 退出循环
138+
var isHappy = function(n) {
139+
if (getN(n) == 1) return true;
140+
let a = getN(n), b = getN(getN(n));
141+
// 如果 a === b
142+
while (b !== 1 && getN(b) !== 1 && a !== b) {
143+
a = getN(a);
144+
b = getN(getN(b));
145+
}
146+
return b === 1 || getN(b) === 1 ;
147+
};
148+
```
115149

116150

117151

0 commit comments

Comments
 (0)