参加了两家小小小公司的面试,有几道比较有趣记录了下来
1.字符串压缩
不考虑字符数超过9。
#include <string>
#include <iostream>
std::string solution(std::string s) {
std::string ans;
int i = 0, j = 0;
while (i < int(s.length())) {
while (s[i] == s[j]) ++i;
ans += s[j];
ans += std::to_string(i - j);
j = i;
}
return ans;
}
int main() {
// 输入
std::string s;
std::cin >> s;
// 字符串压缩
std::string ans = solution(s);
// 输出
std::cout << ans;
}
考虑字符数超过9。
#include <string>
#include <iostream>
std::string solution(std::string s) {
std::string ans;
int i = 0, j = 0;
w