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();
- 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 == )
- {
- return S;
- }
- V.push_back(S.substr(, firstPart));
- if (firstPart != )
- {
- V.push_back("-");
- }
- for (int i = ; i < Parts; i++)
- {
- V.push_back(S.substr(firstPart + i*K, K));
- if (i != Parts - )
- {
- V.push_back("-");
- }
- }
- string R;
- for (int i = ; i < V.size(); i++)
- {
- R += V[i];
- }
- return R;
- }
leetcode482的更多相关文章
- [Swift]LeetCode482. 密钥格式化 | License Key Formatting
You are given a license key represented as a string S which consists only alphanumeric character and ...
随机推荐
- codeforces 705B:Spider Man
Description Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a ...
- Php加速原理及工具试验
Php加速原理及工具测试 本实验相关软件地址:http://pan.baidu.com/s/1dDuwvE5 第一部分.Php加速分类: 一.缓冲层级别的优化 1.xCache是把 PHP 操作码缓存 ...
- 算法练习3---水仙花数java版
所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身. java程序代码如下: public class ArithTest { public static void ...
- New Concept English three (51)
22 76 Predicting the future is notoriously difficult. Who could have imagined, in the mid 1970s, for ...
- 关于/usr/bin/ld: cannot find -lcrypto 的错误
Linux下 build code 时,要做 -lssl, -lcrypto 的链接,出现类似下面的错误: /usr/bin/ld: cannot find -lcrypto /usr/bin/ld: ...
- 1110. Complete Binary Tree (25)
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- BZOJ5334: [Tjoi2018]数学计算
BZOJ5334: [Tjoi2018]数学计算 https://lydsy.com/JudgeOnline/problem.php?id=5334 分析: 线段树按时间分治即可. 代码: #incl ...
- loj 6083.「美团 CodeM 资格赛」数码
题目: 给定两个整数\(l\)和\(r\),对于任意\(x\),满足\(l\leq x\leq r\),把\(x\)所有约数写下来. 对于每个写下来的数,只保留最高位的那个数码.求\([1,9]\)中 ...
- 【模板】【学习笔记】noip数学
一.素数 欧拉筛 void prime(){ check[]=; ;i<=n;i++){ if(!check[i])prim[++cnt]=i;//这个if语句后面没有大括号!! ;j<= ...
- GO语言list剖析
GO语言list剖析 本节内容 使用方法 list提供的方法 源码剖析 1. 使用方法 在GO语言的标准库中,提供了一个container包,这个包中提供了三种数据类型,就是heap,list和rin ...