【leetcode】482. License Key Formatting
problem
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的更多相关文章
- 【LeetCode】482. License Key Formatting 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 482. License Key Formatting - LeetCode
Question 482. License Key Formatting Solution 思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-就忽略,如果遍历了k个字符 ...
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- LeetCode算法题-License Key Formatting(Java实现)
这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...
- 482 License Key Formatting 注册码格式化
详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...
- 482. License Key Formatting
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- LeetCode_482. License Key Formatting
482. License Key Formatting Easy You are given a license key represented as a string S which consist ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
随机推荐
- js 快速生成数组的方法
//实现方法一:循环赋值var arr1 = new Array(100);for(var i=0;i<arr1.length;i++){ arr1[i] = i;}console.log(ar ...
- 详解Linux下swig 3.0.12的手动安装过程
详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...
- day2 购物车
需求: 商家入口: 1.商品列表永久保存(暂时使用存储在文件,也可以使用sqlite)里. 2.商家可以增加商品,也可以修改商品价格 买家入口: 1.购物车信息永久保存,暂时使用存储在文件,也可以使用 ...
- Python小札
在Python中,等号(=)是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量如: a = 123 #a是整数 print(a) a = 'ABC' #a变为字 ...
- netty解码器详解(小白也能看懂!)
什么是编解码器? 首先,我们回顾一下netty的组件设计:Netty的主要组件有Channel.EventLoop.ChannelFuture.ChannelHandler.ChannelPipe等. ...
- Android : Camera之camx hal架构
一.camx的代码结构 目前主流的机型都使用camx架构,这个架构和之前架构的主要区别就是 芯片接口层的代码从hardware/qcom 迁移到 vendor/qcom/proprietary/下面, ...
- wc.exe
1 /* 2 * 没能实现的功能:wc.exe -s递归处理目录下符合条件的文件 3 * wc.exe -x 显示图形界面 4 * 5 * 6 * 实现的功能: wc.exe -c显示文件的字符数. ...
- Exploit-Exercises nebule 旅行日志(三)
继续探索之路,经过昨天的题目,忽然有那么点开窍了,今天继续: 看题目,还是用level对应的级别的帐号和密码登录,flag02的程序源码如图上所示,getegid 和 geteuid就不说了,这个程序 ...
- Java 优化要点
java优化四个要素:1.-server服务器方式启动2.-Xms1g -Xmx1g初始大小与最大大小保持一致 -Xss256k线程栈从默认128扩到256 3.-Xmn512m新生代内存:年老代内 ...
- sprigmvc 传值jsp页面
https://blog.csdn.net/qq_41357573/article/details/84675535#如何将controller层值传递到jsp页面