参考链接:
https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/solution/yi-ge-mo-ban-shua-bian-suo-you-zi-fu-chu-x6vh/
class Solution {
public:
int lengthOfLastWord(string s) {
if(s.empty()){
return 0;
}
s += " ";这里在最后一个字符位置加上空格,这样最后一个字符串就不会遗漏
vector<string>res;
string temp = "";
for(char x:s){
if(x == ' '){
if(!temp.empty()){要不要判断非空取决于给定的字符串有没有前置或者后置的空格
res.push_back(temp);
temp.clear();
}
}else{
temp += x;
}
}
if(res.empty()){
return 0;
}
return res.back().size();
}
};
class Solution {
public:
string reverseWords(string s) {
if(s.empty()){
return 0;
}
s += " ";
vector<string>res;
string temp = "";
for(char x:s){
if(x == ' '){
res.push_back(temp);
temp.clear();
}else{
temp += x;
}
}
s.clear();
for(string &m:res){
reverse(m.begin(),m.end());
s += m + ' ';
}
s.pop_back(); //注意最后多加了一个空格要去掉
return s;
}
};
class Solution {
public:
string reverseWords(string s) {
if(s.empty()){
return "";
}
s += " ";
vector<string>res;
string temp = "";
for(char x:s){
if(x == ' '){
if(!temp.empty()){
res.push_back(temp);
temp.clear();
}
}else{
temp += x;
}
}
s.clear();
reverse(res.begin(),res.end());
for(string m:res){
s += m + ' ';
}
s.pop_back();
return s;
}
};
class Solution {
public:
string truncateSentence(string s, int k) {
if(s.empty()){
return "";
}
s+=" ";
string temp = "";
vector<string>res;
for(char x:s){
if(x == ' '){
res.push_back(temp);
temp.clear();
}else{
temp += x;
}
}
s.clear();
// int m = 0;
// for(string &x:res){
// s += x + ' ';
// m++;
// if(m == k){
// break;
// }
// }
for(int i =0;i < k;i++){
s += res[i]+' ';
}
s.pop_back();
return s;
}
};
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
return ' '.join(s.split(' ')[:k])
class Solution {
public:
int numDifferentIntegers(string word) {
set<string>s;
word += 'a';//以防止最后一个是数字
string temp = "";
for(char x:word){
if(isalpha(x)){
if(!temp.empty()){
s.insert(temp);//在集合中插入数字
temp.clear();
}
}else{//遇到数字
if(temp == "0"){//数字前导为0
temp.clear();
}
temp += x;
}
}
return s.size();//集合去重,集合大小就是所有整数长度
}
};
class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) {
paragraph += " ";
string temp = "";
set<string>ban(banned.begin(),banned.end());
map<string,int>s;
for(char x:paragraph){
if(!isalpha(x)){
if(!temp.empty()){
s[temp]++;//对每一个单词计数
temp.clear();
}
}else{
temp += tolower(x);//转小写
}
}
vector<string>words;
for(auto p:s){
words.push_back(p.first);
}
sort(words.begin(),words.end(),[&](string &p,string &q){return s[p]>s[q];}); //按照频次降序
if(ban.empty()){//如果没有禁用单词,直接返回排序后列表首元素
return words[0];
}
for(auto w:words){
if(ban.find(w) == ban.end()){
return w;
}
}
return "";
}
};
class Solution {
public:
string toGoatLatin(string sentence) {
sentence += " ";
string temp = "";
vector<string>res;
string vowels = "aeiouAEIOU";
for(char x:sentence){
if(x == ' '){
res.push_back(temp);
temp.clear();
}else{
temp += x;
}
}
sentence.clear();
for(int i = 0; i<res.size();i++){
if(vowels.find(res[i][0]) != -1){//如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
sentence += res[i];
}else{
string t = res[i] + res[i][0];//如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
t.erase(t.begin());
sentence += t;
}
sentence += "ma";
sentence.insert(sentence.size(),i+1,'a');//根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a'
sentence += " ";
}
sentence.pop_back();
return sentence;
}
};