Question

482. License Key Formatting

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的更多相关文章

  1. 【leetcode】482. License Key Formatting

    problem 482. License Key Formatting solution1: 倒着处理,注意第一个字符为分隔符的情况要进行删除,注意字符的顺序是否正序. class Solution ...

  2. [LeetCode] 482. License Key Formatting 注册码格式化

    You are given a license key represented as a string S which consists only alphanumeric character and ...

  3. 【LeetCode】482. License Key Formatting 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 482 License Key Formatting 注册码格式化

    详见:https://leetcode.com/problems/license-key-formatting/description/ C++: class Solution { public: s ...

  5. 482. License Key Formatting

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. LeetCode_482. License Key Formatting

    482. License Key Formatting Easy You are given a license key represented as a string S which consist ...

  7. [LeetCode] License Key Formatting 注册码格式化

    Now you are given a string S, which represents a software license key which we would like to format. ...

  8. [Swift]LeetCode482. 密钥格式化 | License Key Formatting

    You are given a license key represented as a string S which consists only alphanumeric character and ...

  9. LeetCode算法题-License Key Formatting(Java实现)

    这是悦乐书的第241次更新,第254篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第108题(顺位题号是482).您将获得一个表示为字符串S的许可证密钥,该字符串仅包含字 ...

随机推荐

  1. Rust 中的数据布局--非正常大小的类型

    非正常大小的类型 大多数的时候,我们期望类型在编译时能够有一个静态已知的非零大小,但这并不总是 Rust 的常态. Dynamically Sized Types (DSTs) Rust 支持动态大小 ...

  2. H.265

    Baseline支持I/P 帧,只支持无交错(Progressive)和CAVLC一般用于低阶或需要额外容错的应用,比如视频通话.手机视频等: Main支持I/P/B 帧,无交错(Progressiv ...

  3. 4_ 比例控制器_燃烧卡路里(2)_Matlab/Simulink_Proportional Control

  4. C# Tutorial for Frontend Developer

    1.Basic Hello World Console output -> console.log Console.WriteLine("Hello World!"); Va ...

  5. 内网穿透系列-Go语言

    一.介绍 软件在KCP出现后进行了重构,将其底层UDP支持替换为了KCP,使其效率大大提高,在某些恶劣的网络环境下依旧能有不错的效果.当然,它也是支持TCP模式的,另外它也是支持加密的,在P2P打洞失 ...

  6. 解决SVG animation 在IE中不起作用

    问题描述 CSS animation没办法解决SVG路径运动的问题,下图路径运动的过程,通过查资料发现所有的IE的版本都不支持SVG animation.在IE中没有水流动的效果. 主要代码 < ...

  7. 【分享】WeX5的正确打开方式(6)——数据组件初探

    本文是[WeX5的正确打开方式]系列的第6篇文章,简单介绍一下WeX5中数据组件的特性和结构形式. 数据组件的由来 上一篇 WeX5绑定机制我们实现了一个简单的记账本应用,当时所有数据都用 JSON ...

  8. Brunch:入门上手

    在 Phoenix 项目中遇到关于 Branch 这个 HTML5 构建工具的问题, 在这里为了剥离问题的复杂度, 独立创建一个 Branch 前端项目来探索如何使用 Brunch 这个全新的前端构建 ...

  9. Python Turtle库绘制蟒蛇

    使用Python Turtle库来绘制蟒蛇 import turtle引入了海龟绘图体系 使用setup函数,设定了一个宽650像素和高350像素的窗体,其位置左上角坐标是200,200 说明位置在距 ...

  10. 小程序拿checkbox的checked属性

     方法一.checkbox <checkbox class="round red" bindtap="checkboxChange" checked=&q ...