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. Matlab绘图基础——用print函数保存图片(Print figure or save to file)

    print(figure_handle,'formats','-rnumber','filename')  %将图形保存为png格式,分辨率为number的(默认为72),最好指定的分辨率大一点,否则 ...

  2. [bzoj1601]灌水_kruskal

    灌水 bzoj-1601 题目大意:给你n块地,将两块地之间连通有代价$P_{i,j}$,单独在一块地打井需要代价$C_i$,问将所有的井都有水的代价是多少. 注释:1<=n<=300. ...

  3. 爬虫(scrapy--豆瓣TOP250)

    # -*- coding: utf-8 -*- import scrapy from douban_top250.items import DoubanTop250Item class MovieSp ...

  4. Jedis操作Redis

    Jedis操作Redis的常用封装方法 @Resource(name="jedispool") private JedisPool pool=null; /** * 设置缓存对象过 ...

  5. JavaScript(第十八天)【DOM基础】

    学习要点: 1.DOM介绍 2.查找元素 3.DOM节点 4.节点操作 DOM(Document Object Model)即文档对象模型,针对HTML和XML文档的API(应用程序接口).DOM描绘 ...

  6. 201621123043 《Java程序设计》第6周学习总结

    1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖面向对象的 ...

  7. mysql基础篇 - SELECT 语句详解

    基础篇 - SELECT 语句详解         SELECT语句详解 一.实验简介 SQL 中最常用的 SELECT 语句,用来在表中选取数据,本节实验中将通过一系列的动手操作详细学习 SELEC ...

  8. C# 大数组赋值给小数组,小数组赋值给大数组

    ]; ]; " }; arraymax = arraystr;//变成和arraystr一样 arraymin = arraystr;//变成和arraystr一样

  9. Excel+DDT数据驱动实例

    一.首先安装dtt模块 数据驱动原理 1.测试数据为多个字典的list类型 2.测试类前加修饰@ddt.ddt 3.case前加修饰@ddt.data() 4.运行后用例会自动加载成N个单独的用例 二 ...

  10. PAT1048. Find Coins(01背包问题动态规划解法)

    问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...