【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 ...
随机推荐
- 某mac最简单调节亮度
说明 mac用户其实都可以使用 背景 很多人都说需要打补丁啊,或者修改什么的,我试过不管用.不如通过软件调节.. 正文 从appstore下载 Brightness Slider 即可,虽然不能达到系 ...
- json转换字符串
在使用json模块时需要先 import json 引入模块 json.dumps()模块函数 功能:将Python数据类型转换成字符串[有参] 使用方法:json.dumps(要转换的数据类型变量) ...
- 8、TypeScript-解构赋值
1.数组的解构赋值 2.对象的解构赋值 注意:在浏览器环境中,windows本身有一个成员name,所以要重新,语法为 属性名:重命名 3.函数的解构赋值
- [Err] 1093 - You can't specify target table 'master_data' for update in FROM clause
delete from master_data where category_id not in (select category_id from master_data a, bc_category ...
- C# [LINQ] Linq Expression 获取多格式文件
using System; using System.IO; using System.Linq; using System.Linq.Expressions; internal static str ...
- MySQL索引的原理,B+树、聚集索引和二级索引的结构分析
索引是一种用于快速查询行的数据结构,就像一本书的目录就是一个索引,如果想在一本书中找到某个主题,一般会先找到对应页码.在mysql中,存储引擎用类似的方法使用索引,先在索引中找到对应值,然后根据匹配的 ...
- Linux 动态链接库 - dll劫持
如何使用动态链接库 Linux下打开使用动态链接库需要三步(实际上和windows下基本一样):1.加载动态链接库,通过调用库函数dlopen()获得链接库的句柄,对应于windows下的 AfxLo ...
- 配置solr6.2
1 解压solr ,把 solr-6.2.0\server\solr-webapp下的 webapp 文件夹拷贝到tomcat 的webapps下,重命名为solr,也可以是其他名字: ( 注意,此处 ...
- spring cloud + spring boot + springmvc+mybatis分布式微服务云架构
做一个微服务架构需要的技术整理: View: H5.Vue.js.Spring Tag.React.angularJs Spring Boot/Spring Cloud:Zuul.Ribbon.Fei ...
- 课下作业——MyCP
作业要求 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容为 ...