当前位置:网站首页>LeetCode字符串
LeetCode字符串
2022-07-17 05:13:00 【小名王能全】
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;
}
};
边栏推荐
- 面试复习第N次
- 2021-11-10 micropyton tb6600 step drive class
- 简单Chrome脚本 自动跳过b站视频播放结束后的的充电鸣谢页面
- mapping索引属性 & 创建索的操作
- Ehab the xorcist (XOR property, construction)
- MEX and Increments
- Vscode instant English translation plug-in [translation (English Chinese Dictionary)]
- WebService接口的创建与实现
- Speed sensor signal isolation, acquisition and transformation, sine wave and sawtooth wave signal input, square wave signal output, signal converter
- Lithium battery charging management chip
猜你喜欢

Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle

【力扣】翻转二叉树

【力扣】对称二叉树

Configure the 'log' shortcut key in vscode and remove the console log(‘‘); Semicolon in;

Hm8203 linear two string charging management controller IC

Cours de mathématiques de base 2 Fonction Euler, écran linéaire, élargissement de l'Europe

Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends

Minio installation, deployment and simple use

MySQL Workbench基本使用【创建一个数据库】

处理中文分词 ik分词器以及拓展和停止字典
随机推荐
【力扣】二叉树的前序遍历
Acwing game 59 (AK)
谷歌浏览器不能手动修改cookies,cookie报红标红
4-20mA to 4-20mA 0-5V to 0-5V analog signal isolation transmitter
解决:无法加载文件 C:\Program Files\.. 因为在此系统上禁止运行脚本...
RestClient查询文档
RestAPI实现自动补全 & 案例实现(搜索框输入进行自动补全)
Acwing第 59 场周赛(AK)
Introduction to Darwin streaming server
Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle
c语言 指定日期开始多少天 显示
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
make menuconfig缺少ncurses
Ht7727 ht7730 ht7733 ht7737 ht7750 asynchronous DCDC boost IC
【牛客】二叉树遍历
busybox date 日期增加一天明天 网上都是减一天 昨天
Chrome browser settings [display the translation language icon in the upper right corner]
4-20mA to 0-5khz, 5V pulse converter
【力扣】单值二叉树
Tips for using tp4054 charging IC -- used in conjunction with Zhongke Lanxun ab5365b