problem

482. License Key Formatting

solution1:

倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序。

class Solution {
public:
string licenseKeyFormatting(string S, int K) {
if(S.size()<=) return NULL;
int cnt = ;
string ans = "";
for(int i=S.size()-; i>=; i--)//倒着处理.
{
char c = S[i];
if(S[i]=='-') continue;
else if(S[i]>='a'&&S[i]<='z') c = S[i] - ;
ans.push_back(c);
cnt++;
if(cnt%K==)//
{
ans.push_back('-');
}
}
//if(!ans.empty() && ans.back()=='-') ans.pop_back();
int s = ans.size();
string ans1;
if(ans.back()=='-') ans1 = ans.substr(, s-);
else ans1 = ans;
return string(ans1.rbegin(), ans1.rend());// }
};

参考

1. Leetcode_482. License Key Formatting;

2. GrandYang;

【leetcode】482. License Key Formatting的更多相关文章

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

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

  2. 482. License Key Formatting - LeetCode

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

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

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

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

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

  5. 482 License Key Formatting 注册码格式化

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

  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. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  9. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

随机推荐

  1. css实现横向带箭头步骤流程效果

    通过纯css实现圆角的步骤流程效果,扩展性强,可以随意增加减少数量,兼容性ie6+. 具体代码: <style type="text/css"> ul{margin:0 ...

  2. [record]WebLogic域之创建-文本界面

    WebLogic域的创建方式主要有图形界面.文本方式(字符界面).脚本方式(WLST脚本)等,本文用文本方式来创建域. 一.启动Configuration Wizard •config.cmd/con ...

  3. 第一次提交 nacos 代码

    新建 issue认领 issuefork修改 nacos 项目 .git/config 中的用户信息pushcreate pull request 到 develop 分支

  4. 在浏览器端获取文件的MD5值

    前几天接到一个奇怪的需求,要在web页面中计算文件的md5值,还好这个项目是只需兼容现代浏览器的,不然要坑死了. 其实对文件进行md5,对于后端来说是及其简单的.比如使用Node.js,只要下面几行代 ...

  5. 详解Linux下swig 3.0.12的手动安装过程

    详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...

  6. XAMPP本地服务器打不开解决方案

    第一步:先开启相关服务:如图 第二步:在浏览器上输入localhost:端口号,(或127.0.0.1:端口号),按回车,就成功登陆本地服务器. =========================== ...

  7. C# [WIN32] [API] Global Hook

    NativeMethods.cs: using System; using System.Runtime.InteropServices; namespace GlobalHook { interna ...

  8. js 原型原型链

    个人的理解,有什么不正确的请指教,共同学习 //声明一个构造函数Personfunction Person(name,age){ this.name = name; this.age = age;} ...

  9. Sublime Text3 调色板 ColorPicker插件安装及快捷键

    一.安装 第一步:打开菜单栏下的tools>command palette或是快捷键ctrl+shift+p输入PI 点击第一个安装包等待跳出窗口,输入ColorPicker,待安装完成 第二步 ...

  10. 2、CentOS下编译安装Python2.7.6(转)

    CentOS系统下面Python在升级到2.7.6的时候,没有找到安装包直接安装,只能通过源代码编译的方式来安装Python 2.7.6版本.这篇是编译和安装Python2.7.6的过程记录. Cen ...