Merge pull request #20 from daluozha/main

leetcode 141、206、225,剑指offer 09 补充js版代码
This commit is contained in:
算法基地 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 ```java
class MyStack { 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/)去实现一下下面我们看代码 大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下下面我们看代码
Java Code:
```java ```java
class CQueue { 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 ```java
public class Solution { public class Solution {
public boolean hasCycle(ListNode head) { 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 ```java
class Solution { class Solution {
public ListNode reverseList(ListNode head) { 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 ```java
class Solution { class Solution {
public ListNode reverseList(ListNode head) { 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;
};
```