Krononberg

1694. Reformat Phone Number.cpp 본문

개발 로그/알고리즘

1694. Reformat Phone Number.cpp

k._. 2021. 12. 15. 19:44

class Solution {
public:
    string reformatNumber(string number) {
        if(number.size()<3)return number;
        string nb ="";
        for(auto n : number) if(isdigit(n)) nb+=n;
        string res="";
        int x = nb.size()%3;
        
        if(x==1){
            for(int i =0; i<nb.size();i++){
                res+=nb[i];
                if(i==nb.size()-3) {
                    res+='-';
                    res+=nb[++i];
                    res+=nb[++i];
                    return res;
                }
                if(i%3 ==2) res+='-';
            }
        }
        for(int i =0; i<nb.size();i++){
            res+=nb[i];
            if(i!= nb.size()-1 && i%3 ==2) res+='-';
            
        }
        return res;
    }
};

'개발 로그 > 알고리즘' 카테고리의 다른 글

1078. Occurrences After Bigram.cpp  (0) 2021.12.15
1598. Crawler Log Folder.cpp  (0) 2021.12.15
575. Distribute Candies.cpp  (0) 2021.12.15
884. Uncommon Words from Two Sentences.cpp  (0) 2021.12.15
46. Permutations.cpp  (0) 2021.12.14