- #刷题
- #leetcode
482. License Key Formatting C# o(n) 无法通过,求优化

4061
显示:34 / 38 test cases passed.
求优化
[mw_shl_code=objc,true]public class Solution {
public string LicenseKeyFormatting(string S, int K) {
S=S.ToUpper();
string result="";
int p=S.Length-1;
while(p>=0){
int cnt=K;
string ss="";
while(cnt>0 && p>=0){
if(S=='-'){
p--;
}
else{
ss=S+ss;
p--;
cnt--;
}
}
if(ss!=""){
result='-'+ss+result;
}
}
return result.Substring(1);
}
}[/mw_shl_code]
求优化
[mw_shl_code=objc,true]public class Solution {
public string LicenseKeyFormatting(string S, int K) {
S=S.ToUpper();
string result="";
int p=S.Length-1;
while(p>=0){
int cnt=K;
string ss="";
while(cnt>0 && p>=0){
if(S=='-'){
p--;
}
else{
ss=S+ss;
p--;
cnt--;
}
}
if(ss!=""){
result='-'+ss+result;
}
}
return result.Substring(1);
}
}[/mw_shl_code]
1条回复
热度排序