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. 【C语言】预处理、宏定义、内联函数 _

    一.由源码到可执行程序的过程 1. 预处理: 源码经过预处理器的预处理变成预处理过的.i中间文件   1 gcc -E test.c -o test.i 2. 编译: 中间文件经过编译器编译形成.s的 ...

  2. 纯JS实现KeyboardNav(学习笔记)二

    纯JS实现KeyboardNav(学习笔记)二 这篇博客只是自己的学习笔记,供日后复习所用,没有经过精心排版,也没有按逻辑编写 这篇主要是添加css,优化js编写逻辑和代码排版 GitHub项目源码 ...

  3. ajax遍历list数据解决方法

    在使用ajax遍历后台传来的list的时,总是遍历不出来,明明在控制台可以打印出来,但就是遍历不出来 之后发现是忘了加一个 dataType: "json" 导致遍历不出来

  4. 自己给idea下载Scala插件

    场景:有时候在idea上直接下载的scala可能因为太新所以有bug,需要手动下载插件 经验:自己下载完之后发现比较老的版本idea根本不让你装,只能装一些跟idea上推荐的scala相近的版本,感觉 ...

  5. C++:Abstract class : invalid abstract return type for member function ‘virtual...’

    #include <iostream> #include <cmath> #include <sstream> using namespace std; class ...

  6. java中final变量的用法

    4.4 final变量    final变量的数值不能在初始化之后进行改变(你希望a=3,有很多用到a的场合, 你当然不能在程序中就用3来代替a). 比如: final int h = 0; 想像有一 ...

  7. java基础-多线程线程池

    线程池 * 程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互.而使用线程池可以很好的提高性能,尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池.线程池里的每一个线程代 ...

  8. 在node中通过cors跨域。

    cors : 全称 cross origin resource share  跨资源共享 在nodejs 中可以通过在服务器端设置代码如下实现cors跨域 res.setHeader('Access- ...

  9. mongodb安装错误以及原理

    安装mongodb,默认是安装到"C:\Program Files\MongoDB\"这里的,我在注册表里没有找到mongodb的信息,所以猜测它只是将其解压到那个位置而已,它只是 ...

  10. Spring核心 IoC和AOP原理

    1. 什么是Spring Spring是一个轻量的Java开源框架,它简化了应用开发,实现基于POJO的编程模型.它的两大核心是:IoC(控制反转),AOP(面向切面编程). 2. IoC控制反转 简 ...