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: | |
string reverseWords(string s) { | |
reverse(s.begin(), s.end()); | |
int str_len = 0; | |
int l = -1; | |
for (int i = 0; i < s.size(); ++i) { | |
if (l >= 0 && s[i] == ' ') { | |
reverse(s.begin() + l, s.begin() + i); | |
for (int k = 0; k < i - l; ++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: | |
string replaceSpace(string s) { | |
int space_cnt = count(s.begin(), s.end(), ' '); | |
if (space_cnt < 0) return s; | |
int lft = s.size() - 1; | |
int rht = s.size() + space_cnt * 2 - 1; | |
s.resize(s.size() + space_cnt * 2); | |
for (; lft >= 0; lft--) { |
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: | |
string reverseStr(string s, int k) { | |
int n = 0; | |
for (int i = 0; i < s.size(); i += (2*k)) { | |
if (i + k <= s.size()) | |
reverse(s.begin() + i, s.begin() + i + k); | |
else | |
reverse(s.begin() + i, s.end()); | |
} |
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: | |
void reverseString(vector<char>& s) { | |
int left = 0, right = s.size() - 1; | |
while (left < right) { | |
char tmp = s[left]; | |
s[left] = s[right]; | |
s[right] = tmp; | |
left++; | |
right--; |
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) { |
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) { |
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: | |
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: | |
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: | |
bool isHappy(int n) { | |
unordered_set<int> nums; | |
int sum = 0; | |
while (sum != 1) { | |
sum = 0; | |
while (n > 0) { | |
int k = n % 10; |