Skip to content

Commit 5bae992

Browse files
authored
Merge pull request chefyuan#20 from daluozha/main
leetcode 141、206、225,剑指offer 09 补充js版代码
2 parents 5c0f7ca + 46b5f5e commit 5bae992

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

animation-simulation/栈和队列/225.用队列实现栈.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
下面我们来看一下题目代码,也是很容易理解。
2020

21+
Java Code:
2122
```java
2223
class MyStack {
2324
//初始化队列
@@ -55,3 +56,34 @@ class MyStack {
5556

5657
```
5758

59+
JS Code:
60+
```javascript
61+
var MyStack = function() {
62+
this.queue = [];
63+
};
64+
65+
MyStack.prototype.push = function(x) {
66+
this.queue.push(x);
67+
if (this.queue.length > 1) {
68+
let i = this.queue.length - 1;
69+
while (i) {
70+
this.queue.push(this.queue.shift());
71+
i--;
72+
}
73+
}
74+
};
75+
76+
MyStack.prototype.pop = function() {
77+
return this.queue.shift();
78+
};
79+
80+
MyStack.prototype.top = function() {
81+
return this.empty() ? null : this.queue[0];
82+
83+
};
84+
85+
MyStack.prototype.empty = function() {
86+
return !this.queue.length;
87+
};
88+
```
89+

animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CQueue {
5858

5959
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
6060

61+
Java Code:
6162
```java
6263
class CQueue {
6364
//初始化两个栈
@@ -89,3 +90,24 @@ class CQueue {
8990
}
9091
```
9192

93+
JS Code:
94+
```javascript
95+
var CQueue = function() {
96+
this.stack1 = [];
97+
this.stack2 = [];
98+
};
99+
100+
CQueue.prototype.appendTail = function(value) {
101+
this.stack1.push(value);
102+
};
103+
104+
CQueue.prototype.deleteHead = function() {
105+
if (!this.stack2.length) {
106+
while(this.stack1.length) {
107+
this.stack2.push(this.stack1.pop());
108+
}
109+
}
110+
return this.stack2.pop() || -1;
111+
};
112+
```
113+

animation-simulation/链表篇/leetcode141环形链表.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
**题目代码**
4040

41+
Java Code:
4142
```java
4243
public class Solution {
4344
public boolean hasCycle(ListNode head) {
@@ -56,3 +57,18 @@ public class Solution {
5657
}
5758
```
5859

60+
JS Code:
61+
```javascript
62+
var hasCycle = function(head) {
63+
let fast = head;
64+
let slow = head;
65+
while (fast && fast.next) {
66+
fast = fast.next.next;
67+
slow = slow.next;
68+
if (fast === slow) {
69+
return true;
70+
}
71+
}
72+
return false;
73+
};
74+
```

animation-simulation/链表篇/leetcode206反转链表.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
3333

3434
我会对每个关键点进行注释,大家可以参考动图理解。
3535

36+
Java Code:
3637
```java
3738
class Solution {
3839
public ListNode reverseList(ListNode head) {
@@ -62,9 +63,27 @@ class Solution {
6263

6364
}
6465
```
66+
JS Code:
67+
```javascript
68+
var reverseList = function(head) {
69+
if(!head || !head.next) {
70+
return head;
71+
}
72+
let low = null;
73+
let pro = head;
74+
while (pro) {
75+
let temp = pro;
76+
pro = pro.next;
77+
temp.next = low;
78+
low = temp;
79+
}
80+
return low;
81+
};
82+
```
6583

6684
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
6785

86+
Java Code:
6887
```java
6988
class Solution {
7089
public ListNode reverseList(ListNode head) {
@@ -86,3 +105,16 @@ class Solution {
86105

87106
```
88107

108+
JS Code:
109+
```javascript
110+
var reverseList = function(head) {
111+
if (!head || !head.next) {
112+
return head;
113+
}
114+
let pro = reverseList(head.next);
115+
head.next.next = head;
116+
head.next = null;
117+
return pro;
118+
};
119+
```
120+

0 commit comments

Comments
 (0)