mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-12-27 21:06:17 +00:00
Merge pull request #20 from daluozha/main
leetcode 141、206、225,剑指offer 09 补充js版代码
This commit is contained in:
commit
5bae992b9a
@ -18,6 +18,7 @@
|
||||
|
||||
下面我们来看一下题目代码,也是很容易理解。
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class MyStack {
|
||||
//初始化队列
|
||||
@ -55,3 +56,34 @@ class MyStack {
|
||||
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var MyStack = function() {
|
||||
this.queue = [];
|
||||
};
|
||||
|
||||
MyStack.prototype.push = function(x) {
|
||||
this.queue.push(x);
|
||||
if (this.queue.length > 1) {
|
||||
let i = this.queue.length - 1;
|
||||
while (i) {
|
||||
this.queue.push(this.queue.shift());
|
||||
i--;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
MyStack.prototype.pop = function() {
|
||||
return this.queue.shift();
|
||||
};
|
||||
|
||||
MyStack.prototype.top = function() {
|
||||
return this.empty() ? null : this.queue[0];
|
||||
|
||||
};
|
||||
|
||||
MyStack.prototype.empty = function() {
|
||||
return !this.queue.length;
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -58,6 +58,7 @@ class CQueue {
|
||||
|
||||
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class CQueue {
|
||||
//初始化两个栈
|
||||
@ -89,3 +90,24 @@ class CQueue {
|
||||
}
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var CQueue = function() {
|
||||
this.stack1 = [];
|
||||
this.stack2 = [];
|
||||
};
|
||||
|
||||
CQueue.prototype.appendTail = function(value) {
|
||||
this.stack1.push(value);
|
||||
};
|
||||
|
||||
CQueue.prototype.deleteHead = function() {
|
||||
if (!this.stack2.length) {
|
||||
while(this.stack1.length) {
|
||||
this.stack2.push(this.stack1.pop());
|
||||
}
|
||||
}
|
||||
return this.stack2.pop() || -1;
|
||||
};
|
||||
```
|
||||
|
||||
|
@ -38,6 +38,7 @@
|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
public class Solution {
|
||||
public boolean hasCycle(ListNode head) {
|
||||
@ -56,3 +57,18 @@ public class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var hasCycle = function(head) {
|
||||
let fast = head;
|
||||
let slow = head;
|
||||
while (fast && fast.next) {
|
||||
fast = fast.next.next;
|
||||
slow = slow.next;
|
||||
if (fast === slow) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
|
||||
|
||||
我会对每个关键点进行注释,大家可以参考动图理解。
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
@ -62,9 +63,27 @@ class Solution {
|
||||
|
||||
}
|
||||
```
|
||||
JS Code:
|
||||
```javascript
|
||||
var reverseList = function(head) {
|
||||
if(!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
let low = null;
|
||||
let pro = head;
|
||||
while (pro) {
|
||||
let temp = pro;
|
||||
pro = pro.next;
|
||||
temp.next = low;
|
||||
low = temp;
|
||||
}
|
||||
return low;
|
||||
};
|
||||
```
|
||||
|
||||
上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class Solution {
|
||||
public ListNode reverseList(ListNode head) {
|
||||
@ -86,3 +105,16 @@ class Solution {
|
||||
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var reverseList = function(head) {
|
||||
if (!head || !head.next) {
|
||||
return head;
|
||||
}
|
||||
let pro = reverseList(head.next);
|
||||
head.next.next = head;
|
||||
head.next = null;
|
||||
return pro;
|
||||
};
|
||||
```
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user