当前位置:网站首页>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
边栏推荐
- PHP high precision computing
- Why are there 1px problems? How?
- 将实例化对象的方法 给新的对象用
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
- vslam PPT
- Comparison of indoor positioning methods of several intelligent robots
- Cloud computing skills competition -- the first part of openstack private cloud environment
- Feign source code analysis
- An article understands variable lifting
- 以下程序实现从字符串str中删除第i个字符开始的连续n个字
猜你喜欢

数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲

Cloud computing skills competition -- the first part of openstack private cloud environment

LeetCode简单题之统计字符串中的元音子字符串

vivo,硬件安全的爱与雷霆

惨了,搞坏了领导的机密文件,吐血分享备份文件的代码技巧

Go语学习笔记 - 结构体 | 从零开始Go语言

Go语学习笔记 - 数组 | 从零开始Go语言

Thinkphp6 + JWT realizes login verification

Go语学习笔记 - Slice、Map | 从零开始Go语言
![[极客大挑战 2019]Havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)
[极客大挑战 2019]Havefun1
随机推荐
分布式服务治理Nacos
Solidity IDE Remix中文版使用手册
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
Ribbon start process
3C装配中的机械臂运动规划
AAAI 2022 recruit speakers!!
渗透测试面试合集---HVV---
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
Compiling principle questions - with answers
求3个字符串(每串不超过20个字符)中的最大者。
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
将实例化对象的方法 给新的对象用
【Appium】测试时遇到手机内嵌H5页面的切换问题
雲計算技能大賽 -- openstack私有雲環境 第一部分
NLLLoss+log_SoftMax=CE_Loss
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
Usage of databinding
3C裝配中的機械臂運動規劃
校园转转二手市场源码下载