这道题主要使用了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的更多相关文章

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

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

随机推荐

  1. POJ2741 Colored Cubes

    Description There are several colored cubes. All of them are of the same size but they may be colore ...

  2. JAVA 中的集合框架

    java集合框架提供了一套性能优良.使用方便的接口和类,它们位于java.util包中 一.集合与数组 数组:(可以存储基本数据类型)是用来存现对象的一种容器,但是数组的长度固定,不适合在对象数量未知 ...

  3. Ceilometer 数据库比较

    Ceilometer和其他OpenStack项目相比,部署中比较不同的是Ceilometer可以选用多种不同的后台数据库来保存测量值和警告器. 目前Ceilometer支持的后台数据库及及其相应的配置 ...

  4. JDK与JRE及其在Eclipse中的使用

    转载自:http://blog.csdn.net/gx1058742912/article/details/51033942 JDK与jRE的区别 JDK(java development kit): ...

  5. Spring Boot入门——JDBCTemplate使用及其相关问题解决

    1.在pom.xml文件中引入相应依赖 <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> & ...

  6. php get_magic_quotes_gpc()

    magic_quotes_gpc函数在php中的作用是判断解析用户输入的数据,如包括有:post.get.cookie过来的数据增加转义字符“\”,以确保这些数据不会引起程序异常,特别是数据库语句因为 ...

  7. Uploading files using ASP.NET Web Api

    http://chris.59north.com/post/Uploading-files-using-ASPNET-Web-Api

  8. Python爬虫之利用BeautifulSoup爬取豆瓣小说(二)——回车分段打印小说信息

    在上一篇文章中,我主要是设置了代理IP,虽然得到了相关的信息,但是打印出来的信息量有点多,要知道每打印一页,15个小说的信息全部会显示而过,有时因为屏幕太小,无法显示全所有的小说信息,那么,在这篇文章 ...

  9. 格式化SQL和逆格式SQL

    上篇说过SQLyog中的计算合适数据类型的功能,现在说下Navicat格式化SQL和逆格式化SQL 啥也不说了,一看图,一目了然,Beautify SQL(格式化,即变成最美的语句)和Minify S ...

  10. BW里转换简单常用ABAP

    用户查看的是当时的物料折让,那你必然要给物料加个时间,才好区分.总是以现在最新的物料状态查看历史数据会出现问题.当时这个物料是折让的,现在不折让了.数据会有问题.加个DSO.做一个时间记录.读取系统时 ...