# 482. License Key Formatting - LeetCode 原

yysue

## Solution

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) : "";
}
``````

### yysue

winrar 去除广告

geek_loser
2016/01/27
184
1
PostgreSQL中时间转换

The PostgreSQL formatting functionsprovide a powerful set of tools for converting various data types(date/time, integer, floating point, numeric) to formatted strings andfor con......

sucre
2016/02/18
285
0

2017/10/08
931
2

linzechi
2017/11/19
0
0
10款优秀的Rich Text Editor

Introduction Javascript rich text editor has ease our life when we need to edit articles, post or even documents online. Most of the editors allow user to edit the content strai......

Alex_曰生
2015/04/11
0
0
Leetcode 1——Two Sum

Quincuntial
2017/03/15
0
0

2017/12/06
0
0
PHP 开发的 CMS 系统--Baun

Baun 是一个现代、轻量级和可扩展的 CMS 系统，使用 PHP 开发。其灵感来自作者之前的 Pico 系统。 主要特性： 简单 Simple - Baun makes creating and maintaining a website as simple as ...

2015/03/06
2.5K
0
748. Shortest Completing Word。

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate......

Leafage_M
02/23
0
0
NoSQL比较：Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase vs Membase vs Neo4j

2011/08/29
14K
8

Git 基础 - 远程仓库的使用

6分钟前
0
0

suyain
8分钟前
0
0

1、pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.7</version></dependency><dependency> <groupId>log4j</......

18分钟前
0
0

talen
23分钟前
0
0
spring @Resource 和 @Autowired 的使用区别

28分钟前
0
0
springboot集成elasticsearch客户端问题记录

1背景说明 服务端ES版本为5.5.2，springboot版本为1.5.6。 工程中添加如下依赖 2问题记录 2.1 NetworkPlugin类找不到 报错java.lang.ClassNotFoundException: org.elasticsearch.plugins.Net...

zjg23
30分钟前
1
0

30分钟前
0
0
Java构造器使用注意

public class 父类A {int age = 10;protected void say() {System.out.println("父类A");}public 父类A() {override();}public void override() {Syst...

31分钟前
0
0
TensorFlow 线性分类

34分钟前
0
0

35分钟前
0
0