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的许可证密钥,该字符串仅包含字 ...
随机推荐
- JavaScript HTML5脚本编程——“历史状态管理”的注意要点
历史状态管理是现代Web应用开发中的一个难点.在现代Web应用中,用户的每次操作不一定会打开一个全新的页面,因此"后退"和"前进"按钮也就失去了作用,导致用户很 ...
- 让弹幕给 PPD 生个孩子
怎样才能跑起来我写的弹幕程序 资源下载 申请野狗后端云账号注册 创建应用: 复制appId到index.html的 var ref = new Wilddog("https://<ap ...
- java中为什么把Checked Exception翻译成受检的异常?
6.Checked Exception(受检的异常) 马克-to-win:为什么我大胆的把Checked Exception翻译成受检的异常?因为这类异常,编译器检查发现到它后会强令你catch它或t ...
- 1.Markdown语法
Markdown学习 一.标题:(# +标题名字) 标题 三级标题 四级标题 二.字体 (空格内容前后的空格删掉) Hello,World! **粗体** Hello,World! *斜体* Hell ...
- Blazor WebAssembly 渐进式 Web 应用程序 (PWA) 使用 LocalStorage 离线处理数据
原文链接:https://www.cnblogs.com/densen2014/p/16133343.html Window.localStorage 只读的localStorage 属性允许你访问一 ...
- Intellij IDEA 2022 正式发布,这些功能真不错
Intellij IDEA 2022 正式发布了,作为正版用户,胖哥赶紧更新了一波,好家伙!这几个功能确实很香啊.新版更新的东西真不少,不愧是一个大版本更新. 依赖分析 IDEA的依赖检查.依赖冲突解 ...
- 【大学物理实验】01 单摆测重力加速度 的 g 计算代码
单摆测重力加速度 传统摆 (代码没保存,就截了个图) 研究单摆周期与摆长之间的关系 (依旧是g的计算) 我还不太会数据拟合
- HCIE笔记-第五节-IP地址+VLSM
192.168.1.111 -- 点分十进制 -- IPV4地址表示格式 计算机 只能识别 01010101 二进制 4组十进制数 规则:二进制0/1 在不同位表达的含义是不一致的,0永远代表不取值, ...
- C# 中托管内存与非托管内存之间的转换
c#有自己的内存回收机制,所以在c#中我们可以只new,不用关心怎样delete,c#使用gc来清理内存,这部分内存就是managed memory,大部分时候我们工作于c#环境中,都是在使用托管内存 ...
- 通知:PostgreSQL证书申报退税请抓紧!
2021年个税申报已于3月开始为了确保PostgreSQL证书能顺利退税中心特调研了学员中仅针对证书一项做退税申请没几天就得到结果2021年取得的PostgreSQL职业 技术证书确定可以退税! ...