当前位置:网站首页>Leetcode | 38 appearance array
Leetcode | 38 appearance array
2022-04-23 13:45:00 【Du Xiaorui】
I've worked on a problem for a long time. It turns out to be a simple problem ..
Title Description

Address : Appearance array
The meaning of the topic is easy to understand , It's a recursive process , from ‘1’ Start , Make a description of each input string in several formats , Then take this description as the next input , Until the second n The result of item .
Solution 1
This problem is an obvious recursive problem , But because I'm not good at recursion , So consider how to do it directly without recursion . So let's briefly say what I think . My approach is to write a function that divides and describes the input string first , Then call this function repeatedly . So the focus of this method is how to write the function of string division and description .
class Solution:
def countAndSay(self, n: int) -> str:
if n ==1:
return '1'
res = self.countandsay('1')
for i in range(2,n):
t = self.countandsay(res)
res = t
return res
def countandsay(self, s: str) -> str:
res = []
count = 1
pres = s[0]
i, n = 1, len(s)
if n == 1:
res.append('1')
res.append(str(s[0]))
return ''.join(res)
while i < n:
if pres == s[i]:
count = count + 1
i = i+1
else:
res.append(str(count))
res.append(str(pres))
pres = s[i]
count = 0
if count > 0:
res.append(str(count))
res.append(str(s[n-1]))
return "".join(res)
Find the description of the string and directly traverse the string , Compare whether each character is the same as the previous character and count , Then save the characters traversed each time and the number of occurrences in the array , Finally, convert the array into a string and return .
recursive + Double pointer
Methods from the comments area , I feel that this is the thing to be tested in this question .
class Solution:
def countAndSay(self, n: int) -> str:
if n == 1:
return '1'
s = self.countAndSay(n - 1)
ans = ''
start, end = 0, 0
while end < len(s):
while end < len(s) and s[start] == s[end]:
end += 1
ans += str(end - start) + s[start]
start = end
return ans
author : Like Ye Zi's Wang
link :https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnpvdm/?discussion=sKH5WT
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Recursion still needs to be learned
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556585053.html
边栏推荐
- Zero copy technology
- Es introduction learning notes
- Apache seatunnel 2.1.0 deployment and stepping on the pit
- Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
- Lenovo Savior y9000x 2020
- Interface idempotency problem
- OSS cloud storage management practice (polite experience)
- 顶级元宇宙游戏Plato Farm,近期动作不断利好频频
- Launcher hides app icons that do not need to be displayed
- Opening: identification of double pointer instrument panel
猜你喜欢

OSS cloud storage management practice (polite experience)

Zero copy technology

The interviewer dug a hole for me: what's the use of "/ /" in URI?
![[point cloud series] so net: self organizing network for point cloud analysis](/img/3c/6136b7aa322c42089f40ce13a2d747.png)
[point cloud series] so net: self organizing network for point cloud analysis

Cross carbon market and Web3 to achieve renewable transformation

Unified task distribution scheduling execution framework

联想拯救者Y9000X 2020

面试官给我挖坑:URI中的 “//” 有什么用?

Lenovo Saver y9000x 2020

kettle庖丁解牛第16篇之输入组件周边讲解
随机推荐
10g database cannot be started when using large memory host
Handling of high usage of Oracle undo
MySQL [acid + isolation level + redo log + undo log]
Oracle creates tablespaces and modifies user default tablespaces
OSS cloud storage management practice (polite experience)
kettle庖丁解牛第16篇之输入组件周边讲解
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
Storage scheme of video viewing records of users in station B
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
Oracle job scheduled task usage details
Solve tp6 download error course not find package topthink / think with stability stable
At the same time, the problems of height collapse and outer margin overlap are solved
Detailed explanation of constraints of Oracle table
Lenovo Savior y9000x 2020
Resolution: argument 'radius' is required to be an integer
19c environment ora-01035 login error handling
Error 403 in most cases, you or one of your dependencies are requesting
pycharm Install packages failed
Common commands of ADB shell
Oracle modify default temporary tablespace