日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/2/28 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leet Code OJ 482. License Key Formatting [Difficulty: Medium] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

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.
  • 翻譯

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

    分析

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

    Java版代碼(時間復雜度O(n),空間復雜度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);} }

    總結

    以上是生活随笔為你收集整理的Leet Code OJ 482. License Key Formatting [Difficulty: Medium]的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。