Merge branch 'main' of https://github.com/chefyuan/algorithm-base
commit
9fd63f4a6b
|
@ -18,6 +18,8 @@
|
|||
|
||||
下面我们来看一下题目代码,也是很容易理解。
|
||||
|
||||
#### 题目代码
|
||||
|
||||
Java Code:
|
||||
```java
|
||||
class MyStack {
|
||||
|
@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
|
|||
};
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class MyStack {
|
||||
queue <int> q;
|
||||
public:
|
||||
void push(int x) {
|
||||
q.push(x);
|
||||
for(int i = 1;i < q.size();i++){
|
||||
int val = q.front();
|
||||
q.push(val);
|
||||
q.pop();
|
||||
}
|
||||
}
|
||||
|
||||
int pop() {
|
||||
int val = q.front();
|
||||
q.pop();
|
||||
return val;
|
||||
}
|
||||
int top() {
|
||||
return q.front();
|
||||
}
|
||||
bool empty() {
|
||||
return q.empty();
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
|
|
@ -29,9 +29,11 @@
|
|||
|
||||
|
||||
|
||||
**题目代码**
|
||||
|
||||
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
|
@ -67,3 +69,27 @@ class Solution {
|
|||
```
|
||||
|
||||
当然这个题目也可以用 set 来做,大家可以随意发挥
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
string removeDuplicates(string S) {
|
||||
string str;
|
||||
if (S.empty() || S.size() == 1) {
|
||||
return S;
|
||||
}
|
||||
for (int i = 0; i<S.size(); i++) {
|
||||
if(str.empty() || S[i] != str.back()) {
|
||||
str.push_back(S[i]);
|
||||
}
|
||||
else {
|
||||
str.pop_back();
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
|
|
@ -89,3 +89,19 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var oddEvenList = function(head) {
|
||||
if(!head || !head.next) return head;
|
||||
let odd = head, even = head.next, evenHead = even;
|
||||
while(odd.next && even.next){
|
||||
odd.next = even.next;
|
||||
odd = odd.next;
|
||||
even.next = odd.next;
|
||||
even = even.next;
|
||||
}
|
||||
odd.next = evenHead;
|
||||
return head;
|
||||
};
|
||||
```
|
||||
|
||||
|
|
|
@ -79,6 +79,24 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var getIntersectionNode = function(headA, headB) {
|
||||
let tempa = headA, tempb = headB
|
||||
const map = new Map()
|
||||
while(tempa){
|
||||
map.set(tempa, 1)
|
||||
tempa = tempa.next
|
||||
}
|
||||
while(tempb){
|
||||
if(map.get(tempb))
|
||||
return tempb
|
||||
tempb = tempb.next
|
||||
}
|
||||
return tempb
|
||||
};
|
||||
```
|
||||
|
||||
下面这个方法比较巧妙,不是特别容易想到,大家可以自己实现一下,这个方法也是利用我们的双指针思想。
|
||||
|
||||
下面我们直接看动图吧,特别直观,一下就可以搞懂。
|
||||
|
@ -128,6 +146,18 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var getIntersectionNode = function(headA, headB) {
|
||||
let tempa = headA, tempb = headB
|
||||
while(tempa !== tempb){
|
||||
tempa = tempa ? tempa.next : headB
|
||||
tempb = tempb ? tempb.next : headA
|
||||
}
|
||||
return tempa
|
||||
};
|
||||
```
|
||||
|
||||
好啦,链表的题目就结束啦,希望大家能有所收获,下周就要更新新的题型啦,继续坚持,肯定会有收获的。
|
||||
|
||||
|
||||
|
|
|
@ -93,3 +93,18 @@ public:
|
|||
};
|
||||
```
|
||||
|
||||
JS Code:
|
||||
```javascript
|
||||
var getKthFromEnd = function(head, k) {
|
||||
if(!head) return head;
|
||||
let pro = head, after = head;
|
||||
for(let i = 0; i < k - 1; i++){
|
||||
pro = pro.next;
|
||||
}
|
||||
while(pro.next){
|
||||
pro = pro.next;
|
||||
after = after.next;
|
||||
}
|
||||
return after;
|
||||
};
|
||||
```
|
Loading…
Reference in New Issue