【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 ...
随机推荐
- sprigcloud Eureka Server环境搭建
1.搭建springcloud的Erueka组件,现在搭建这些套件已经变的很方便了,进入https://start.spring.io/页面,如下图: 2.选择好Eureka Server,点击Gen ...
- vue2.0项目 calendar.js(日历组件封装)
最近一直闲来无事,便寻思着做一下自己的个人项目,也想说能使用现在比较流行的一些mvvm框架来做,于是就选用了这样的一个技术栈vue2.0+vue-router+vuex+webpack来做,做得也是多 ...
- Word中使用宏处理表格内容 小记
不解释,直接上代码... Sub 删除表格列WT0818() '此符号是注释 ' 删除表格列WT0818 宏 ' For Each tb In ActiveDocument.Tables //wor ...
- 【推荐】Data Structure Visualizations
University of San Francisco David Galles 功能:可视化数据结构&算法实现过程 网站地址 https://www.cs.usfca.edu/~ga ...
- GCP 谷歌云平台申请教程
最近为了学个国外的课程,想要用谷歌云平台的GPU,谷歌云平台,新注册,赠送300美金,免费用一年.注册的时候发现,必须要有国外的信用卡,网上搜索,并试了几个解决方案. 1.不用信用卡,能不能申请成功? ...
- vue中父子组件的通信
1.父组件向子组件传递数据 父组件传递:data = parent.data 子组件接收props: {data:{}} 2.子组件向父组件传递数据(https://vuefe.cn/v2/guide ...
- 外贸站全球网速测试+免费CDN使用教程
关于外贸网站速度测试,以前一全老师(www.yiquanseo.com)也讲到过,但是在那篇文章中推荐给大家的两个测试网站(https://developers.google.com/speed/pa ...
- c# 类的历遍和历遍操作
string id = Request.Form["id"]; string type = Request.Form["type"]; string info ...
- js插件ztree使用
最新给公司后台写了一个配置页面,在网上搜到一个js插件ztree,记录一下使用心得. 首先说一下ztree官网,好多方法我都是从官网api上学习的,官网地址http://www.treejs.cn/v ...
- 关于web项目中静态资源加载不了的一些解决思路
问题的产生: <!--springMVC前端控制器加载--> <servlet> <servlet-name>springmvc</servlet-name& ...