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

  1. string licenseKeyFormatting(string S, int K) {
  2. transform(S.begin(), S.end(), S.begin(), ::toupper);
  3. string oldStr = "-";
  4. string newStr = "";
  5. while (true) {
  6. string::size_type pos();
  7. if ((pos = S.find(oldStr)) != string::npos)
  8. {
  9. S.replace(pos, oldStr.length(), newStr);
  10. }
  11. else
  12. {
  13. break;
  14. }
  15. }
  16.  
  17. vector<string> V;
  18. int len = S.length();
  19. int firstPart = len % K;
  20. int Parts = len / K;
  21. if (Parts == )
  22. {
  23. return S;
  24. }
  25. V.push_back(S.substr(, firstPart));
  26. if (firstPart != )
  27. {
  28. V.push_back("-");
  29. }
  30. for (int i = ; i < Parts; i++)
  31. {
  32. V.push_back(S.substr(firstPart + i*K, K));
  33. if (i != Parts - )
  34. {
  35. V.push_back("-");
  36. }
  37. }
  38.  
  39. string R;
  40. for (int i = ; i < V.size(); i++)
  41. {
  42. R += V[i];
  43. }
  44. return R;
  45. }

leetcode482的更多相关文章

  1. [Swift]LeetCode482. 密钥格式化 | License Key Formatting

    You are given a license key represented as a string S which consists only alphanumeric character and ...

随机推荐

  1. codeforces 705B:Spider Man

    Description Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a ...

  2. ​Php加速原理及工具试验

    Php加速原理及工具测试 本实验相关软件地址:http://pan.baidu.com/s/1dDuwvE5 第一部分.Php加速分类: 一.缓冲层级别的优化 1.xCache是把 PHP 操作码缓存 ...

  3. 算法练习3---水仙花数java版

    所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身. java程序代码如下: public class ArithTest { public static void ...

  4. New Concept English three (51)

    22 76 Predicting the future is notoriously difficult. Who could have imagined, in the mid 1970s, for ...

  5. 关于/usr/bin/ld: cannot find -lcrypto 的错误

    Linux下 build code 时,要做 -lssl, -lcrypto 的链接,出现类似下面的错误: /usr/bin/ld: cannot find -lcrypto /usr/bin/ld: ...

  6. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  7. BZOJ5334: [Tjoi2018]数学计算

    BZOJ5334: [Tjoi2018]数学计算 https://lydsy.com/JudgeOnline/problem.php?id=5334 分析: 线段树按时间分治即可. 代码: #incl ...

  8. loj 6083.「美团 CodeM 资格赛」数码

    题目: 给定两个整数\(l\)和\(r\),对于任意\(x\),满足\(l\leq x\leq r\),把\(x\)所有约数写下来. 对于每个写下来的数,只保留最高位的那个数码.求\([1,9]\)中 ...

  9. 【模板】【学习笔记】noip数学

    一.素数 欧拉筛 void prime(){ check[]=; ;i<=n;i++){ if(!check[i])prim[++cnt]=i;//这个if语句后面没有大括号!! ;j<= ...

  10. GO语言list剖析

    GO语言list剖析 本节内容 使用方法 list提供的方法 源码剖析 1. 使用方法 在GO语言的标准库中,提供了一个container包,这个包中提供了三种数据类型,就是heap,list和rin ...