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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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
class Solution { | |
public: | |
bool isAnagram(string s, string t) { | |
if (s.size() != t.size()) return false; | |
int table[26] = {0}; | |
for (int i = 0; i < s.size(); ++i) { | |
table[s[i] - 'a']++; | |
table[t[i] - 'a']--; | |
} |
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
class Solution { | |
public: | |
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | |
unordered_set<int> same_set; | |
unordered_set<int> num1_set(nums1.begin(), nums1.end()); | |
for (auto num : nums2) { | |
if (num1_set.find(num) != num1_set.end()) | |
same_set.insert(num); | |
} |
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
class Solution { | |
public: | |
bool isHappy(int n) { | |
unordered_set<int> nums; | |
int sum = 0; | |
while (sum != 1) { | |
sum = 0; | |
while (n > 0) { | |
int k = n % 10; |
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
class Solution { | |
public: | |
vector<int> twoSum(vector<int>& nums, int target) { | |
unordered_map<int, int> nums_map; | |
for (int i = 0; i < nums.size(); ++i){ | |
int k = target - nums[i]; | |
auto iter = nums_map.find(k); | |
if (iter != nums_map.end()) { | |
return {i, nums_map[k]}; | |
} |
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
class Solution { | |
public: | |
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { | |
int ret = 0; | |
unordered_map<int, int> ab_map; | |
for (int a : nums1) { | |
for (int b : nums2) { | |
ab_map[a+b]++; | |
} | |
} |
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
class Solution { | |
public: | |
bool canConstruct(string ransomNote, string magazine) { | |
if (ransomNote.size() > magazine.size()) return false; | |
int table[26] = {0}; | |
for (int i = 0; i < magazine.size(); ++i) { | |
table[magazine[i] - 'a']++; | |
} | |
for (int i = 0; i < ransomNote.size(); ++i) { |
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
class Solution { | |
public: | |
inline void forward(vector<int>& nums, int k, int& j) { | |
while (j < k && nums[j] == nums[j+1]) { | |
j++; | |
} | |
} | |
inline void backward(vector<int>& nums, int j, int& k) { |