482. License Key Formatting

0人阅读 评论(0) 收藏 举报
分类:

1,题目要求
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.
这里写图片描述
将一个用’-‘分割的字符串划分为给定组长度的字符串,其中,第一个块的长度可以不是给定长度。
字符串由数字和字母组成,要求输出的字符串全部由大写字母组成。

2,题目思路
首先,字符大小写转换可以用自带的函数来实现,也可以用ASCII的方式来实现(大写字母比小写字母的ASCII值小了32)。
普通的办法,就是先对字符串进行遍历,然后去掉”-“后,根据给定的K值来对”-“进行重新安排。
需要注意的是,因为第一个块没有长度限制,如果对其进行单独处理,虽然也可以做到,但是比较繁琐。
简便的办法就是从后往前进行字符串的遍历和处理。

3,程序源码

class Solution {
public:
    string licenseKeyFormatting(string S, int K) {
        string res = "";
        for(auto a = S.rbegin();a<S.rend();a++)
        {
            if(*a !='-')
            {
                if(res.size()%(K+1) == K)
                    res += "-";
                res += toupper(*a);
            }
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

rbegin() 和 rend()迭代器是一个指针,因此在调用时需要按照指针的方式进行使用。
作用是从后往前进行迭代。

 if(res.size()%(K+1) == K)

关于这一点,首先,因为如果给定的值为K(假如为2),那么每两个字符中间就要插入一个”-“,因此在求余时,就要mod K+1而不是K。
其次,之所以是 == K 而不是 ==0,是由于如果 ==0,会使得res的长度为0(初始),也会加入一个’-‘,也就是会在开头(实质上是尾部)加入一个错误’-‘,因此需要 ==K来解决这一问题。

查看评论

微信沙箱开发,微信社交立减金接口升级

-
  • 1970年01月01日 08:00

leetcode482: License Key Formatting

Now you are given a string S, which represents a software license key which we would like to format....
  • sinat_35425429
  • sinat_35425429
  • 2017-02-11 11:25:28
  • 140

Leetcode 482. License Key Formatting 序列号格式 解题报告

1 解题思想这题目就是让规整格式的。 输入了一些数字字母还有破折号混合的字符串序列号,和一个K 现在要求将这个序列号,去除原来的破折号格式,整理成每K个字母数字使用一个破折号的格式,如果不能每组都...
  • MebiuW
  • MebiuW
  • 2017-01-13 16:14:39
  • 1180

482. License Key Formatting**

Now you are given a string S, which represents a software license key which we would like to format....
  • alwaystry
  • alwaystry
  • 2017-01-19 11:03:28
  • 186

leetcode 482. License Key Formatting(C语言,标准化密钥)25

leetcode 482. License Key Formatting(C语言,标准化密钥,解析及解决代码
  • m0_37454852
  • m0_37454852
  • 2017-10-07 12:15:53
  • 58

[leetcode] 482. License Key Formatting

Now you are given a string S, which represents a software license key which we would like to format....
  • TstsUgeg
  • TstsUgeg
  • 2017-01-12 18:07:08
  • 512

LeetCode 482. License Key Formatting

LeetCode 482. License Key Formatting
  • junchen1992
  • junchen1992
  • 2017-01-08 22:25:31
  • 299

【Leetcode】482. License Key Formatting

482. License Key Formatting Now you are given a string S, which represents a software license k...
  • zhizhi_zhizhi_zhizhi
  • zhizhi_zhizhi_zhizhi
  • 2017-01-15 14:48:03
  • 96

482. License Key Formatting (M)

Now you are given a string S, which represents a software license key which we would like to format....
  • SHURamos
  • SHURamos
  • 2017-01-18 20:30:20
  • 119

[leetcode]482. License Key Formatting

题目链接:https://leetcode.com/problems/license-key-formatting/?tab=Description Now you are given a str...
  • xiaocong1990
  • xiaocong1990
  • 2017-03-03 11:40:14
  • 126
    个人资料
    持之以恒
    等级:
    访问量: 4749
    积分: 1775
    排名: 2万+
    文章存档