Skip to content

Commit 5ad5915

Browse files
author
lucifer
committed
feat(ml): cpp
1 parent 6044e75 commit 5ad5915

7 files changed

+188
-21
lines changed

problems/142.Linked-List-Cycle-II.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ return fast
111111

112112
### 代码
113113

114-
- 语言支持: JS, Go, PHP
114+
- 语言支持: JS, Go, PHP, CPP
115115

116116
JS Code:
117117

@@ -209,6 +209,28 @@ class Solution
209209
}
210210
```
211211

212+
CPP Code:
213+
214+
```cpp
215+
class Solution {
216+
public:
217+
ListNode *detectCycle(ListNode *head) {
218+
if (!head) return NULL;
219+
auto p = head, q = head;
220+
while (p && p->next) {
221+
p = p->next->next;
222+
q = q->next;
223+
if (p == q) break;
224+
}
225+
if (!p || !p->next) return NULL;
226+
p = head;
227+
for (; p != q; p = p->next, q = q->next);
228+
return p;
229+
}
230+
};
231+
232+
```
233+
212234
**复杂度分析**
213235
214236
- 时间复杂度:$O(N)$

problems/145.binary-tree-postorder-traversal.md

+31
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
6262

6363
## 代码
6464

65+
代码支持:JS, CPP
66+
67+
JS Code:
68+
6569
```js
6670
/**
6771
* Definition for a binary tree node.
@@ -111,6 +115,33 @@ var postorderTraversal = function (root) {
111115
};
112116
```
113117

118+
CPP Code:
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<int> postorderTraversal(TreeNode* root) {
124+
vector<int> ans;
125+
stack<TreeNode*> s;
126+
TreeNode *prev = NULL;
127+
while (root || s.size()) {
128+
while (root) {
129+
s.push(root);
130+
root = root->left;
131+
}
132+
root = s.top();
133+
if (!root->right || root->right == prev) {
134+
ans.push_back(root->val);
135+
s.pop();
136+
prev = root;
137+
root = NULL;
138+
} else root = root->right;
139+
}
140+
return ans;
141+
}
142+
};
143+
```
144+
114145
**复杂度分析**
115146
116147
- 时间复杂度:$O(N)$

problems/146.lru-cache.md

+37-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ lRUCache.get(3); // 返回 3
3636
lRUCache.get(4); // 返回 4
3737
```
3838

39-
### 思路: 哈希法
39+
### 思路
4040

4141
1. 确定需要使用的数据结构
4242
1. 根据题目要求,存储的数据需要保证顺序关系(逻辑层面) ===> 使用数组,链表等保证顺序关系
@@ -66,7 +66,7 @@ lRUCache.get(4); // 返回 4
6666
2. 返回该节点的值
6767
2. 节点不存在, 返回 null
6868

69-
- 伪代码
69+
伪代码如下:
7070

7171
```js
7272
var LRUCache = function(capacity) {
@@ -103,7 +103,9 @@ function put (key, value) {
103103
};
104104
```
105105

106-
- 语言支持: JS, Go, PHP
106+
## 代码
107+
108+
语言支持: JS, Go, PHP, CPP
107109

108110
JS Code:
109111

@@ -375,6 +377,38 @@ class DoubleList
375377
}
376378
```
377379

380+
CPP Code:
381+
382+
```cpp
383+
class LRUCache {
384+
private:
385+
list<pair<int, int>> data;
386+
unordered_map<int, list<pair<int, int>>::iterator> m;
387+
int capacity;
388+
public:
389+
LRUCache(int capacity) : capacity(capacity) {}
390+
391+
int get(int key) {
392+
if (!m.count(key)) return -1;
393+
data.splice(data.begin(), data, m[key]);
394+
m[key] = data.begin();
395+
return data.front().second;
396+
}
397+
398+
void put(int key, int value) {
399+
if (get(key) == -1) {
400+
if (data.size() == capacity) {
401+
auto p = data.back();
402+
m.erase(p.first);
403+
data.pop_back();
404+
}
405+
data.emplace_front(key, value);
406+
m[key] = data.begin();
407+
} else data.front().second = value;
408+
}
409+
};
410+
```
411+
378412
**复杂度分析**
379413
380414
- 时间复杂度:$O(1)$

problems/147.insertion-sort-list.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class Solution:
147147

148148
## 代码
149149

150-
代码支持:Python3,Java,JS
150+
代码支持:Python3,Java,JS, CPP
151151

152152
Python Code:
153153

@@ -209,6 +209,26 @@ var insertionSortList = function (head) {
209209
};
210210
```
211211

212+
CPP Code:
213+
214+
```cpp
215+
class Solution {
216+
public:
217+
ListNode* insertionSortList(ListNode* head) {
218+
ListNode dummy, *p;
219+
while (head) {
220+
auto *n = head;
221+
head = head->next;
222+
p = &dummy;
223+
while (p->next && p->next->val < n->val) p = p->next;
224+
n->next = p->next;
225+
p->next = n;
226+
}
227+
return dummy.next;
228+
}
229+
};
230+
```
231+
212232
**复杂度分析**
213233
214234
- 时间复杂度:$O(N^2)$,其中 N 为链表长度。

problems/150.evaluate-reverse-polish-notation.md

+36-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
2-
31
## 题目地址(150. 逆波兰表达式求值)
2+
43
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
54

65
## 题目描述
@@ -32,7 +31,7 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
3231
3332
输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
3433
输出: 22
35-
解释:
34+
解释:
3635
该算式转化为常见的中缀算术表达式为:
3736
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
3837
= ((10 * (6 / (12 * -11))) + 17) + 5
@@ -66,11 +65,12 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
6665
- 腾讯
6766

6867
## 思路
68+
6969
逆波兰表达式又叫做后缀表达式。在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为`中缀表示`
7070

71-
波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示表达式的方法,按此方法,每一运算符都置于其运算对象之后,故称为`后缀表示`
71+
波兰逻辑学家 J.Lukasiewicz 于 1929 年提出了另一种表示表达式的方法,按此方法,每一运算符都置于其运算对象之后,故称为`后缀表示`
7272

73-
> 逆波兰表达式是一种十分有用的表达式,它将复杂表达式转换为可以依靠简单的操作得到计算结果的表达式。例如(a+b)*(c+d)转换为ab+cd+*
73+
> 逆波兰表达式是一种十分有用的表达式,它将复杂表达式转换为可以依靠简单的操作得到计算结果的表达式。例如(a+b)_(c+d)转换为 ab+cd+_
7474
7575
思路就是:
7676

@@ -84,15 +84,15 @@ https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
8484

8585
1. 栈的基本用法
8686

87-
2. 如果你用的是JS的话,需要注意/ 和 其他很多语言是不一样的
87+
2. 如果你用的是 JS 的话,需要注意/ 和 其他很多语言是不一样的
8888

89-
3. 如果你用的是JS的话,需要先将字符串转化为数字。否则有很多意想不到的结果
89+
3. 如果你用的是 JS 的话,需要先将字符串转化为数字。否则有很多意想不到的结果
9090

9191
4. 操作符的顺序应该是 先出栈的是第二位,后出栈的是第一位。 这在不符合交换律的操作中很重要, 比如减法和除法。
9292

9393
## 代码
9494

95-
代码支持:JS,Python,Java
95+
代码支持:JS,Python,Java, CPP
9696

9797
JS Code:
9898

@@ -101,7 +101,7 @@ JS Code:
101101
* @param {string[]} tokens
102102
* @return {number}
103103
*/
104-
var evalRPN = function(tokens) {
104+
var evalRPN = function (tokens) {
105105
// 这种算法的前提是 tokens是有效的,
106106
// 当然这由算法来保证
107107
const stack = [];
@@ -121,7 +121,7 @@ var evalRPN = function(tokens) {
121121
if (token === "*") {
122122
stack.push(b * a);
123123
} else if (token === "/") {
124-
stack.push(b / a >> 0);
124+
stack.push((b / a) >> 0);
125125
} else if (token === "+") {
126126
stack.push(b + a);
127127
} else if (token === "-") {
@@ -132,10 +132,8 @@ var evalRPN = function(tokens) {
132132

133133
return stack.pop();
134134
};
135-
136135
```
137136

138-
139137
Python Code:
140138

141139
```python
@@ -163,7 +161,6 @@ class Solution:
163161
return int(tokens[-1])
164162
```
165163

166-
167164
Java Code:
168165

169166
```java
@@ -189,10 +186,35 @@ class Solution {
189186
}
190187
```
191188

189+
CPP Code:
190+
191+
```cpp
192+
class Solution {
193+
public:
194+
int evalRPN(vector<string>& tokens) {
195+
stack<int> s;
196+
for (string t : tokens) {
197+
if (isdigit(t.back())) s.push(stoi(t));
198+
else {
199+
int n = s.top();
200+
s.pop();
201+
switch(t[0]) {
202+
case '+': s.top() += n; break;
203+
case '-': s.top() -= n; break;
204+
case '*': s.top() *= n; break;
205+
case '/': s.top() /= n; break;
206+
}
207+
}
208+
}
209+
return s.top();
210+
}
211+
};
212+
```
192213
193214
## 扩展
194215
195216
逆波兰表达式中只改变运算符的顺序,并不会改变操作数的相对顺序,这是一个重要的性质。另外逆波兰表达式完全不关心操作符的优先级,这在中缀表达式中是做不到的,这很有趣,感兴趣的可以私下查找资料研究下为什么会这样。
196217
218+
```
197219

198-
220+
```

problems/152.maximum-product-subarray.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var maxProduct = function (nums) {
6767

6868
## 代码
6969

70-
代码支持:Python3,JavaScript
70+
代码支持:Python3,JavaScript, CPP
7171

7272
Python3 Code:
7373

@@ -136,6 +136,24 @@ var maxProduct = function (nums) {
136136
};
137137
```
138138

139+
CPP Code:
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
int maxProduct(vector<int>& A) {
145+
int maxProd = 1, minProd = 1, ans = INT_MIN;
146+
for (int n : A) {
147+
int a = n * maxProd, b = n * minProd;
148+
maxProd = max({n, a, b});
149+
minProd = min({n, a, b});
150+
ans = max(ans, maxProd);
151+
}
152+
return ans;
153+
}
154+
};
155+
```
156+
139157
**复杂度分析**
140158
141159
- 时间复杂度:$O(N)$

problems/169.majority-element.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ https://leetcode-cn.com/problems/majority-element/
5151

5252
## 代码
5353

54-
- 语言支持:JS,Python
54+
- 语言支持:JS,Python, CPP
5555

5656
Javascript Code:
5757

@@ -89,6 +89,26 @@ class Solution:
8989
return majority
9090
```
9191

92+
CPP Code:
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
int majorityElement(vector<int>& nums) {
98+
int ans = 0, cnt = 0;
99+
for (int n : nums) {
100+
if (ans == n) ++cnt;
101+
else if (cnt > 0) --cnt;
102+
else {
103+
ans = n;
104+
cnt = 1;
105+
}
106+
}
107+
return ans;
108+
}
109+
};
110+
```
111+
92112
**复杂度分析**
93113
94114
- 时间复杂度:$O(N)$,其中 N 为数组长度

0 commit comments

Comments
 (0)