当前位置:网站首页>The simple problem of leetcode is to calculate the numerical sum of strings

The simple problem of leetcode is to calculate the numerical sum of strings

2022-04-23 08:14:00 ·Starry Sea

subject

Give you a number of numbers (0 - 9) Composed string s , And an integer .
If s Is longer than k , You can perform a round of operations . In one round of operation , The following work needs to be done :
take s Split The growth rate is k A number of Continuous digital group , Make the front k All characters are in the first group , Next k All characters are grouped in the second group , And so on . Be careful , The length of the last number group can be less than k .
Use a string representing the sum of all numbers in each number group to Replace Corresponding number group . for example ,“346” Will be replaced by “13” , because 3 + 4 + 6 = 13 .
Merge All groups to form a new string . If the length of the new string is greater than k Then repeat the first step .
Returns... After all rounds have been completed s .
Example 1:
Input :s = “11111222223”, k = 3
Output :“135”
explain :

  • The first round , take s Divide into :“111”、“112”、“222” and “23” .
    next , Calculate the sum of each set of numbers :1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 and 2 + 3 = 5 .
    such ,s After the first round, it becomes “3” + “4” + “6” + “5” = “3465” .
  • The second round , take s Divide into :“346” and “5” .
    next , Calculate the sum of each set of numbers :3 + 4 + 6 = 13 、5 = 5 .
    such ,s After the second round, it becomes “13” + “5” = “135” .
    Now? ,s.length <= k , So back “135” As the answer .
    Example 2:
    Input :s = “00000000”, k = 3
    Output :“000”
    explain :
    take “000”, “000”, and “00”.
    next , Calculate the sum of each set of numbers :0 + 0 + 0 = 0 、0 + 0 + 0 = 0 and 0 + 0 = 0 .
    s Turn into “0” + “0” + “0” = “000” , Its length is equal to k , So back “000” .
    Tips :
    1 <= s.length <= 100
    2 <= k <= 100
    s Just numbers (0 - 9) form .
    source : Power button (LeetCode)

Their thinking

   I can think of such a topic at a glance and give it a try . It can be seen at a glance that recursive topics are often written in a relatively simple way , Just match the first result and you can write it right away code. Do a simple thing in each layer of recursion , That is to put the whole string three at first into a new string according to the requirements , As for the new string length, there is no need to pay attention to , The next level will solve .

class Solution:
    def digitSum(self, s: str, k: int) -> str:
        if len(s)<=k:  # Recursive export 
            return s
        S,temp,count='','',0
        for i in s:
            temp+=i
            count+=1
            if count%k==0:
                S+=str(sum([int(j) for j in temp]))
                temp=''
        if temp:  # If s The length of is not k Integer multiple , Handle tail 
            S+=str(sum([int(j) for j in temp]))
        s=S
        return self.digitSum(s,k)

 Insert picture description here

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590490.html