博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode-482-License Key Formatting
阅读量:7086 次
发布时间:2019-06-28

本文共 2655 字,大约阅读时间需要 8 分钟。

题目描述:

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 = 4Output: "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 = 2Output: "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其实花费时间也不多。

转载于:https://www.cnblogs.com/chenjx85/p/9159194.html

你可能感兴趣的文章
Maven学习总结(四)——Maven核心概念
查看>>
linux shell中,单引号、 双引号,反引号(``),$()的区别
查看>>
Java8 十大新特性详解
查看>>
多线程的同步介绍
查看>>
开源js框架中各种你看不懂的js语法解释大全!!
查看>>
Mysql学习总结(9)——MySql视图原理讲解与使用大全
查看>>
linux挂载ipsan服务器
查看>>
Java持续集成(二)-- 整合以及使用
查看>>
什么是智能DNS解析?
查看>>
Mybatis应用学习(2)——配置文件编写
查看>>
一个图灵API的调用
查看>>
oracle-后台进程
查看>>
HTML5新特性:元素的classList属性与应用
查看>>
PHP开发会员系统
查看>>
asp.net c#轻松调用百度在线翻译功能
查看>>
我的友情链接
查看>>
DNS Server 3 子域的授权
查看>>
2013-05-25
查看>>
Initializing the Oracle ASMLib driver: [FAILED]
查看>>
innodb_force_recovery强制修复MySQL异常关闭问题
查看>>