File tree Expand file tree Collapse file tree 5 files changed +118
-0
lines changed Expand file tree Collapse file tree 5 files changed +118
-0
lines changed Original file line number Diff line number Diff line change 18
18
19
19
下面我们来看一下题目代码,也是很容易理解。
20
20
21
+ #### 题目代码
22
+
21
23
Java Code:
22
24
``` java
23
25
class MyStack {
@@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
87
89
};
88
90
```
89
91
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
+
Original file line number Diff line number Diff line change 29
29
30
30
31
31
32
+ ** 题目代码**
32
33
33
34
34
35
36
+ Java Code:
35
37
36
38
``` java
37
39
class Solution {
@@ -67,3 +69,27 @@ class Solution {
67
69
```
68
70
69
71
当然这个题目也可以用 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
+
Original file line number Diff line number Diff line change @@ -89,3 +89,19 @@ public:
89
89
};
90
90
```
91
91
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
+
Original file line number Diff line number Diff line change @@ -79,6 +79,24 @@ public:
79
79
};
80
80
```
81
81
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
+
82
100
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
83
101
84
102
下面我们直接看动图吧,特别直观,一下就可以搞懂。
@@ -128,6 +146,18 @@ public:
128
146
};
129
147
```
130
148
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
+
131
161
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
132
162
133
163
Original file line number Diff line number Diff line change @@ -93,3 +93,18 @@ public:
93
93
};
94
94
```
95
95
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
+ ```
You can’t perform that action at this time.
0 commit comments