1，题目要求
You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.

Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.

Given a non-empty string S and a number K, format the string according to the rules described above.

2，题目思路

3，程序源码

class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string res = "";
for(auto a = S.rbegin();a<S.rend();a++)
{
if(*a !='-')
{
if(res.size()%(K+1) == K)
res += "-";
res += toupper(*a);
}
}
reverse(res.begin(), res.end());
return res;
}
};


rbegin() 和 rend()迭代器是一个指针，因此在调用时需要按照指针的方式进行使用。

 if(res.size()%(K+1) == K)

• 评论

• 上一篇
• 下一篇