leetcode-482-License Key Formatting
题目描述:
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:
- The length of string S will not exceed 12,000, and K is a positive integer.
- String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
- 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的更多相关文章
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- 【leetcode】482. License Key Formatting
problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...
- 482. License Key Formatting - LeetCode
Question 482. License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符 ...
- 【LeetCode】482. License Key Formatting 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 482 License Key Formatting 注册码格式化
详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...
- 482. License Key Formatting
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- LeetCode_482. License Key Formatting
482. License Key Formatting Easy You are given a license key represented as a string S which consist ...
- [LeetCode] License Key Formatting 注册码格式化
Now you are given a string S, which represents a software license key which we would like to format. ...
- [Swift]LeetCode482. 密钥格式化 | License Key Formatting
You are given a license key represented as a string S which consists only alphanumeric character and ...
- LeetCode算法题-License Key Formatting(Java实现)
这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...
随机推荐
- Linux&&Mac 自动增加CSDN访问量
我心里面有两个小人. 一个叫愧疚,对CSDN这么一个分享知识的平台的愧疚,因为我正在做一件对不起CSDN的事情. 一个叫虚荣,对CSDN访问量的渴望过渡使得我踏出了这一步. 这一步,踏入了深渊.. 最 ...
- mysql中timestamp简单用法
该时间字段有比较特殊的地方,显示内容datetime字段一样.当取值为null或者不赋值时,显示当前系统时间,然后在其他地区读取的时候会根据当地的时间转换成当地的系统时间.
- ruby变量
Ruby 支持五种类型的变量. 一般小写字母.下划线开头:变量(Variable).局部变量的作用域从 class.module.def 或 do 到相对应的结尾或者从左大括号到右大括号 {}. 当调 ...
- http头部信息
1.常见的返回码 100: 请服务器端继续返回 200:成功 301:永久重定向 存的地址永久的改变了 301 302 : 暂时重定向 302仍然使用老得url 401 : 无法找到资源file n ...
- Linux的系统引导
启动引导: 1.主机加电自检,加载BIOS信息 2.读取MBR的引导文件[grub lilo] 3.引导linux内核 4.运行第一个进程init pid=1 5.进入相应的运行级别[0-6] 6.运 ...
- (广搜)可口可乐 -- hdu -- 1495
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1495 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- string 转换为枚举对应的值
public static Object Parse(Type enumType,string value) 例如:(Colors)Enum.Parse(typeof(Colors), "R ...
- 基于SSH的网上图书商城-JavaWeb项目-有源码
开发工具:Myeclipse/Eclipse + MySQL + Tomcat 项目简介: 技术:Java:JSP:JDBC,struts2,spring,hibernate数据库: mysqlweb ...
- 自己写一个图片按钮(XAML)
有时需要用三张图片(正常状态,鼠标移上,鼠标按下)来作为一个按钮的样式,虽然这种做法不好,应该用矢量的方式制作样式,但有的时候还是需要这样做的. 每次都修改按钮的样式来实现这个做法,既麻烦又会生成大段 ...
- Postgres-XL9.5r1.6 搭建
Postgres-XL9.5r1.6 安装部署1,环境准备 关闭防护墙 关闭selinux 下载依赖 yum install -y flex bison readline-devel zlib-dev ...