Skip to content

Commit 9fd63f4

Browse files
committed
2 parents ffe1810 + 3081abf commit 9fd63f4

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

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

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

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

21+
#### 题目代码
22+
2123
Java Code:
2224
```java
2325
class MyStack {
@@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
8789
};
8890
```
8991

92+
C++ Code:
93+
94+
```cpp
95+
class MyStack {
96+
queue <int> q;
97+
public:
98+
void push(int x) {
99+
q.push(x);
100+
for(int i = 1;i < q.size();i++){
101+
int val = q.front();
102+
q.push(val);
103+
q.pop();
104+
}
105+
}
106+
107+
int pop() {
108+
int val = q.front();
109+
q.pop();
110+
return val;
111+
}
112+
int top() {
113+
return q.front();
114+
}
115+
bool empty() {
116+
return q.empty();
117+
}
118+
};
119+
```
120+

animation-simulation/栈和队列/leetcode1047 删除字符串中的所有相邻重复项.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030

3131

32+
**题目代码**
3233

3334

3435

36+
Java Code:
3537

3638
```java
3739
class Solution {
@@ -67,3 +69,27 @@ class Solution {
6769
```
6870

6971
当然这个题目也可以用 set 来做,大家可以随意发挥
72+
73+
C++ Code:
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
string removeDuplicates(string S) {
79+
string str;
80+
if (S.empty() || S.size() == 1) {
81+
return S;
82+
}
83+
for (int i = 0; i<S.size(); i++) {
84+
if(str.empty() || S[i] != str.back()) {
85+
str.push_back(S[i]);
86+
}
87+
else {
88+
str.pop_back();
89+
}
90+
}
91+
return str;
92+
}
93+
};
94+
```
95+

animation-simulation/链表篇/leetcode328奇偶链表.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,19 @@ public:
8989
};
9090
```
9191

92+
JS Code:
93+
```javascript
94+
var oddEvenList = function(head) {
95+
if(!head || !head.next) return head;
96+
let odd = head, even = head.next, evenHead = even;
97+
while(odd.next && even.next){
98+
odd.next = even.next;
99+
odd = odd.next;
100+
even.next = odd.next;
101+
even = even.next;
102+
}
103+
odd.next = evenHead;
104+
return head;
105+
};
106+
```
107+

animation-simulation/链表篇/leetcode相交链表.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ public:
7979
};
8080
```
8181
82+
JS Code:
83+
```javascript
84+
var getIntersectionNode = function(headA, headB) {
85+
let tempa = headA, tempb = headB
86+
const map = new Map()
87+
while(tempa){
88+
map.set(tempa, 1)
89+
tempa = tempa.next
90+
}
91+
while(tempb){
92+
if(map.get(tempb))
93+
return tempb
94+
tempb = tempb.next
95+
}
96+
return tempb
97+
};
98+
```
99+
82100
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
83101

84102
下面我们直接看动图吧,特别直观,一下就可以搞懂。
@@ -128,6 +146,18 @@ public:
128146
};
129147
```
130148
149+
JS Code:
150+
```javascript
151+
var getIntersectionNode = function(headA, headB) {
152+
let tempa = headA, tempb = headB
153+
while(tempa !== tempb){
154+
tempa = tempa ? tempa.next : headB
155+
tempb = tempb ? tempb.next : headA
156+
}
157+
return tempa
158+
};
159+
```
160+
131161
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
132162

133163

animation-simulation/链表篇/剑指offer2倒数第k个节点.md renamed to animation-simulation/链表篇/剑指offer22倒数第k个节点.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ public:
9393
};
9494
```
9595
96+
JS Code:
97+
```javascript
98+
var getKthFromEnd = function(head, k) {
99+
if(!head) return head;
100+
let pro = head, after = head;
101+
for(let i = 0; i < k - 1; i++){
102+
pro = pro.next;
103+
}
104+
while(pro.next){
105+
pro = pro.next;
106+
after = after.next;
107+
}
108+
return after;
109+
};
110+
```

0 commit comments

Comments
 (0)