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

时间:2022-08-23 08:36:00

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. &lbrack;LeetCode&rsqb; 482&period; 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&period; License Key Formatting

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

  4. LeetCode&lowbar;482&period; License Key Formatting

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

  5. &lbrack;Swift&rsqb;LeetCode482&period; 密钥格式化 &vert; License Key Formatting

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

  6. 【LeetCode】482&period; License Key Formatting 解题报告(Python)

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

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

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

  8. 482&period; License Key Formatting

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

  9. Sublime Text 注册码 License Key

    Sublime Text (3103版本可用) 注册码 License Key  

随机推荐

  1. 解决方案:带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载可能出现 COM 组件的80040154错误

      建议大家在微软的组件出现问题时,在GOOGLE上搜索解决方案,一般来说,总有结果:  带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载,可能出现 COM 组件的80 ...

  2. Row&lowbar;Number&lpar;&rpar; OVER 的用法

    Row_Number() OVER 的用法: 为每一条分组记录返回一个数字. 参考博友 , 博友二

  3. python高级编程之我不测试

    # -*- coding: utf-8 -*-__author__ = 'Administrator'#测试驱动开发(高级编程处学习,第11章)#测试驱动开发也叫TDD, 是制造高质量软件的一种简单技 ...

  4. golang http server分析&lpar;一&rpar;

    http:http请求过程实质上是一个tcp连接通信,具体通过socket接口编码实现 在go中是通过listenAndServer()方法对外提供了一个http服务,在该方法中完成了socket的通 ...

  5. 操作系统,时间片轮转算法的C语言实现Round Robin

    #include "windows.h" #include <conio.h> #include <stdlib.h> #include <fstre ...

  6. zabbix回顾

    1.zabbix能收集哪些信息? 磁盘空间,磁盘IO,cpu负载,内存使用情况,开机时间,网卡的网络流量,进程数等 2.zabbix支持哪些通讯方式? agent:通过专用的代理程序进行监控,是mas ...

  7. mac 安装2019Adobe全家桶

    链接:https://pan.baidu.com/s/1UrAnB5fmHKmYPESvqkU3Rg  密码:803f 下载大师版, 进入解压后的文件夹,双击安装程序 打开了安装界面,进入下图中的安装 ...

  8. VirtualBox中slitaz系统不能联网

    首先,关于VirtualBox虚拟机中安装slitaz操作系统中,先不讲,现在假设电脑中已经装好了VirtualBox,并且已经装好了slitaz操作系统,一个轻量版的linux发行版本. 右上角我画 ...

  9. 练习题&vert;MySQL

    MySQL主要内容: 1.数据库介绍.类型.特性2.MySQL数据库安装.连接.启动.停止3.表字段类型介绍.主键约束.表创建语句4.常用增删改查语句.分组.聚合5.外键管理.unique字段.表结构 ...

  10. Angular2学习笔记

    Angular2 这里 Angular2 是指采用 TypeScript 语言的 Angular 2.0及以上版本.与采用 JavaScript 语言的 AngularJS 相比,Angular2 不 ...