详见:https://leetcode.com/problems/license-key-formatting/description/

C++:

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

参考:https://www.cnblogs.com/grandyang/p/6277972.html

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

    Now you are given a string S, which represents a software license key which we would like to format. ...

  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 解题报告(Python)

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

  6. 482. License Key Formatting

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

  7. LeetCode_482. License Key Formatting

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

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

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

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

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

随机推荐

  1. POJ2976 Dropping tests —— 01分数规划 二分法

    题目链接:http://poj.org/problem?id=2976 Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. import data from excel to sql server

    https://www.c-sharpcorner.com/article/how-to-import-excel-data-in-sql-server-2014/ 需要注意的是,第一次是选择sour ...

  3. Oracle:通过pl/sql developer工具导入excel数据

    1.在pl/sql developer中选择工具-->ODBC导入器 2.选择需要导入的EXCEL文件(CVS也可以):用户名.口令不用管,直接点“连接”,找到要导入的xls文件 3. 选择“导 ...

  4. WebStorm配置SVN

    下载SVN客户端管理工具TortoiseSVN-1.8.5.25224-x64-svn-1.8.8,选择合适的Windows版本 配置项目目录,对应的VCS为Subversion 设置Subversi ...

  5. POJ2976:Dropping tests(01分数规划入门)

    In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cum ...

  6. win7Setx修改环境变量

    SETX.exe (Resource Kit, Windows 7) Set environment variables permanently, SETX can be used to set En ...

  7. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 07. View的Model 和 Tag Helpers

    student添加一个属性BirthDate 然后把生成数据的地方,字段也加上 建立ViewModel list转换为ViewModel 进一步改进代码 StudentViewModel HomeIn ...

  8. Gym 100962G Green Day (找规律)

    题意:你用k 个生成树构成一个完全图. 析:n 个点的完全图有n(n-1)/2个边,一个生成树有n-1个边,你有k 个生成树 即边数等于 K(n-1) ,即  n(n-1)/2 == k(n-1)   ...

  9. UVaLive 6585 && Gym 100299F Draughts (暴力+回溯)

    题意:给定一个 10*10的矩阵,每一个W可以跳过一个B向对角走到#并把B吃掉,并且可以一直跳直到不能动为止,现在是W走的时候,问你最多吃几个B. 析:直接暴力+回溯,深搜就好. 代码如下: #pra ...

  10. Win32控制台程序和Win32应用程序

    刚接触Windows那一套,大多数概念都还没建立起来,整理了一下网上对“Win32控制台程序”的理解,谢谢各位网友了. win32控制台项目指在32位Windows命令提示符(即所谓的dos)环境下运 ...