五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 2243. Calculate Digit Sum of a String

2023-05-26 13:02 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a string?s?consisting of digits and an integer?k.

A?round?can be completed if the length of?s?is greater than?k. In one round, do the following:

  1. Divide?s?into?consecutive groups?of size?k?such that the first?k?characters are in the first group, the next?k?characters are in the second group, and so on.?Note?that the size of the last group can be smaller than?k.

  2. Replace?each group of?s?with a string representing the sum of all its digits. For example,?"346"?is replaced with?"13"?because?3 + 4 + 6 = 13.

  3. Merge?consecutive groups together to form a new string. If the length of the string is greater than?k, repeat from step?1.

Return?s?after all rounds have been completed.

?

Example 1:

Input: s = "11111222223", k = 3

Output: "135"

Explanation:?

- For the first round, we divide s into groups of size 3: "111", "112", "222", and "23". ?

Then we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5. ?

?So, s becomes "3" + "4" + "6" + "5" = "3465" after the first round.?

- For the second round, we divide s into "346" and "5". ??

Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. ??

So, s becomes "13" + "5" = "135" after second round. Now, s.length <= k, so we return "135" as the answer.

Example 2:

Input: s = "00000000", k = 3

Output: "000"

Explanation:?

We divide s into "000", "000", and "00".?

Then we calculate the digit sum of each group:?

0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. s becomes "0" + "0" + "0" = "000", whose length is equal to k, so we return "000".

?需要2個循環(huán),一個是每次字符串的長度>k,另一個是每次去用一個stringbuilder去存儲處理的到數(shù)據(jù);

下面是代碼:

Constraints:

  • 1 <= s.length <= 100

  • 2 <= k <= 100

  • s?consists of digits only.


Runtime:?1 ms, faster than?89.94%?of?Java?online submissions for?Calculate Digit Sum of a String.

Memory Usage:?40.7 MB, less than?73.27%?of?Java?online submissions for?Calculate Digit Sum of a String.


LeetCode 2243. Calculate Digit Sum of a String的評論 (共 條)

分享到微博請遵守國家法律
汝南县| 广州市| 日照市| 长葛市| 灵璧县| 泸水县| 天气| 横山县| 中西区| 肇源县| 佛坪县| 油尖旺区| 钦州市| 永安市| 合川市| 通江县| 高要市| 洪雅县| 建湖县| 乐山市| 上杭县| 孝昌县| 云梦县| 宜城市| 海南省| 桐城市| 曲麻莱县| 麻城市| 太谷县| 樟树市| 得荣县| 镇雄县| 高淳县| 遂溪县| 广宁县| 樟树市| 广丰县| 静海县| 内江市| 邯郸县| 岱山县|