题目描述:

You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.

Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.

Given a non-empty string S and a number K, format the string according to the rules described above.

Example 1:

Input: S = "5F3Z-2e-9-w", K = 4

Output: "5F3Z-2E9W"

Explanation: The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.

Example 2:

Input: S = "2-5g-3-J", K = 2

Output: "2-5G-3J"

Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.

Note:

  1. The length of string S will not exceed 12,000, and K is a positive integer.
  2. String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
  3. String S is non-empty.

要完成的函数:

string licenseKeyFormatting(string S, int K)

说明:

1、给定一个字符串S和一个正数K,字符串中只含有字母、数字和破折号,比如5F3Z-2e-9-w。

要求将字符串的格式重新编排,使得被破折号隔开的每个部分含有K个字符,除了最开始的第一个部分可以是小于等于K个字符的,但必须至少有一个字符。

同时要求字符串中的小写字母转化为大写字母。

比如字符串是5F3Z-2e-9-w,K=4,那么重新编排完格式是5F3Z-2E9W。

如果字符串是9-5F3Z-2e-9-w,K=4,那么重新编排完格式就是9-5F3Z-2E9W。

2、题意清晰,这道题我们要从字符串的最后开始处理,这样比较方便。

代码如下:(附详解)

    string licenseKeyFormatting(string S, int K)
{
string res;//最后要返回的字符串
int i=S.size()-1,count=0;
while(i>=0)
{
if(S[i]!='-')//如果这个字符是字母或者数字
{
count++;
if(count<=K)//如果还没有达到K个
{
res+=char(toupper(S[i]));//大小写转换,增加到res中
i--;
}
else//如果达到了K个
{
res+='-';//插入'-'
count=0;//重新开始计数
}
}
else//如果这个字符是'-'
i--;
}
reverse(res.begin(),res.end());//最后反转一下,就是我们要的字符串。
return res;
}

上述代码实测12ms,beats 98.58% of cpp submissions。

3、一些其他说明:

可能有的同学写的也是跟笔者一样的2中的代码,只不过在res+=char(toupper(S[i]))这里,改成了res=char(toupper(S[i]))+res。这样最后就不用reverse了。

但这样提交了之后会发现花费时间巨大,是2中代码花费时间的20倍左右。

原因是res=char(toupper(S[i]))+res处理的时候,可以看做是重新定义一个临时字符串,把S[I]的值放进去,然后再增加了res,最后把临时字符串赋给了res。

而res+=char(toupper(S[i]))是直接在res后面增加了S[i],类似于vector.push_back()。

两者相比较起来,还是2中的方法快速,最后做一下reverse其实花费时间也不多。

leetcode-482-License Key Formatting的更多相关文章

  1. [LeetCode] 482. License Key Formatting 注册码格式化

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

  2. 【leetcode】482. License Key Formatting

    problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...

  3. 482. License Key Formatting - LeetCode

    Question 482. License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符 ...

  4. 【LeetCode】482. License Key Formatting 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 482 License Key Formatting 注册码格式化

    详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...

  6. 482. License Key Formatting

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  7. LeetCode_482. License Key Formatting

    482. License Key Formatting Easy You are given a license key represented as a string S which consist ...

  8. [LeetCode] License Key Formatting 注册码格式化

    Now you are given a string S, which represents a software license key which we would like to format. ...

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

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

  10. LeetCode算法题-License Key Formatting(Java实现)

    这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...

随机推荐

  1. 滑动窗口的最大值 · sliding-window-maximum

    [抄题]: Given an array nums, there is a sliding window of size k which is moving from the very left of ...

  2. Git忽略规则及.gitignore规则不生效的解决办法(转)

    在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改根目录中 .gitignore 文件的方法(如无,则需自己手工建立此文件).这个文件每一行保存了一个匹配的规则例如: # 此为注 ...

  3. cmake get_filename_component

    get_filename_component Get a specific component of a full filename. get_filename_component(<VAR&g ...

  4. 使用delphi 开发多层应用(二十一)使用XE5 RESTClient 直接访问kbmmw 数据库

    delphi XE5 出来了,增加了android 的开发支持,另外增加了一个RESTClient 来支持访问REST 服务器. 这个功能非常强大,可以直接使用非常多的REST 服务器.同时也可以支持 ...

  5. excel定位函数

    在Excel中MATCH函数可以返回指定内容所在的位置,而INDEX又可以根据指定位置查询到位置所对应的数据,各取其优点,我们可以返回指定位置相关联的数据.       MATCH函数(返回指定内容所 ...

  6. Uploadify多文件上传插件.NET使用案例+PHP使用案例

    ploadify是一个非常好用的多文件上传插件 插件下载:http://www.uploadify.com 下载后需要用到的文件: 接下来就是直接添加代码: Default.aspx代码 <%@ ...

  7. 大数据项目中js中代码和java中代码(解决Tomcat打印日志中文乱码)

    Idea2018中集成Tomcat9导致OutPut乱码找到tomcat的安装目录,打开logging.properties文件,增加一行代码,覆盖默认设置,将日志编码格式修改为GBK.java.ut ...

  8. R12.1.3 patch9239090

    参考文档:Oracle E-Business Suite Release 12.1.3 Readme [ID 1080973.1]1.调整参数_disable_fast_validate=TRUEpg ...

  9. SQL -- What Tables Queries are Used to Display the Counts in the Inventory Account Periods form (INVTTGPM.fmb) (Doc ID ID 357997.1)

    Applies to: Oracle Inventory Management - Version 11.5.9 to 12.1.3 [Release 11.5 to 12.1] Informatio ...

  10. 作业一:博客和Github简单练习

    (1)自我介绍 Hello everybody! 我叫纪杨阳,学号1413042002,网络工程141班. 本人没啥特殊的兴趣爱好,都是些平常得不能再平常的吃吃睡睡.要说感兴趣的,可能就是音乐和服饰还 ...