Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from apachecn/master
Sync
- Loading branch information
Showing
11 changed files
with
787 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 42. Trapping Rain Water | ||
|
||
**<font color=red>难度Hard<font>** | ||
|
||
## 刷题内容 | ||
> 原题连接 | ||
* https://leetcode.com/problems/trapping-rain-water/ | ||
|
||
> 内容描述 | ||
``` | ||
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. | ||
Example: | ||
Input: [0,1,0,2,1,0,1,3,2,1,2,1] | ||
Output: 6 | ||
``` | ||
|
||
记录 l = 0,先正向遍历数组,如果 height[i] < height[l],则说明此位置无法积水,记录无法积水的面积,如果 height[i] >= height[l],则说明[i,l]的区间内可以积水,然后减去无法积水的面积就是积水的面积。遍历完数组之后,如果 l != height.size() - 1,则反向遍历数组,进行上述步骤。 | ||
> 思路 | ||
******- 时间复杂度: O(N)******- 空间复杂度: O(1)****** | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int trap(vector<int>& height) { | ||
int l = 0,sum1 = 0,water = 0,i; | ||
for(i = 1;i < height.size();++i) | ||
if(height[i] >= height[l]) | ||
{ | ||
water = water + height[l] * (i - l - 1) - sum1; | ||
l = i; | ||
sum1 = 0; | ||
} | ||
else | ||
sum1 += height[i]; | ||
if(l != (height.size() - 1)) | ||
{ | ||
int temp = l; | ||
sum1 = 0; | ||
for(i = height.size() - 2,l = height.size() - 1;i >= temp;--i) | ||
if(height[i] >= height[l]) | ||
{ | ||
water = water + height[l] * (l- i - 1) - sum1; | ||
l = i; | ||
sum1 = 0; | ||
} | ||
else | ||
sum1 += height[i]; | ||
} | ||
return water; | ||
} | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# 101. Symmetric Tree | ||
|
||
**<font color=red>难度Easy<font>** | ||
|
||
## 刷题内容 | ||
> 原题连接 | ||
* https://leetcode.com/problems/symmetric-tree/ | ||
|
||
> 内容描述 | ||
``` | ||
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). | ||
For example, this binary tree [1,2,2,3,4,4,3] is symmetric: | ||
1 | ||
/ \ | ||
2 2 | ||
/ \ / \ | ||
3 4 4 3 | ||
But the following [1,2,2,null,3,null,3] is not: | ||
1 | ||
/ \ | ||
2 2 | ||
\ \ | ||
3 3 | ||
``` | ||
|
||
可以直接DFS比较左右两颗子树是否相等,若想等返回true,若不等,返回false | ||
> 思路 | ||
******- 时间复杂度: O(N)******- 空间复杂度: O(1)****** | ||
|
||
```cpp | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
bool judge(TreeNode* oTree,TreeNode* mTree){ | ||
if((oTree != nullptr) && (mTree != nullptr)) | ||
{ | ||
if((oTree ->val == mTree ->val) && (oTree ->val == mTree ->val)) | ||
return judge(oTree ->left,mTree ->right) && judge(oTree ->right,mTree ->left); | ||
return false; | ||
} | ||
if(oTree == mTree) | ||
return true; | ||
return false; | ||
} | ||
bool isSymmetric(TreeNode* root) { | ||
if(root != nullptr) | ||
return judge(root ->left,root ->right); | ||
return true; | ||
} | ||
}; | ||
``` |
49 changes: 49 additions & 0 deletions
49
docs/Leetcode_Solutions/C++/949._Largest_Time_for_Given_Digits.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
### 949. Largest Time for Given Digits | ||
|
||
题目: | ||
https://leetcode.com/problems/largest-time-for-given-digits/ | ||
|
||
难度: | ||
Easy | ||
|
||
题意: | ||
|
||
1. 给4个0-9的数字 | ||
2. 求能组成最大的时间,时间格式是hh:mm | ||
|
||
思路: | ||
|
||
- 枚举所有的hh:mm的格式 | ||
- 判断是否符合时间的要求,hh<24,mm<60 | ||
- 寻找最大的时间 | ||
|
||
解法: | ||
|
||
```c++ | ||
class Solution { | ||
public: | ||
string largestTimeFromDigits(vector<int>& A) { | ||
string ret = ""; | ||
for (int i = 0;i < 4;i++) { | ||
for (int j = 0;j < 4;j++) { | ||
if (i == j) continue; | ||
if (A[i] * 10 + A[j] >= 24) continue; | ||
for (int k = 0;k < 4;k++) { | ||
if (i == k || j == k) continue; | ||
for (int l = 0;l < 4;l++) { | ||
if (i == l || j == l || k == l) continue; | ||
string s = ""; | ||
if (A[k] * 10 + A[l] >= 60) continue; | ||
s = string("") + char(A[i] + '0') + char(A[j] + '0') + ":" + char(A[k] + '0') + char(A[l] + '0'); | ||
if (ret < s) { | ||
ret = s; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
}; | ||
``` | ||
72 changes: 72 additions & 0 deletions
72
docs/Leetcode_Solutions/C++/950._Reveal_Cards_In_Increasing_Order.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
### 950. Reveal Cards In Increasing Order | ||
|
||
题目: | ||
https://leetcode.com/problems/reveal-cards-in-increasing-order/ | ||
|
||
难度: | ||
Medium | ||
|
||
题意: | ||
|
||
1. 有这么个操作 | ||
2. 将牌的第一张拿出来,把下一张放到底部 | ||
3. 重复这种操作 | ||
4. 到最后拿出来的是一个严格递增数列 | ||
5. 求牌开始的顺序 | ||
|
||
思路: | ||
|
||
- 反过来操作即可 | ||
- 对牌排个序,备选 | ||
- 把底部的牌放到第一张,从备选的牌中选择最大的一张放在顶部 | ||
- 重复这种操作 | ||
|
||
解法: | ||
|
||
```c++ | ||
class Solution { | ||
public: | ||
int list[10000]; | ||
int front, back; | ||
|
||
void add(int val) { | ||
list[front++] = val; | ||
if (front == 10000) { | ||
front = 0; | ||
} | ||
} | ||
|
||
int remove() { | ||
int ret = list[back]; | ||
back++; | ||
if (back == 10000) { | ||
back = 0; | ||
} | ||
return ret; | ||
} | ||
|
||
vector<int> deckRevealedIncreasing(vector<int>& deck) { | ||
front = back = 0; | ||
sort(deck.begin(), deck.end()); | ||
add(deck[deck.size() - 1]); | ||
for (int i = deck.size() - 2;i >= 0;i--) { | ||
int x = remove(); | ||
add(x); | ||
x = deck[i]; | ||
add(x); | ||
} | ||
vector<int> ret; | ||
int i = back; | ||
while (i != front) { | ||
ret.push_back(list[i]); | ||
i++; | ||
if (i == 10000) { | ||
i = 0; | ||
} | ||
} | ||
reverse(ret.begin(), ret.end()); | ||
return ret; | ||
} | ||
}; | ||
``` | ||
58 changes: 58 additions & 0 deletions
58
docs/Leetcode_Solutions/C++/951._Flip_Equivalent_Binary_Trees.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
### 951. Flip Equivalent Binary Trees | ||
|
||
题目: | ||
https://leetcode.com/problems/flip-equivalent-binary-trees/ | ||
|
||
难度: | ||
Medium | ||
|
||
题意: | ||
|
||
1. 给两棵树 | ||
2. 两棵树可以做交换左右子树的操作 | ||
3. 求两棵树是否能通过某些操作,使得两棵树一模一样 | ||
|
||
思路: | ||
|
||
- 递归判断 | ||
- 两个都为空,返回true | ||
- 一个为空,返回false | ||
- 根的值不同,返回false | ||
- 递归判断左子树和右子树,判断左右两棵对应的子树是否为true,或者交换下子树,判断是否为true,返回true | ||
- 其他情况返回false | ||
|
||
解法: | ||
|
||
```c++ | ||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
bool flipEquiv(TreeNode* root1, TreeNode* root2) { | ||
if (!root1 && !root2) { | ||
return true; | ||
} | ||
if (!root1 || !root2) { | ||
return false; | ||
} | ||
if (root1->val != root2->val) { | ||
return false; | ||
} | ||
if (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right)) { | ||
return true; | ||
} | ||
if (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
``` | ||
Oops, something went wrong.