1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 string licenseKeyFormatting(string S, int K) 12 { 13 int len=S.length(); 14 int count=0; 15 string res; 16 for(int i=len-1;i>=0;i--) 17 { 18 char c=toupper(S[i]); 19 if(c=='-') 20 continue; 21 res.push_back(c); 22 count++; 23 if(count==K) 24 { 25 res.push_back('-'); 26 count=0; 27 } 28 } 29 if(res.back()=='-') 30 res.pop_back(); 31 reverse(res.begin(),res.end()); 32 return res; 33 } 34 };
从后往前扫描,问题不大
浙公网安备 33010602011771号