zoukankan      html  css  js  c++  java
  • leetcode482

    这道题主要使用了C++的几个API,大小写转化,字符串替换。其余的逻辑都比较简单。而且经查资料,string类字符串拼接的速度使用+=的速度是很快的。以下代码,也是用的+=来拼接字符串。

        string licenseKeyFormatting(string S, int K) {
            transform(S.begin(), S.end(), S.begin(), ::toupper);
            string oldStr = "-";
            string newStr = "";
            while (true) {
                string::size_type   pos(0);
                if ((pos = S.find(oldStr)) != string::npos)
                {
                    S.replace(pos, oldStr.length(), newStr);
                }
                else
                {
                    break;
                }
            }
    
            vector<string> V;
            int len = S.length();
            int firstPart = len % K;
            int Parts = len / K;
            if (Parts == 0)
            {
                return S;
            }
            V.push_back(S.substr(0, firstPart));
            if (firstPart != 0)
            {
                V.push_back("-");
            }
            for (int i = 0; i < Parts; i++)
            {
                V.push_back(S.substr(firstPart + i*K, K));
                if (i != Parts - 1)
                {
                    V.push_back("-");
                }
            }
    
            string R;
            for (int i = 0; i < V.size(); i++)
            {
                R += V[i];
            }
            return R;
        }
  • 相关阅读:
    Linux 实战
    bash 环境配置及脚本
    Linux vi/vim
    Linux 正则表达式
    001 KNN分类 最邻近算法
    测序名解
    流式细胞术
    CircRNA 环化RNA
    笔记总结
    Flume
  • 原文地址:https://www.cnblogs.com/asenyang/p/9703177.html
Copyright © 2011-2022 走看看