Skip to content

Commit

Permalink
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Sep 23, 2021
2 parents 3ed64ce + 5ed55ff commit 9fa333e
Show file tree
Hide file tree
Showing 19 changed files with 544 additions and 42 deletions.
2 changes: 1 addition & 1 deletion problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func removeElement(nums []int, val int) int {
```

JavaScript:
```
```javascript
//时间复杂度O(n)
//空间复杂度O(1)
var removeElement = (nums, val) => {
Expand Down
28 changes: 28 additions & 0 deletions problems/0077.组合.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,34 @@ class Solution {
}
```

Python2:
```python
class Solution(object):
def combine(self, n, k):
"""
:type n: int
:type k: int
:rtype: List[List[int]]
"""
result = []
path = []
def backtracking(n, k, startidx):
if len(path) == k:
result.append(path[:])
return

# 剪枝, 最后k - len(path)个节点直接构造结果,无需递归
last_startidx = n - (k - len(path)) + 1
result.append(path + [idx for idx in range(last_startidx, n + 1)])

for x in range(startidx, last_startidx):
path.append(x)
backtracking(n, k, x + 1) # 递归
path.pop() # 回溯

backtracking(n, k, 1)
return result
```

## Python
```python
Expand Down
57 changes: 57 additions & 0 deletions problems/0078.子集.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,63 @@ var subsets = function(nums) {
};
```

C:
```c
int* path;
int pathTop;
int** ans;
int ansTop;
//记录二维数组中每个一维数组的长度
int* length;
//将当前path数组复制到ans中
void copy() {
int* tempPath = (int*)malloc(sizeof(int) * pathTop);
int i;
for(i = 0; i < pathTop; i++) {
tempPath[i] = path[i];
}
ans = (int**)realloc(ans, sizeof(int*) * (ansTop+1));
length[ansTop] = pathTop;
ans[ansTop++] = tempPath;
}

void backTracking(int* nums, int numsSize, int startIndex) {
//收集子集,要放在终止添加的上面,否则会漏掉自己
copy();
//若startIndex大于数组大小,返回
if(startIndex >= numsSize) {
return;
}
int j;
for(j = startIndex; j < numsSize; j++) {
//将当前下标数字放入path中
path[pathTop++] = nums[j];
backTracking(nums, numsSize, j+1);
//回溯
pathTop--;
}
}

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
//初始化辅助变量
path = (int*)malloc(sizeof(int) * numsSize);
ans = (int**)malloc(0);
length = (int*)malloc(sizeof(int) * 1500);
ansTop = pathTop = 0;
//进入回溯
backTracking(nums, numsSize, 0);
//设置二维数组中元素个数
*returnSize = ansTop;
//设置二维数组中每个一维数组的长度
*returnColumnSizes = (int*)malloc(sizeof(int) * ansTop);
int i;
for(i = 0; i < ansTop; i++) {
(*returnColumnSizes)[i] = length[i];
}
return ans;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
76 changes: 76 additions & 0 deletions problems/0093.复原IP地址.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,82 @@ func isNormalIp(s string,startIndex,end int)bool{

```
C:
```c
//记录结果
char** result;
int resultTop;
//记录应该加入'.'的位置
int segments[3];
int isValid(char* s, int start, int end) {
if(start > end)
return 0;
if (s[start] == '0' && start != end) { // 0开头的数字不合法
return false;
}
int num = 0;
for (int i = start; i <= end; i++) {
if (s[i] > '9' || s[i] < '0') { // 遇到非数字字符不合法
return false;
}
num = num * 10 + (s[i] - '0');
if (num > 255) { // 如果大于255了不合法
return false;
}
}
return true;
}
//startIndex为起始搜索位置,pointNum为'.'对象
void backTracking(char* s, int startIndex, int pointNum) {
//若'.'数量为3,分隔结束
if(pointNum == 3) {
//若最后一段字符串符合要求,将当前的字符串放入result种
if(isValid(s, startIndex, strlen(s) - 1)) {
char* tempString = (char*)malloc(sizeof(char) * strlen(s) + 4);
int j;
//记录添加字符时tempString的下标
int count = 0;
//记录添加字符时'.'的使用数量
int count1 = 0;
for(j = 0; j < strlen(s); j++) {
tempString[count++] = s[j];
//若'.'的使用数量小于3且当前下标等于'.'下标,添加'.'到数组
if(count1 < 3 && j == segments[count1]) {
tempString[count++] = '.';
count1++;
}
}
tempString[count] = 0;
//扩容result数组
result = (char**)realloc(result, sizeof(char*) * (resultTop + 1));
result[resultTop++] = tempString;
}
return ;
}
int i;
for(i = startIndex; i < strlen(s); i++) {
if(isValid(s, startIndex, i)) {
//记录应该添加'.'的位置
segments[pointNum] = i;
backTracking(s, i + 1, pointNum + 1);
}
else {
break;
}
}
}
char ** restoreIpAddresses(char * s, int* returnSize){
result = (char**)malloc(0);
resultTop = 0;
backTracking(s, 0, 0);
*returnSize = resultTop;
return result;
}
```


-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
3 changes: 2 additions & 1 deletion problems/0101.对称二叉树.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ public:
queue<TreeNode*> que;
que.push(root->left); // 将左子树头结点加入队列
que.push(root->right); // 将右子树头结点加入队列
while (!que.empty()) { // 接下来就要判断这这两个树是否相互翻转

while (!que.empty()) { // 接下来就要判断这两个树是否相互翻转
TreeNode* leftNode = que.front(); que.pop();
TreeNode* rightNode = que.front(); que.pop();
if (!leftNode && !rightNode) { // 左节点为空、右节点为空,此时说明是对称的
Expand Down
25 changes: 25 additions & 0 deletions problems/0188.买卖股票的最佳时机IV.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Go:
Javascript:

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

return dp[prices.length - 1][2 * k];
};

// 方法二:动态规划+空间优化
var maxProfit = function(k, prices) {
let n = prices.length;
let dp = new Array(2*k+1).fill(0);
// dp 买入状态初始化
for (let i = 1; i <= 2*k; i += 2) {
dp[i] = - prices[0];
}

for (let i = 1; i < n; i++) {
for (let j = 1; j < 2*k+1; j++) {
// j 为奇数:买入状态
if (j % 2) {
dp[j] = Math.max(dp[j], dp[j-1] - prices[i]);
} else {
// j为偶数:卖出状态
dp[j] = Math.max(dp[j], dp[j-1] + prices[i]);
}
}
}

return dp[2*k];
};
```

-----------------------
Expand Down
34 changes: 18 additions & 16 deletions problems/0202.快乐数.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,28 @@ func getSum(n int) int {
javaScript:

```js
function getN(n) {
if (n == 1 || n == 0) return n;
let res = 0;
while (n) {
res += (n % 10) * (n % 10);
n = parseInt(n / 10);
var isHappy = function (n) {
let m = new Map()

const getSum = (num) => {
let sum = 0
while (n) {
sum += (n % 10) ** 2
n = Math.floor(n / 10)
}
return sum
}
return res;
}

var isHappy = function(n) {
const sumSet = new Set();
while (n != 1 && !sumSet.has(n)) {
sumSet.add(n);
n = getN(n);
while (true) {
// n出现过,证明已陷入无限循环
if (m.has(n)) return false
if (n === 1) return true
m.set(n, 1)
n = getSum(n)
}
return n == 1;
};
}

// 使用环形链表的思想 说明出现闭环 退出循环
// 方法二:使用环形链表的思想 说明出现闭环 退出循环
var isHappy = function(n) {
if (getN(n) == 1) return true;
let a = getN(n), b = getN(getN(n));
Expand Down
25 changes: 19 additions & 6 deletions problems/0235.二叉搜索树的最近公共祖先.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,30 @@ class Solution {
递归法:
```python
class Solution:
"""二叉搜索树的最近公共祖先 递归法"""

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root: return root //
if root.val >p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left,p,q) //
elif root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right,p,q) //
else: return root
if root.val > p.val and root.val > q.val:
return self.lowestCommonAncestor(root.left, p, q)
if root.val < p.val and root.val < q.val:
return self.lowestCommonAncestor(root.right, p, q)
return root
```

迭代法:
```python
class Solution:
"""二叉搜索树的最近公共祖先 迭代法"""

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
while True:
if root.val > p.val and root.val > q.val:
root = root.left
elif root.val < p.val and root.val < q.val:
root = root.right
else:
return root
```

## Go

Expand Down
21 changes: 13 additions & 8 deletions problems/0236.二叉树的最近公共祖先.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,21 @@ class Solution {
## Python

```python
//递归
class Solution:
"""二叉树的最近公共祖先 递归法"""

def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or root == p or root == q: return root //找到了节点p或者q,或者遇到空节点
left = self.lowestCommonAncestor(root.left,p,q) //
right = self.lowestCommonAncestor(root.right,p,q) //
if left and right: return root //中: left和right不为空,root就是最近公共节点
elif left and not right: return left //目标节点是通过left返回的
elif not left and right: return right //目标节点是通过right返回的
else: return None //没找到
if not root or root == p or root == q:
return root

left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)

if left and right:
return root
if left:
return left
return right
```

## Go
Expand Down
23 changes: 23 additions & 0 deletions problems/0455.分发饼干.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,29 @@ var findContentChildren = function(g, s) {
```

C:
```c
int cmp(int* a, int* b) {
return *a - *b;
}

int findContentChildren(int* g, int gSize, int* s, int sSize){
if(sSize == 0)
return 0;

//将两个数组排序为升序
qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp);

int numFedChildren = 0;
int i = 0;
for(i = 0; i < sSize; ++i) {
if(numFedChildren < gSize && g[numFedChildren] <= s[i])
numFedChildren++;
}
return numFedChildren;
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
Expand Down
Loading

0 comments on commit 9fa333e

Please sign in to comment.