当前位置:网站首页>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)

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590490.html
边栏推荐
- Go语学习笔记 - 语言接口 | 从零开始Go语言
- Fibula dynamic programming
- Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
- BUUCTF [ACTF2020 新生赛]Include1
- AAAI 2022 recruit speakers!!
- 华硕笔记本电脑重装系统后不能读取usb,不能上网
- 以下程序实现从字符串str中删除第i个字符开始的连续n个字
- js常用数组方法
- sql 使用过的查询语句
- NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
猜你喜欢
随机推荐
GUI,CLI与Unix哲学
数据库之Mysql——概述安装篇
校园转转二手市场源码下载
数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲
惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧
室内定位技术对比
Brief description of CPU
如何在SQL Server中导入excel数据,2019版
PHP high precision computing
LeetCode 1611. 使整数变为 0 的最少操作次数
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
渗透测试面试合集---HVV---
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
Codeforces Round #784 (Div. 4)
Samsung, March to the west again
怎么读书读论文
Convert object to URL
Alibaba sentinel learning QA
网赚APP资源下载类网站源码








![BUUCTF [极客大挑战 2019]EasySQL1](/img/ad/afca09bc1da003393319af700e90e3.png)
