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: | |
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: | |
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: | |
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 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 reverseLeftWords(string s, int n) { | |
reverse(s.begin(), s.end()); | |
reverse(s.begin(), s.begin() + s.size() - n); | |
reverse(s.begin() + s.size() - n, s.end()); | |
return s; | |
} | |
}; |
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 strStr(string haystack, string needle) { | |
if (haystack.size() < needle.size()) | |
return -1; | |
if (needle.size() == 0) | |
return 0; | |
for (int i = 0; i < haystack.size() - needle.size() + 1; ++i) { | |
if (haystack[i] == needle[0]) { | |
int k = 0; |
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 get_pmt(const string& s, int*pmt) { | |
pmt[0] = 0; | |
int j = 0; | |
for (int i = 1; i < s.size(); ++i) { | |
while (j > 0 && s[i] != s[j]) | |
j = pmt[j - 1]; | |
if (s[i] == s[j]) |
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 MyQueue { | |
public: | |
MyQueue() { | |
} | |
void push(int x) { | |
my_deque_.push_back(x); | |
} | |
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 isValid(string s) { | |
stack<int> check; | |
map<char, char> bracket_map; | |
bracket_map['('] = ')'; | |
bracket_map['['] = ']'; | |
bracket_map['{'] = '}'; | |
for (int i = 0; i < s.size(); ++i) { | |
if (s[i] == '(' || s[i] == '[' || s[i] == '{') |