LeetCode字符串
LeetCode.38 外观数列
class Solution {
public:
string countAndSay(int n) {
string s = "1";
for(int i = 1; i < n; ++i) {
string ns;
for(int j = 0; j < s.size(); ++j) {
int k = j;
while(k < s.size() && s[k] == s[j]) ++k;
ns += to_string(k-j) + s[j];
j = k-1; // 因为for循环结束一次j要+1,所以这里-1;
}
s = ns;
}
return s;
}
};
LeetCode.49 字母异位词分组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string,vector<string>> hash;
for(auto str:strs) {
string t = str;
sort(t.begin(), t.end());
hash[t].push_back(str);
}
vector<vector<string>> ans;
for(auto h : hash) ans.push_back(h.second);
return ans;
}
};
LeetCode.151 颠倒字符串中的单词
class Solution {
public:
string reverseWords(string s) {
int k = 0;
for(int i = 0; i < s.size(); ++i) {
while(i < s.size() && s[i] == ' ') ++i;
if(i == s.size()) break;
int j = i;
while(j < s.size() && s[j] != ' ') ++j;
reverse(s.begin() + i, s.begin() + j);
if(k) s[k++] = ' ';
while(i < j) s[k++] = s[i++];
}
s.erase(s.begin() + k, s.end());
reverse(s.begin(), s.end());
return s;
}
};
LeetCode.165 比较版本号
class Solution {
public:
int compareVersion(string version1, string version2) {
int i = 0, j = 0;
// 两个字符串都遍历完再结束循环
while(i < version1.size() || j < version2.size()) {
int x = i, y = j;
while(x < version1.size() && version1[x] != '.') ++x;
while(y < version2.size() && version2[y] != '.') ++y;
int a = x == i? 0 : atoi(version1.substr(i, x-i).c_str());// 两个字符串长度可能不一致,如果有一个字符串先遍历完,0
int b = y == j? 0 : atoi(version2.substr(j, y-j).c_str());
if(a > b) return 1;
if(a < b) return -1;
i = x+1, j = y+1;
}
return 0;
}
};
LeetCode.929 独特的电子邮件地址
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> hash;
for(auto email:emails) {
int index = email.find('@');
string name;
// 过滤
for(auto c:email.substr(0, index)) {
if(c == '+') break;
else if(c != '.') name += c;
}
string domain = email.substr(index);
hash.insert(name + domain);
// cout << name + domain << endl;
}
return hash.size();
}
};
LeetCode.5 最长回文子串
class Solution {
public:
string longestPalindrome(string s) {
string ans;
for(int i = 0; i < s.size(); ++i) {
for(int j = i, k = i; j >=0 && k < s.size() && s[j]==s[k]; --j,++k)
if(k-j+1 > ans.size())
ans = s.substr(j, k-j+1);
}
for(int i = 0; i < s.size(); ++i) {
for(int j = i, k = i+1; j >=0 && k < s.size() && s[j]==s[k]; --j,++k)
if(k-j+1 > ans.size())
ans = s.substr(j, k-j+1);
}
return ans;
}
};
LeetCode.6 Z字形变换
找规律:
- 第0行是首项为0,公差为2*(n - 1)的等差数列;
- 中间的是两个等差数列交错,每相邻的两个数为一组,和为2*(n - 1);
- 第n - 1行是首项为n - 1,公差为2*(n - 1)的等差数列;
class Solution {
public:
string convert(string s, int numRows) {
// 处理numRows = 1时的情况
if(numRows == 1) return s;
string ans;
for(int i = 0; i < numRows; ++i) {
if(!i || i == numRows - 1) {
for(int j = i; j < s.size(); j += 2 * (numRows - 1))
ans += s[j];
}
else {
for(int j = i, k = 2*(numRows-1)-i; j < s.size() || k < s.size();j += 2*(numRows-1), k += 2*(numRows-1)) {
if(j < s.size()) ans += s[j];
if(k < s.size()) ans += s[k];
}
}
}
return ans;
}
};
LeetCode.3 无重复字符的最长子串
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> hash;
int res = 0;
for(int i = 0, j = 0; i < s.size(); ++i) {
hash[s[i]]++;
// 每次循环往不重复子串中新添加的可能是不重复的,可能是重复的
// 如果发现新添加了之后是重复的,就移动左边界j,
while(hash[s[i]] > 1) hash[s[j++]] --;
res = max(res, i - j + 1);
}
return res;
}
};
LeetCode.273 整数数组转换英文表示
分块处理:
— Billion — Million — Thousand —
(1-999) (1-999) (1-999) (1-999)
class Solution {
public:
string small[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
string decade[10] = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
string big[4] = {"Billion", "Million", "Thousand", ""};
string numberToWords(int num) {
string res;
if(!num) return small[num];
for(int i = 1000000000, j = 0; i > 0; i/=1000, ++j) {
if(num >= i) {
res += get_part(num / i) + big[j] + " ";
num %= i;
}
}
while(res.back() == ' ') res.pop_back();
return res;
}
string get_part(int num) {
string res;
if(num >= 100) {
res += small[num / 100] + " Hundred ";
num %= 100;
}
if(!num) return res;
if(num >= 20) {
res += decade[num / 10] + " ";
num %= 10;
}
if(!num) return res;
res += small[num] + " ";
return res;
}
};