Skip to content

Commit 9fa333e

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents 3ed64ce + 5ed55ff commit 9fa333e

19 files changed

+544
-42
lines changed

problems/0027.移除元素.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func removeElement(nums []int, val int) int {
185185
```
186186

187187
JavaScript:
188-
```
188+
```javascript
189189
//时间复杂度O(n)
190190
//空间复杂度O(1)
191191
var removeElement = (nums, val) => {

problems/0077.组合.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,34 @@ class Solution {
368368
}
369369
```
370370

371+
Python2:
372+
```python
373+
class Solution(object):
374+
def combine(self, n, k):
375+
"""
376+
:type n: int
377+
:type k: int
378+
:rtype: List[List[int]]
379+
"""
380+
result = []
381+
path = []
382+
def backtracking(n, k, startidx):
383+
if len(path) == k:
384+
result.append(path[:])
385+
return
386+
387+
# 剪枝, 最后k - len(path)个节点直接构造结果,无需递归
388+
last_startidx = n - (k - len(path)) + 1
389+
result.append(path + [idx for idx in range(last_startidx, n + 1)])
390+
391+
for x in range(startidx, last_startidx):
392+
path.append(x)
393+
backtracking(n, k, x + 1) # 递归
394+
path.pop() # 回溯
395+
396+
backtracking(n, k, 1)
397+
return result
398+
```
371399

372400
## Python
373401
```python

problems/0078.子集.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,63 @@ var subsets = function(nums) {
263263
};
264264
```
265265

266+
C:
267+
```c
268+
int* path;
269+
int pathTop;
270+
int** ans;
271+
int ansTop;
272+
//记录二维数组中每个一维数组的长度
273+
int* length;
274+
//将当前path数组复制到ans中
275+
void copy() {
276+
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
277+
int i;
278+
for(i = 0; i < pathTop; i++) {
279+
tempPath[i] = path[i];
280+
}
281+
ans = (int**)realloc(ans, sizeof(int*) * (ansTop+1));
282+
length[ansTop] = pathTop;
283+
ans[ansTop++] = tempPath;
284+
}
285+
286+
void backTracking(int* nums, int numsSize, int startIndex) {
287+
//收集子集,要放在终止添加的上面,否则会漏掉自己
288+
copy();
289+
//若startIndex大于数组大小,返回
290+
if(startIndex >= numsSize) {
291+
return;
292+
}
293+
int j;
294+
for(j = startIndex; j < numsSize; j++) {
295+
//将当前下标数字放入path中
296+
path[pathTop++] = nums[j];
297+
backTracking(nums, numsSize, j+1);
298+
//回溯
299+
pathTop--;
300+
}
301+
}
302+
303+
int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
304+
//初始化辅助变量
305+
path = (int*)malloc(sizeof(int) * numsSize);
306+
ans = (int**)malloc(0);
307+
length = (int*)malloc(sizeof(int) * 1500);
308+
ansTop = pathTop = 0;
309+
//进入回溯
310+
backTracking(nums, numsSize, 0);
311+
//设置二维数组中元素个数
312+
*returnSize = ansTop;
313+
//设置二维数组中每个一维数组的长度
314+
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
315+
int i;
316+
for(i = 0; i < ansTop; i++) {
317+
(*returnColumnSizes)[i] = length[i];
318+
}
319+
return ans;
320+
}
321+
```
322+
266323
267324
-----------------------
268325
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0093.复原IP地址.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,82 @@ func isNormalIp(s string,startIndex,end int)bool{
476476

477477
```
478478
479+
C:
480+
```c
481+
//记录结果
482+
char** result;
483+
int resultTop;
484+
//记录应该加入'.'的位置
485+
int segments[3];
486+
int isValid(char* s, int start, int end) {
487+
if(start > end)
488+
return 0;
489+
if (s[start] == '0' && start != end) { // 0开头的数字不合法
490+
return false;
491+
}
492+
int num = 0;
493+
for (int i = start; i <= end; i++) {
494+
if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法
495+
return false;
496+
}
497+
num = num * 10 + (s[i] - '0');
498+
if (num > 255) { // 如果大于255了不合法
499+
return false;
500+
}
501+
}
502+
return true;
503+
}
504+
505+
//startIndex为起始搜索位置,pointNum为'.'对象
506+
void backTracking(char* s, int startIndex, int pointNum) {
507+
//若'.'数量为3,分隔结束
508+
if(pointNum == 3) {
509+
//若最后一段字符串符合要求,将当前的字符串放入result种
510+
if(isValid(s, startIndex, strlen(s) - 1)) {
511+
char* tempString = (char*)malloc(sizeof(char) * strlen(s) + 4);
512+
int j;
513+
//记录添加字符时tempString的下标
514+
int count = 0;
515+
//记录添加字符时'.'的使用数量
516+
int count1 = 0;
517+
for(j = 0; j < strlen(s); j++) {
518+
tempString[count++] = s[j];
519+
//若'.'的使用数量小于3且当前下标等于'.'下标,添加'.'到数组
520+
if(count1 < 3 && j == segments[count1]) {
521+
tempString[count++] = '.';
522+
count1++;
523+
}
524+
}
525+
tempString[count] = 0;
526+
//扩容result数组
527+
result = (char**)realloc(result, sizeof(char*) * (resultTop + 1));
528+
result[resultTop++] = tempString;
529+
}
530+
return ;
531+
}
532+
533+
int i;
534+
for(i = startIndex; i < strlen(s); i++) {
535+
if(isValid(s, startIndex, i)) {
536+
//记录应该添加'.'的位置
537+
segments[pointNum] = i;
538+
backTracking(s, i + 1, pointNum + 1);
539+
}
540+
else {
541+
break;
542+
}
543+
}
544+
}
545+
546+
char ** restoreIpAddresses(char * s, int* returnSize){
547+
result = (char**)malloc(0);
548+
resultTop = 0;
549+
backTracking(s, 0, 0);
550+
*returnSize = resultTop;
551+
return result;
552+
}
553+
```
554+
479555

480556
-----------------------
481557
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0101.对称二叉树.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public:
185185
queue<TreeNode*> que;
186186
que.push(root->left); // 将左子树头结点加入队列
187187
que.push(root->right); // 将右子树头结点加入队列
188-
while (!que.empty()) { // 接下来就要判断这这两个树是否相互翻转
188+
189+
while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转
189190
TreeNode* leftNode = que.front(); que.pop();
190191
TreeNode* rightNode = que.front(); que.pop();
191192
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的

problems/0188.买卖股票的最佳时机IV.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Go:
280280
Javascript:
281281

282282
```javascript
283+
// 方法一:动态规划
283284
const maxProfit = (k,prices) => {
284285
if (prices == null || prices.length < 2 || k == 0) {
285286
return 0;
@@ -300,6 +301,30 @@ const maxProfit = (k,prices) => {
300301

301302
return dp[prices.length - 1][2 * k];
302303
};
304+
305+
// 方法二:动态规划+空间优化
306+
var maxProfit = function(k, prices) {
307+
let n = prices.length;
308+
let dp = new Array(2*k+1).fill(0);
309+
// dp 买入状态初始化
310+
for (let i = 1; i <= 2*k; i += 2) {
311+
dp[i] = - prices[0];
312+
}
313+
314+
for (let i = 1; i < n; i++) {
315+
for (let j = 1; j < 2*k+1; j++) {
316+
// j 为奇数:买入状态
317+
if (j % 2) {
318+
dp[j] = Math.max(dp[j], dp[j-1] - prices[i]);
319+
} else {
320+
// j为偶数:卖出状态
321+
dp[j] = Math.max(dp[j], dp[j-1] + prices[i]);
322+
}
323+
}
324+
}
325+
326+
return dp[2*k];
327+
};
303328
```
304329

305330
-----------------------

problems/0202.快乐数.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,28 @@ func getSum(n int) int {
159159
javaScript:
160160

161161
```js
162-
function getN(n) {
163-
if (n == 1 || n == 0) return n;
164-
let res = 0;
165-
while (n) {
166-
res += (n % 10) * (n % 10);
167-
n = parseInt(n / 10);
162+
var isHappy = function (n) {
163+
let m = new Map()
164+
165+
const getSum = (num) => {
166+
let sum = 0
167+
while (n) {
168+
sum += (n % 10) ** 2
169+
n = Math.floor(n / 10)
170+
}
171+
return sum
168172
}
169-
return res;
170-
}
171173

172-
var isHappy = function(n) {
173-
const sumSet = new Set();
174-
while (n != 1 && !sumSet.has(n)) {
175-
sumSet.add(n);
176-
n = getN(n);
174+
while (true) {
175+
// n出现过,证明已陷入无限循环
176+
if (m.has(n)) return false
177+
if (n === 1) return true
178+
m.set(n, 1)
179+
n = getSum(n)
177180
}
178-
return n == 1;
179-
};
181+
}
180182

181-
// 使用环形链表的思想 说明出现闭环 退出循环
183+
// 方法二:使用环形链表的思想 说明出现闭环 退出循环
182184
var isHappy = function(n) {
183185
if (getN(n) == 1) return true;
184186
let a = getN(n), b = getN(getN(n));

problems/0235.二叉搜索树的最近公共祖先.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,30 @@ class Solution {
268268
递归法:
269269
```python
270270
class Solution:
271+
"""二叉搜索树的最近公共祖先 递归法"""
272+
271273
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
272-
if not root: return root //
273-
if root.val >p.val and root.val > q.val:
274-
return self.lowestCommonAncestor(root.left,p,q) //
275-
elif root.val < p.val and root.val < q.val:
276-
return self.lowestCommonAncestor(root.right,p,q) //
277-
else: return root
274+
if root.val > p.val and root.val > q.val:
275+
return self.lowestCommonAncestor(root.left, p, q)
276+
if root.val < p.val and root.val < q.val:
277+
return self.lowestCommonAncestor(root.right, p, q)
278+
return root
278279
```
279280

280281
迭代法:
282+
```python
283+
class Solution:
284+
"""二叉搜索树的最近公共祖先 迭代法"""
281285

286+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
287+
while True:
288+
if root.val > p.val and root.val > q.val:
289+
root = root.left
290+
elif root.val < p.val and root.val < q.val:
291+
root = root.right
292+
else:
293+
return root
294+
```
282295

283296
## Go
284297

problems/0236.二叉树的最近公共祖先.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,21 @@ class Solution {
264264
## Python
265265

266266
```python
267-
//递归
268267
class Solution:
268+
"""二叉树的最近公共祖先 递归法"""
269+
269270
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
270-
if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点
271-
left = self.lowestCommonAncestor(root.left,p,q) //
272-
right = self.lowestCommonAncestor(root.right,p,q) //
273-
if left and right: return root //中: left和right不为空,root就是最近公共节点
274-
elif left and not right: return left //目标节点是通过left返回的
275-
elif not left and right: return right //目标节点是通过right返回的
276-
else: return None //没找到
271+
if not root or root == p or root == q:
272+
return root
273+
274+
left = self.lowestCommonAncestor(root.left, p, q)
275+
right = self.lowestCommonAncestor(root.right, p, q)
276+
277+
if left and right:
278+
return root
279+
if left:
280+
return left
281+
return right
277282
```
278283

279284
## Go

problems/0455.分发饼干.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,29 @@ var findContentChildren = function(g, s) {
217217
218218
```
219219

220+
C:
221+
```c
222+
int cmp(int* a, int* b) {
223+
return *a - *b;
224+
}
225+
226+
int findContentChildren(int* g, int gSize, int* s, int sSize){
227+
if(sSize == 0)
228+
return 0;
229+
230+
//将两个数组排序为升序
231+
qsort(g, gSize, sizeof(int), cmp);
232+
qsort(s, sSize, sizeof(int), cmp);
233+
234+
int numFedChildren = 0;
235+
int i = 0;
236+
for(i = 0; i < sSize; ++i) {
237+
if(numFedChildren < gSize && g[numFedChildren] <= s[i])
238+
numFedChildren++;
239+
}
240+
return numFedChildren;
241+
}
242+
```
220243
221244
-----------------------
222245
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)