当前位置:网站首页>LeetCode簡單題之計算字符串的數字和
LeetCode簡單題之計算字符串的數字和
2022-04-23 08:14:00 【·星辰大海】
題目
給你一個由若幹數字(0 - 9)組成的字符串 s ,和一個整數。
如果 s 的長度大於 k ,則可以執行一輪操作。在一輪操作中,需要完成以下工作:
將 s 拆分 成長度為 k 的若幹 連續數字組 ,使得前 k 個字符都分在第一組,接下來的 k 個字符都分在第二組,依此類推。注意,最後一個數字組的長度可以小於 k 。
用錶示每個數字組中所有數字之和的字符串來 替換 對應的數字組。例如,“346” 會替換為 “13” ,因為 3 + 4 + 6 = 13 。
合並 所有組以形成一個新字符串。如果新字符串的長度大於 k 則重複第一步。
返回在完成所有輪操作後的 s 。
示例 1:
輸入:s = “11111222223”, k = 3
輸出:“135”
解釋:
- 第一輪,將 s 分成:“111”、“112”、“222” 和 “23” 。
接著,計算每一組的數字和:1 + 1 + 1 = 3、1 + 1 + 2 = 4、2 + 2 + 2 = 6 和 2 + 3 = 5 。
這樣,s 在第一輪之後變成 “3” + “4” + “6” + “5” = “3465” 。 - 第二輪,將 s 分成:“346” 和 “5” 。
接著,計算每一組的數字和:3 + 4 + 6 = 13 、5 = 5 。
這樣,s 在第二輪之後變成 “13” + “5” = “135” 。
現在,s.length <= k ,所以返回 “135” 作為答案。
示例 2:
輸入:s = “00000000”, k = 3
輸出:“000”
解釋:
將 “000”, “000”, and “00”.
接著,計算每一組的數字和:0 + 0 + 0 = 0 、0 + 0 + 0 = 0 和 0 + 0 = 0 。
s 變為 “0” + “0” + “0” = “000” ,其長度等於 k ,所以返回 “000” 。
提示:
1 <= s.length <= 100
2 <= k <= 100
s 僅由數字(0 - 9)組成。
來源:力扣(LeetCode)
解題思路
這樣的題目一看就非常能想到遞歸來試下。一眼就能看出可以遞歸的題目往往寫法上比較簡單,只需要符合第一個結果就能馬上寫出code。在每一層遞歸裏做一個簡單的事,就是將整個字符串先三個三個按照要求拼成一個新的字符串,至於新的字符串長度是什麼情况無需關注,下一層自會解决。
class Solution:
def digitSum(self, s: str, k: int) -> str:
if len(s)<=k: #遞歸出口
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: #假如s的長度不是k的整數倍,處理尾巴
S+=str(sum([int(j) for j in temp]))
s=S
return self.digitSum(s,k)
版权声明
本文为[·星辰大海]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230701590490.html
边栏推荐
- 网赚APP资源下载类网站源码
- 华硕笔记本电脑重装系统后不能读取usb,不能上网
- 浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
- Discussion on ES6 tail tune optimization
- Solidity IDE Remix中文版使用手册
- idea:使用easyYapi插件导出yapi接口
- Ignis公链的NFT生态发展:Unicorn.art的捐赠开发之路
- 扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
- 【无标题】
- Smart business card applet business card details page function implementation key code
猜你喜欢
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
1216_MISRA_C规范学习笔记_控制流的规则要求
Go语学习笔记 - Slice、Map | 从零开始Go语言
【无标题】
Positioning of high precision welding manipulator
Principle of sentinel integrating Nacos to update data dynamically
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
AAAI 2022招募讲者啦!!
dried food! Point based: differentiable Poisson solver
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
随机推荐
LeetCode 1611. 使整数变为 0 的最少操作次数
3C裝配中的機械臂運動規劃
[untitled]
利用Js实现一个千分位
Thinkphp6 + JWT realizes login verification
[go] common concurrency model [generic version]
Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
Cloud computing skills competition -- the first part of openstack private cloud environment
Implementation of promise all
Alibaba sentinel learning QA
渗透测试面试合集---HVV---
The following program deletes n consecutive words starting from the ith character from the string str
几种智能机器人室内定位方法对比
GUI,CLI与Unix哲学
sql 使用过的查询语句
Face to face summary 2
Guoji Beisheng openstack container cloud environment construction
An article understands variable lifting
Kubernetes in browser and IDE | interactive learning platform killercoda