Java实现 LeetCode 482 密钥格式化
482. 密钥格式化
给定一个密钥字符串S,只包含字母,数字以及 ‘-’(破折号)。N 个 ‘-’ 将字符串分成了 N+1 组。给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字符,第一个分组至少要包含 1 个字符。两个分组之间用 ‘-’(破折号)隔开,并且将所有的小写字母转换为大写字母。
给定非空字符串 S 和数字 K,按照上面描述的规则进行格式化。
示例 1:
输入:S = “5F3Z-2e-9-w”, K = 4
输出:“5F3Z-2E9W”
解释:字符串 S 被分成了两个部分,每部分 4 个字符;
注意,两个额外的破折号需要删掉。
示例 2:
输入:S = “2-5g-3-J”, K = 2
输出:“2-5G-3J”
解释:字符串 S 被分成了 3 个部分,按照前面的规则描述,第一部分的字符可以少于给定的数量,其余部分皆为 2 个字符。
提示:
S 的长度不超过 12,000,K 为正整数
S 只包含字母数字(a-z,A-Z,0-9)以及破折号’-’
S 非空
class Solution {
public String licenseKeyFormatting(String S, int K) {
char [] inp=S.toCharArray(); //i1
int L=inp.length, LL=2*L;
char [] cal=new char [LL]; //i2
int i1=L-1,i2=2*L-1,counter=K;
char c;
while( i1>=0 )
{
c=inp[i1--];
if( c!='-' )
{
if(counter==0)
{
cal[i2--]='-';
counter=K;
}
if(c>='a')
c-=32;
cal[i2--]=c;
counter--;
}
}
return new String (Arrays.copyOfRange(cal,i2+1,2*L));
}
}
Java实现 LeetCode 482 密钥格式化的更多相关文章
- Leetcode 482.密钥格式化
密钥格式化 给定一个密钥字符串S,只包含字母,数字以及 '-'(破折号).N 个 '-' 将字符串分成了 N+1 组.给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字符 ...
- 力扣(LeetCode)482. 密钥格式化
给定一个密钥字符串S,只包含字母,数字以及 '-'(破折号).N 个 '-' 将字符串分成了 N+1 组.给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字##符,第一个 ...
- JAVA,NET RSA密钥格式转换
JAVA和NET RSA密钥格式相互转换(公钥,私钥) 做了一个小项目遇到java和.net非对称加密问题,java的公钥和私钥就直接是一个字符串的形式展示的,但是.net是以xml简单包裹形式展示的 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- quartus ii FFT核使用
quartus ii FFT核使用 导入自己程序自带的txt文件,写出控制模块 时序图 FFT核文件给出的时序图输入 仿真时序图 1024个采样点数,输入结束 fft数据输出 2.代码 `timesc ...
- 设计模式之GOF23中介者模式
中介者模式Mediator 场景:公司中各个部门需要交互,通过中介总经理进行交互 核心: 如果一个系统中对象之间的联系成网状结构,对象之间多对多,将导致关系极其复杂,这些对象统称为“同事关系” 我们可 ...
- linux-Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64 [Couldn't open file /e tc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64]
在安装Python3-pip 的时候遇到 [root@localhost rpm-gpg]# yum install python3-pipFedora 31 - x86_64 - Updates - ...
- 关于layui数据表格的各种事件
table.on('tool(demo)', function(obj){}):监听工具条事件,tool 是工具条事件名,demo 是 table 原始容器的属性 lay-filter="对 ...
- vue实例中created、mounted以及其他类型说明
生命周期图示(图片来自coderwhy老师): 每个 Vue 实例在被创建之前都要经过一系列的初始化过程.例如,实例需要配置数据观测(data observer).编译模版.挂载实例到 DOM ,然后 ...
- 力扣题解-面试题58 - II. 左旋转字符串
题目描述 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能. 比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转 ...
- python之Python VS Code下载和安装教程
Visual Studio Code,简称 VS Code,是由微软公司开发的 IDE 工具.与微软其他 IDE(如 Visual Studio)不同的是,Visual Studio Code 是跨平 ...
- webpack指南(五)TypeScript
将webpack与TS进行集成. 1. 安装TypeScript 编译器和 loader npm install --save-dev typescript ts-loader 2. 在package ...
- 使用CountDownLatch等待多线程
前言 CountDownLatch 允许一个或多个线程等待其他线程完成操作. 应用场景 假如有一个列表的大量数据等待处理,最后全部处理完毕后返回处理结果.普通做法就是从头遍历,一个个顺序执行,这 ...
- iscroll在谷歌浏览器中bug
https://segmentfault.com/q/1010000008489619 iscroll 在安卓app嵌套html页面时,导致列表页滑动不起来,并且在chorme浏览器中使用手机模式,也 ...