文章目录
题目
代码(8.30 首刷自解)
其实不算自解,去网上搜了很多string的方法。。
class Solution {
public:
string maskPII(string s) {
if(find(s.begin(), s.end(), '@') != s.end()) {
string ans;
auto index = s.find('@');
ans = string(1, tolower(s[0])) + "*****" + string(1, tolower(s[index-1])) + "@";
auto tmp = s.substr(index+1);
ans += convert(tmp);
return ans;
} else {
int n = 0;
// 求号码长度
for(char& c : s) {
if(isdigit(c))
n++;
}
// 国家代码的长度
int k = n-10;
string back, ans;
int i = s.size()-1;
// 获取后面四个数字
while(back.size() != 4) {
if(isdigit(s[i])) {
back.insert(0, string(1, s[i]));
}
i--;
}
if(k == 0) {
ans = "***-***-" + back;
} else {
string kk;
ans = "+" + string(k, '*') + "-***-***-" + back;
}
return ans;
}
}
string convert(string& s) {
for(char& c : s) {
if(c >= 'A' && c <= 'Z')
c += 32;
}
return s;
}
};