Merge pull request #20 from daluozha/main

leetcode 141、206、225,剑指offer 09 补充js版代码
pull/21/head
算法基地 2021-04-27 19:00:29 +08:00 committed by GitHub
commit 5bae992b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 0 deletions

View File

@ -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;
};
```

View File

@ -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;
};
```

View File

@ -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;
};
```

View File

@ -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;
};
```