题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
vector<string> res;
int max_len = 0;
string tem;
while (cin >> s) {
res.clear();
max_len = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
if (i == s.size() - 1) {
tem+=s[i];
if (!tem.empty()) {
if (tem.length() > max_len) {
res.clear();
res.push_back(tem);
max_len = tem.length();
} else if (tem.length() == max_len) {
res.push_back(tem);
}
tem.clear();
}
break;
}
tem += s[i];
} else {
if (!tem.empty()) {
if (tem.length() > max_len) {
res.clear();
res.push_back(tem);
max_len = tem.length();
} else if (tem.length() == max_len) {
res.push_back(tem);
}
tem.clear();
}
}
}
if (!res.empty()) {
for (auto x : res) {
cout << x;
}
cout << ',' << res[0].length() << endl;
} else {
cout << "No digits found\n";
}
}
return 0;
}
