国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Leet Code OJ 482. License Key Formatting [Difficulty: Medium]

2019-11-14 12:02:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

題目

Now you are given a string S, which rePResents a software license key which we would like to format. The string S is composed of alphanumerical characters and dashes. The dashes split the alphanumerical characters within the string into groups. (i.e. if there are M dashes, the string is split into M+1 groups). The dashes in the given string are possibly misplaced.

We want each group of characters to be of length K (except for possibly the first group, which could be shorter, but still must contain at least one character). To satisfy this requirement, we will reinsert dashes. Additionally, all the lower case letters in the string must be converted to upper case.

So, you are given a non-empty string S, representing a license key to format, and an integer K. And you need to return the license key formatted according to the description above.

Example 1:

Input: S = "2-4A0r7-4k", K = 4Output: "24A0-R74K"Explanation: The string S has been split into two parts, each part has 4 characters.

Example 2:

Input: S = "2-4A0r7-4k", K = 3Output: "24-A0R-74K"Explanation: The string S has been split into three parts, each part has 3 characters except the first part as it could be shorter as said above.

Note:

The length of string S will not exceed 12,000, and K is a positive integer.String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).String S is non-empty.

翻譯

給定一個(gè)非空字符串S,代表一個(gè)軟件授權(quán)秘鑰,我們需要格式化這個(gè)秘鑰。這個(gè)字符串由字母、數(shù)字(a-z and/or A-Z and/or 0-9)和中劃線(-)組成。中劃線把字符串S分為幾個(gè)組(例如有M個(gè)中劃線,那會(huì)被分為M+1個(gè)組)?,F(xiàn)在,中劃線放置的位置可能被放錯(cuò)了。 我們希望每個(gè)組的字符長(zhǎng)度為整數(shù)K(除了第一個(gè)組,允許少于K,但是至少要有一個(gè)字符)。為了滿足這個(gè)需求,我們需要重新放置中劃線。另外,還需要把所有小寫字母轉(zhuǎn)換為大寫字母。 提示: 1. S的長(zhǎng)度不會(huì)超過(guò)12000,K是一個(gè)正整數(shù)。 2. S只包含字母、數(shù)字和中劃線。 3. S非空。

分析

由于給定的字符串S還無(wú)法判斷直接有效字符(除中劃線外)的個(gè)數(shù),故先做一次遍歷。筆者在這次遍歷中,將所有有效字符全部左移了,并轉(zhuǎn)化了大小寫。 得到有效字符的個(gè)數(shù)后,就可以算出最終字符的長(zhǎng)度newLen了,進(jìn)而創(chuàng)建這個(gè)新字符數(shù)組。遍歷這個(gè)新數(shù)組進(jìn)行填充即可。

java版代碼(時(shí)間復(fù)雜度O(n),空間復(fù)雜度O(n)):

public class Solution { public String licenseKeyFormatting(String S, int K) { int len = 0; int up = 'a' - 'A'; char[] charArr = S.toCharArray(); for (int i = 0; i <= charArr.length - 1; i++) { char ch = charArr[i]; if (ch == '-') { continue; } if (ch >= 'a' && ch <= 'z') { charArr[i] -= up; } charArr[len] = charArr[i]; len++; } if(len==0) return ""; int newLen = len % K == 0 ? len + len / K - 1 : len + len / K; char[] newArr = new char[newLen]; int j = len; for (int i = newLen - 1; i >= 0; i--) { if (i != newLen - 1 && (newLen-i) % (K+1) == 0) { newArr[i] = '-'; } else { newArr[i] = charArr[--j]; } } return String.valueOf(newArr); }}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 信丰县| 金溪县| 元氏县| 军事| 襄汾县| 若羌县| 汝城县| 汤阴县| 陆丰市| 河北省| 凌海市| 行唐县| 满城县| 丘北县| 贞丰县| 郎溪县| 新邵县| 泌阳县| 渑池县| 略阳县| 温泉县| 平阳县| 克拉玛依市| 从江县| 海丰县| 阜南县| 涟水县| 麦盖提县| 新巴尔虎右旗| 上犹县| 酒泉市| 驻马店市| 莱芜市| 木兰县| 浦县| 威信县| 临猗县| 红桥区| 天镇县| 康定县| 花垣县|