|
12 | 12 | 9. [Maximum Swap](#Maximum-Swap) Medium |
13 | 13 | 10. [Intersection of Two Arrays](#Intersection-of-Two-Arrays) Easy |
14 | 14 | 11. [Intersection of Two Arrays II](#Intersection-of-Two-Arrays-II) Easy |
15 | | -12. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium |
16 | 15 | 13. [Rectangle Overlap](#Rectangle-Overlap) Easy |
17 | 16 | 14. [Minimum Window Substring](#Minimum-Window-Substring) Hard |
18 | 17 | 15. [Valid Number](#Valid-Number) Hard |
|
21 | 20 | 18. [Find the Kth largest item](#Find-the-Kth-largest-item) Medium |
22 | 21 | 19. [Count Duplicates](#Count-Duplicates) Medium |
23 | 22 | 20. [3 Sum](#3-Ssum) |
| 23 | +21. [Majority Item](Majority Item) Medium |
24 | 24 |
|
25 | 25 | ## Matrix |
26 | 26 | 1. [Diagnose Tranverse](#Diagnose-Tranverse) Medium |
27 | 27 | 2. [Kth Smallest Element in a Sorted Matrix](#Kth-Smallest-Element-in-a-Sorted-Matrix) Medium |
28 | 28 | 3. [Rotate Image](#Rotate-Image) Medium |
| 29 | +4. [Sparse Matrix Multiplication](#Sparse-Matrix-Multiplication) Medium |
29 | 30 |
|
30 | 31 | ## Binary Search |
31 | 32 | 1. [Search a 2D Matrix](#Search-a-2D-Matrix) Easy |
|
49 | 50 | 11. [Plus One Linked List](#Plus-One-Linked-List) Medium |
50 | 51 | 12. [Intersection of Two Linked Lists](#Intersection-of-Two-Linked-Lists) Medium |
51 | 52 | 13. [Remove Duplicates from Sorted List II](#Remove-Duplicates-from-Sorted-List-II) Medium |
52 | | -14. [Remove Nth Node From End of List](#Remove-Nth node-from-the-end-of-list) Medium |
| 53 | +14. [Remove Nth Node From End of List](#Remove-Nth-node-from-the-end-of-list) Medium |
53 | 54 |
|
54 | 55 | ## String |
55 | 56 | 1. [Valid Anagram](#Valid-Anagram) Easy |
|
60 | 61 | 1. [Is Power of Four](#Is-Power-of-Four) Easy |
61 | 62 | 2. [Range Bit And](#Range-Bit-And) Medium |
62 | 63 | 3. [Number Complement](#Number-Complement) Medium |
| 64 | +4. [Single Number II](#Single-Number-II) Medium |
63 | 65 |
|
64 | 66 | ## Data Structure |
65 | 67 | 1. [LRU cache](#LRU-cache) Hard |
|
86 | 88 | 12. [Verifying a Alien Dictionary](#Verify-a-Alien-Dictionary) Medium |
87 | 89 | 13. [Container With Most Water](#Container-With-Most-Water) Medium |
88 | 90 | 14. [Move Zeroes](#Move-Zeroes) Medium |
| 91 | +15. [Symmetric Tree](#Symmetric-Tree) Easy |
89 | 92 |
|
90 | 93 | ## Implementation |
91 | 94 |
|
@@ -3038,4 +3041,99 @@ public: |
3038 | 3041 | return sentinal.next; |
3039 | 3042 | } |
3040 | 3043 | }; |
| 3044 | +``` |
| 3045 | +
|
| 3046 | +### Symmetric Tree |
| 3047 | +***Big O:*** O(log(n)) speed, O(1) space |
| 3048 | +``` |
| 3049 | +Tips: |
| 3050 | + |
| 3051 | +Recursion. |
| 3052 | +``` |
| 3053 | +```c++ |
| 3054 | +class Solution { |
| 3055 | +public: |
| 3056 | + /** |
| 3057 | + * @param root: root of the given tree |
| 3058 | + * @return: whether it is a mirror of itself |
| 3059 | + */ |
| 3060 | + bool isSymmetric (TreeNode* root) { |
| 3061 | + // Write your code here |
| 3062 | + return root == nullptr || isSymmetricHelp (root->left, root->right); |
| 3063 | + } |
| 3064 | + bool isSymmetricHelp (TreeNode* left, TreeNode* right) { |
| 3065 | + if (left == nullptr || right == nullptr) { |
| 3066 | + return left == nullptr && right == nullptr; |
| 3067 | + } |
| 3068 | + if (left->val != right->val) { |
| 3069 | + return false; |
| 3070 | + } |
| 3071 | + return isSymmetricHelp (left->left, right->right) && isSymmetricHelp (left->right, right->left); |
| 3072 | + } |
| 3073 | +}; |
| 3074 | +``` |
| 3075 | + |
| 3076 | +### Single Number II |
| 3077 | +***Big O:*** O(N) speed, O(1) space |
| 3078 | +``` |
| 3079 | +Tips: |
| 3080 | +
|
| 3081 | +Counting Repeating Bits. If the mod with 3 is one, then that number must have this bit set. |
| 3082 | +``` |
| 3083 | +```c++ |
| 3084 | +class Solution { |
| 3085 | +public: |
| 3086 | + void count_bits(int arr[], int val) { |
| 3087 | + for (int i = 0; i < 32; i++) { |
| 3088 | + if (val & (0x1 << i)) |
| 3089 | + arr[i] ++; |
| 3090 | + } |
| 3091 | + } |
| 3092 | + |
| 3093 | + int singleNumber(vector<int>& nums) { |
| 3094 | + int arr[32] = {}; |
| 3095 | + int ret = 0; |
| 3096 | + |
| 3097 | + for (auto num : nums) { |
| 3098 | + count_bits(arr, num); |
| 3099 | + } |
| 3100 | + |
| 3101 | + for (int i = 0; i < 32; i++) { |
| 3102 | + arr[i] = arr[i]%3; |
| 3103 | + ret |= (arr[i] << i); |
| 3104 | + } |
| 3105 | + |
| 3106 | + return ret; |
| 3107 | + } |
| 3108 | +}; |
| 3109 | +``` |
| 3110 | +
|
| 3111 | +### Majority Item |
| 3112 | +***Big O:*** O(N) speed, O(1) space |
| 3113 | +``` |
| 3114 | +Tips: |
| 3115 | + |
| 3116 | +Counting Repeating Bits. If the bit position appears more than n/2 times, then we know it is for sure in the number. |
| 3117 | +``` |
| 3118 | +```c++ |
| 3119 | +class Solution { |
| 3120 | +public: |
| 3121 | + int majorityElement(vector<int>& nums) { |
| 3122 | + int n = nums.size(); |
| 3123 | + int ans = 0, bits[32] = {0}; |
| 3124 | + |
| 3125 | + for (auto num : nums) { |
| 3126 | + for (int i = 0; i < 32; i++) { |
| 3127 | + bits[i] += ((num >> i) & 0x1); |
| 3128 | + } |
| 3129 | + } |
| 3130 | + |
| 3131 | + for (int i = 0; i < 32; i++) { |
| 3132 | + if (bits[i] > n/2) |
| 3133 | + ans |= (1U << i); |
| 3134 | + } |
| 3135 | + |
| 3136 | + return ans; |
| 3137 | + } |
| 3138 | +}; |
3041 | 3139 | ``` |
0 commit comments