forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
English translation for all easy problems
- Loading branch information
1 parent
6173063
commit 4778266
Showing
40 changed files
with
7,118 additions
and
0 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,50 @@ | ||
# Collection of simple and Difficult questions | ||
|
||
The questions here are relatively difficult. Most of them are simulation questions, or questions that are easy to see how to solve. In addition, simple questions can generally be solved by violent methods. At this time, you only need to look at the range of data and think about the complexity of your algorithm. | ||
|
||
Of course, it does not rule out that many hard topics can also be simulated violently. Everyone can pay more attention to the data range. | ||
|
||
The following are the classic topics I listed (the words with 91 indicate that they are from the **91 Days of Learning algorithm**activity): | ||
|
||
-[Interview Question 17.12. BiNode](../problems/binode-lcci.en.md) | ||
|
||
- [0001. Sum of two numbers](../problems/1.two-sum.en.md) | ||
- [0020. Valid brackets](../problems/20.valid-parents.en.md) | ||
- [0021. Merge two ordered lists](../problems/21.merge-two-sorted-lists.en.md) | ||
- [0026. Delete duplicates in the sorted array](../problems/26.remove-duplicates-from-sorted-array.en.md) | ||
- [0053. Maximum subarray sum)(../problems/53.maximum-sum-subarray-cn.en.md) | ||
- [0066. Plus one](../problems/66.plus-one.en.md) 91 | ||
- [0088. Merge two ordered arrays](../problems/88.merge-sorted-array.en.md) | ||
- [0101. Symmetrical binary tree)(../problems/101.symmetrical-tree.en.md) | ||
- [0104. Maximum depth of binary tree)(../problems/104.maximum-depth-of-binary-tree.en.md) | ||
- [0108. Convert an ordered array to a binary search tree)(../problems/108.convert-sorted-array-to-binary-search-tree.en.md) | ||
- [0121. The best time to buy and sell stocks](../problems/121.best-time-to-buy-and-sell-stock.en.md) | ||
- [0122. The best time to buy and sell stocks II](../problems/122.best-time-to-buy-and-sell-stock-ii.en.md) | ||
- [0125. Verification palindrome string](../problems/125.valid-palindrome.en.md) | ||
- [0136. Numbers that appear only once](../problems/136.single-number.en.md) | ||
- [0155. Minimum stack)(../problems/155.min-stack.en.md) | ||
- [0160. Intersection list](../problems/160.Intersection-of-Two-Linked-Lists.en.md) 91 | ||
- [0167. The sum of two numbers [input ordered array](../problems/167.two-sum-ii-input-array-is-sorted.en.md) | ||
- [0169. Majority element](../problems/169.majority-element.en.md) | ||
- [0172. Zero after factorial](../problems/172.factorial-trailing-zeroes.en.md) | ||
- [0190. Reverse binary bits](../problems/190.reverse-bits.en.md) | ||
- [0191. The number of bits of 1](../problems/191.number-of-1-bits.en.md) | ||
- [0198. House-robbing](../problems/198.house-robber.en.md) | ||
- [0203. Remove linked list elements](../problems/203.remove-linked-list-elements.en.md) | ||
- [0206. Reverse linked list](../problems/206.reverse-linked-list.en.md) | ||
- [0219. Duplicate element II exists)(../problems/219.contains-duplicate-ii.en.md) | ||
- [0226. Flip binary tree](../problems/226.invert-binary-tree.en.md) | ||
- [0232. Implementing queues with stacks](../problems/232.implement-queue-using-stacks.en.md) 91 | ||
- [0263. Ugly number](../problems/263.ugly-number.en.md) | ||
- [0283. Move zero](../problems/283.move-zeroes.en.md) | ||
- [0342. Power of 4](../problems/342.power-of-four.en.md) | ||
- [0349. Intersection of two arrays](../problems/349.intersection-of-two-arrays.en.md) | ||
- [0371. Sum of two integers](../problems/371.sum-of-two-integers.en.md) | ||
- [401. Binary watch](../problems/401.binary-watch.en.md) | ||
- [0437. Path sum III](../problems/437.path-sum-iii.en.md) | ||
- [0455. Distribute cookies](../problems/455.AssignCookies.en.md) | ||
- [0575. Distribute candies)(../problems/575.distribute-candies.en.md) | ||
- [821. The shortest distance of a character](../problems/821.shortest-distance-to-a-character.en.md) 91 | ||
- [0874. Simulation of walking robot)(../problems/874.walking-robot-simulation.en.md) | ||
- [1260. Two-dimensional grid migration](../problems/1260.shift-2d-grid.en.md) | ||
- [1332. Delete palindromic sequences](../problems/1332.remove-palindromic-sequences.en.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,204 @@ | ||
## Problem (101. Symmetrical binary tree) | ||
|
||
https://leetcode.com/problems/symmetric-tree/ | ||
|
||
## Title description | ||
|
||
``` | ||
Given a binary tree, check whether it is mirror symmetrical. | ||
For example, a binary tree [1,2,2,3,4,4,3] is symmetrical. | ||
1 | ||
/ \ | ||
2 2 | ||
/ \ / \ | ||
3 4 4 3 | ||
But the following [1,2,2,null,3,null,3] is not mirror symmetrical: | ||
1 | ||
/ \ | ||
2 2 | ||
\ \ | ||
3 3 | ||
Advanced: | ||
Can you use recursion and iteration to solve this problem? | ||
``` | ||
|
||
## Company | ||
|
||
-Ali | ||
-Tencent | ||
-Baidu | ||
-Byte | ||
|
||
- bloomberg | ||
- microsoft | ||
|
||
## Pre-knowledge | ||
|
||
-[Binary tree](https://github.com/azl397985856/leetcode/blob/master/thinkings/basic-data-structure.md) -[recursion](https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md) | ||
|
||
## Idea | ||
|
||
When I saw this question, my first instinct was DFS. Then I thought: `If the left subtree is a mirror image, and the right subtree is also a mirror image, does it mean that the whole is a mirror image? `. After a few seconds of thinking, this is obviously wrong and does not meet the meaning of the question. | ||
|
||
 | ||
|
||
Obviously, the nodes in the left subtree will be compared with the nodes in the right subtree. I have distinguished the colors of the compared elements for your convenience. | ||
|
||
My idea here is: `When traversing each node, if I can know who its corresponding symmetrical node is by some method, then I can directly compare whether the two are consistent. ` | ||
|
||
Therefore, the idea is to traverse twice. During the first traversal, the traversal results are stored in the hash table at the same time, and then the second traversal goes to the hash table to fetch. This method is feasible, but it requires N space (N is the total number of nodes). I thought that if the two can be traversed at the same time, wouldn't the overhead of the hash table be eliminated? | ||
|
||
 | ||
|
||
If you don't understand, let me give a simple example: | ||
|
||
``` | ||
Given an array, check if it is mirror symmetrical. For example, the array [1,2,2,3,2,2,1] is symmetrical. | ||
``` | ||
|
||
If you use a hash table, it is probably: | ||
|
||
```py | ||
seen = dict() | ||
for i, num in enumerate(nums): | ||
seen[i] = num | ||
for i, num in enumerate(nums): | ||
if seen[len(nums) - 1 - i] ! = num: | ||
return False | ||
return True | ||
``` | ||
|
||
And traversing at the same time is probably like this: | ||
|
||
```py | ||
l = 0 | ||
r = len(nums) - 1 | ||
|
||
while l < r: | ||
if nums[l] ! = nums[r]: return False | ||
l += 1 | ||
r -= 1 | ||
return True | ||
|
||
``` | ||
|
||
> In fact, if it is more like this topic, it should be expanded from the middle to both sides. | ||
## Code | ||
|
||
Code support: C++, Java, Python3 | ||
|
||
C++ Code: | ||
|
||
```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 isSymmetric(TreeNode* root) { | ||
return root==NULL? true:recur(root->left, root->right); | ||
} | ||
|
||
bool recur(TreeNode* l, TreeNode* r) | ||
{ | ||
if(l == NULL && r==NULL) | ||
{ | ||
return true; | ||
} | ||
// There is only one child node or the left and right are not equal | ||
if(l==NULL || r==NULL || l->val ! = r->val) | ||
{ | ||
return false; | ||
} | ||
|
||
return recur(l->left, r->right) && recur(l->right, r->left); | ||
} | ||
}; | ||
``` | ||
Java Code: | ||
```java | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
class Solution { | ||
public boolean isSymmetric(TreeNode root) { | ||
if(root == null) | ||
{ | ||
return true; | ||
} | ||
else{ | ||
return recur(root. left, root. right); | ||
} | ||
// return root == null ? true : recur(root. left, root. right); | ||
} | ||
public boolean recur(TreeNode l, TreeNode r) | ||
{ | ||
if(l == null && r==null) | ||
{ | ||
return true; | ||
} | ||
// There is only one child node or the left and right are not equal | ||
if(l==null || r==null || l. val ! = r. val) | ||
{ | ||
return false; | ||
} | ||
return recur(l. left, r. right) && recur(l. right, r. left); | ||
} | ||
} | ||
``` | ||
|
||
Python3 Code: | ||
|
||
```py | ||
|
||
class Solution: | ||
def isSymmetric(self, root: TreeNode) -> bool: | ||
def dfs(root1, root2): | ||
if root1 == root2 == None: return True | ||
if not root1 or not root2: return False | ||
if root1. val ! = root2. val: return False | ||
return dfs(root1. left, root2. right) and dfs(root1. right, root2. left) | ||
if not root: return True | ||
return dfs(root. left, root. right) | ||
``` | ||
|
||
**Complexity analysis** | ||
|
||
-Time complexity:$O(N)$, where N is the number of nodes. | ||
-Spatial complexity: The highest depth of recursion is the number of nodes, so the spatial complexity is $O(N)$, where N is the number of nodes. | ||
|
||
If you have any comments on this, please leave me a message. I will check the answers one by one when I have time. For more algorithm routines, you can visit my LeetCode problem solving warehouse:https://github.com/azl397985856/leetcode . There are already 37K stars. | ||
You can also pay attention to my public account "Force Buckle Plus" to take you to chew off the hard bone of the algorithm. | ||
 | ||
|
||
 |
Oops, something went wrong.