482. License Key Formatting - LeetCode
Question
Solution
思路:字符串转化为char数组,从后遍历,如果是大写字母就转化为小写字母,如果是-
就忽略,如果遍历了k个字符(排除-
)就追加一个-
。
Java实现1:insert版(StringBuilder的append()与insert()效率比较)
public String licenseKeyFormatting(String S, int K) {
StringBuilder sb = new StringBuilder();
char[] arr = S.toCharArray();
int count = 0;
for (int i = arr.length - 1; i >= 0; i--) {
char c = arr[i];
if (c == '-') continue;
if (count % K == 0) sb.insert(0, '-');
if (c >= 'a' && c <= 'z') c -= 32;
sb.insert(0, c);
count++;
}
// return sb.substring(0, sb.length() - 1); // "---" 不通过
return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "";
}
Java实现2:append版
public String licenseKeyFormatting(String S, int K) {
StringBuilder sb = new StringBuilder();
// char[] arr = S.toCharArray();
int count = 0;
for (int i = S.length() - 1; i >= 0; i--) {
char c = S.charAt(i);
if (c == '-') continue;
if (count % K == 0) sb.append('-');//sb.insert(0, '-');
if (c >= 'a' && c <= 'z') c -= 32;
sb.append(c);// sb.insert(0, c);
count++;
}
// return sb.substring(0, sb.length() - 1); // "---" 不通过
return sb.length() > 0 ? sb.reverse().substring(0, sb.length()-1) : "";
}
482. License Key Formatting - LeetCode的更多相关文章
- 【leetcode】482. License Key Formatting
problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...
- [LeetCode] 482. License Key Formatting 注册码格式化
You are given a license key represented as a string S which consists only alphanumeric character and ...
- 【LeetCode】482. License Key Formatting 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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] License Key Formatting 注册码格式化
Now you are given a string S, which represents a software license key which we would like to format. ...
- [Swift]LeetCode482. 密钥格式化 | 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的许可证密钥,该字符串仅包含字 ...
随机推荐
- iOS全埋点解决方案-界面预览事件
前言 我们先了解 UIViewController 生命周期相关的内容和 iOS 的"黑魔法" Method Swizzling.然后再了解页面浏览事件($AppViewScr ...
- vue双向绑定、Proxy、defineproperty
本文原链接:https://www.jianshu.com/p/2df6dcddb0d7 前言 双向绑定其实已经是一个老掉牙的问题了,只要涉及到MVVM框架就不得不谈的知识点,但它毕竟是Vue的三要素 ...
- H5扇形
使用H5 canvas绘制的可交互扇形 requestAnimationFrame() 现有动画实现方式的不足 setTimeout和setInterval都不十分精确.为它们传入的第二个参数,实际上 ...
- h4ck47h0n ? h4ck3r ? I"m just a coder......
[0x00]刚开了专栏,没事过来投投稿混眼熟啦~//才不是为了抱枕呢刚刚结束了黑客马拉松,回来也是缓了好久才缓过来.这么高强度的脑洞劳动···阿不,是脑力劳动的说.真的第一次.说起来,跟代码相识,还是 ...
- CCF201312-2ISBN号码
问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如"x-xxx-xxxxx-x",其中符号"-&qu ...
- 简单的axios请求返回数据解构赋值
本地 data.json 文件 { "name": "大熊", "age": 18, "fnc": [ 1, 2, 3 ...
- MySQL 社区版
MySQL 社区版 MySQL 社区版是全球广受欢迎的开源数据库的免费下载版本.它遵循 GPL 许可协议,由庞大.活跃的开源开发人员社区提供支持. MySQL 社区版包括: 可插拔的存储引擎架构 多种 ...
- 几种比较经典的波形及其FFT变换(正弦波,三角波,方波和锯齿波)
之前上学时我的信号学得最差了,主要原因还是我高数学得不怎么样.可能是人总敬畏自己最不会的,所以我觉得我学过诸多科目中,数学是最博大精深而最妙的,从最开始的一次函数到反比例函数,二次三次函数和双曲线,椭 ...
- Golang 源码解读 01、深入解析 strings.Builder、strings.Join
strings.Builder 源码解析. 存在意义. 实现原理. 常用方法. 写入方法. 扩容方法. String() 方法. 禁止复制. 线程不安全. io.Writer 接口. 代码. stri ...
- Blazor 使用拖放(drag and drop)上传文件
在很多上传文件的应用实例中, 都可以看到[拖放文件到此上传]这种骚功能 ,今天我们就来试试Blazor能不能完成这个想法. 简述HTML5拖放 拖放是HTML5标准的一部分,任何元素都能够拖放,也能够 ...