Now you are given a string S, which represents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes in the given string are possibly misplaced.

We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character). To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case.

So, you are given a non-empty string S, representing a license key to format, and an integer K. And you need to return the license key formatted according to the description above.

Example 1:

Input: S = "2-4A0r7-4k", K = 4

Output: "24A0-R74K"

Explanation: The string S has been split into two parts, each part has 4 characters.

Example 2:

Input: S = "2-4A0r7-4k", K = 3

Output: "24-A0R-74K"

Explanation: The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said 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.

这道题让我们对注册码进行格式化,正确的注册码的格式是每四个字符后面跟一个短杠,每一部分的长度为K,第一部分长度可以小于K,另外,字母必须是大写的。那么由于第一部分可以不为K,那么我们可以反过来想,我们从S的尾部往前遍历,把字符加入结果res,每K个后面加一个短杠,那么最后遍历完再把res翻转一下即可,注意翻转之前要把结尾的短杠去掉(如果有的话),参见代码如下:

解法一:

class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string res = "";
int cnt = , n = S.size();
for (int i = n - ; i >= ; --i) {
char c = S[i];
if (c == '-') continue;
if (c >= 'a' && c <= 'z') c -= ;
res.push_back(c);
if (++cnt % K == ) res.push_back('-');
}
if (!res.empty() && res.back() == '-') res.pop_back();
return string(res.rbegin(), res.rend());
}
};

上面代码可以进一步精简到下面这种,我们用到了自带函数toupper,把字母转为大写格式,参见代码如下:

解法二:

class Solution {
public:
string licenseKeyFormatting(string S, int K) {
string res = "";
for (int i = (int)S.size() - ; i >= ; --i) {
if (S[i] != '-') {
((res.size() % (K + ) - K) ? res : res += '-') += toupper(S[i]);
}
}
return string(res.rbegin(), res.rend());
}
};

参考资料:

https://discuss.leetcode.com/topic/74995/java-5-lines-clean-solution

https://discuss.leetcode.com/topic/74925/short-and-fast-java-solution

https://discuss.leetcode.com/topic/74993/4-line-c-concise-solution-to-scan-string-backward

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 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. 482 License Key Formatting 注册码格式化

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

  3. 【leetcode】482. License Key Formatting

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

  4. 482. License Key Formatting - LeetCode

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

  5. LeetCode_482. License Key Formatting

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

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

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

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

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

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

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

  9. 482. License Key Formatting

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

随机推荐

  1. oracle数据库--序列(sequence)

    一个问题: 在某张表中,存在一个id列(整数),我们希望在添加记录的时候,该列从1开始,自动的增长,怎么处理? 解决方式:oracle是利用"序列"(sequence)来完成的. ...

  2. 【动态规划】滚动数组的求解(C++)

    虽然接触动态规划算法已经有一段时间,给一个01背包问题,能够做到一个表格简单粗暴下去,然后求得结果,但心里总觉得对这个算法理解十分不到位,抱着对算法的热爱,网上很多大牛的算法思维实在让我佩服的五体投地 ...

  3. Java基础学习笔记二十七 DBUtils和连接池

    DBUtils 如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils.DBUtils就是JDBC的简化开发 ...

  4. 【iOS】swift-Binary operator '|' cannot be applied to two UIViewAutoresizing operands

    let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) addSubview(view) view.autoresi ...

  5. javascript抛物投栏(抛物线实践)

    平面内,到定点与定直线的距离相等的点的轨迹叫做抛物线.水平抛物线就是水平匀速,垂直加速的运动. 抛物线的性质:面内与一个定点F和一条定直线l 的距离相等的点的轨迹叫做抛物线. 定点F叫做抛物线的焦点. ...

  6. OpenShift实战(一):OpenShift高级安装

    1.1 服务器基本信息 本次安装采用一个master.5个node.3个etcd,node节点两块硬盘,60G磁盘用于docker storage,xxx改为自己的域名或主机名. 节点 功能 IP 内 ...

  7. 从同步阻塞聊到Java三种IO方式

    本文总结自 https://zhuanlan.zhihu.com/p/34408883, https://www.zhihu.com/question/19732473中愚抄的回答, http://b ...

  8. ASP.NET 访问项目网站以外的目录文件

    简单的说,可以通过在 IIS 添加虚拟目录的方法做到,获取访问路径的时候就用 HttpContext.Current.Server.MapPath("~/xxx"); 的方式. 下 ...

  9. Python内置函数(9)——int

    英文文档: class int(x=0) class int(x, base=10) Return an integer object constructed from a number or str ...

  10. Mego开发文档 - 数据属性生成值

    数据属性生成值 该功能用于在数据插入或更新时为指定属性生成期望的值,Mego提供了非常灵活的实现方式以满足各种数据提交时的自动赋值问题. 生成值目的及模式 在Mego中生成值的目的一定是插入数据或更新 ...