当前位置:网站首页>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
边栏推荐
- Alibaba sentinel learning QA
- LeetCode简单题之计算字符串的数字和
- Flatten arrays
- sql 使用过的查询语句
- 1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
- Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
- Move layout (Flex layout, viewport label)
- LeetCode15. 三数之和
- 在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)
- Compiler des questions de principe - avec des réponses
猜你喜欢
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
vivo,硬件安全的爱与雷霆
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
Go语学习笔记 - 语言接口 | 从零开始Go语言
Go语学习笔记 - 结构体 | 从零开始Go语言
[极客大挑战 2019]Havefun1
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
搜一下导航完整程序源码
Guoji Beisheng openstack container cloud environment construction
NLLLoss+log_SoftMax=CE_Loss
随机推荐
三星,再次“西征”
简述CPU
Mobile terminal layout (3D conversion, animation)
单点登录 SSO
几种智能机器人室内定位方法对比
渗透测试面试合集---HVV---
Draw a circle quickly in MATLAB (the one that can be drawn directly given the coordinates and radius of the center of the circle)
使用 Ingress 实现金丝雀发布
thinkphp6+jwt 实现登录验证
php生成短链接:将数字转成字母,将字母转成数字
Guoji Beisheng openstack container cloud environment construction
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
3C装配中的机械臂运动规划
一个没啥L用,但可以装X的IDEA插件
PHP high precision computing
Codeforces Round #784 (Div. 4)
C language learning record -- use and analysis of string function (2)
AAAI 2022 recruit speakers!!
利用Js实现一个千分位
Principle of sentinel integrating Nacos to update data dynamically