2047. 句子中的有效单词数
又是看别人代码的一天
stringstream
在#include<sstream>
中
用法 1
可用于分割被空格等符号分割的字符串
#include<iostream>
#include<sstream>
using namespace std;
int main(){
string str = "hh hh hh" ;
stringstream a(str);
string s;
while ( a >> s ){
cout << s << " ";
}
return 0;
}
// 输出 hh hh hh
用法2
不知道是啥 应该有用
#include<iostream>
#include<sstream>
using namespace std;
int main(){
stringstream s;
s << 1;
int n = 0;
s >> n;
cout << "s = " << s.str() << " " << "n = " << n;
return 0;
}
class Solution {
public:
bool yeah(string s){
int n = s.size(), cnt = 0;
for(int i = 0; i < n; i ++ ){
char c = s[i];
if(isdigit(c)) return false;
if(c == '-'){
if(++ cnt > 1) return false;
if(i == 0 || i == n - 1 || !islower(s[i - 1]) || !islower(s[i + 1])) return false;
}
if((c == '!' || c == '.' || c == ',') && i != n - 1) return false;
}
return true;
}
int countValidWords(string a) {
int cnt = 0;
string b;
stringstream s(a);
while(s >> b) cnt += yeah(b);
return cnt;
}
};