当前位置:网站首页>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
边栏推荐
- Personal learning related
- Analysis of redo log generated by select command
- GDB的使用
- Explanation of input components in Chapter 16
- Lenovo Saver y9000x 2020
- Apache seatunnel 2.1.0 deployment and stepping on the pit
- Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
- Android clear app cache
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
猜你喜欢

Oracle job scheduled task usage details

AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)

Why do you need to learn container technology to engage in cloud native development
![Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]](/img/ec/b1e99e0f6e7d1ef1ce70eb92ba52c6.png)
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]

联想拯救者Y9000X 2020
![[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing](/img/12/a330b5e77921bbfa8e96fcbc660daa.png)
[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing

Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling

OSS cloud storage management practice (polite experience)

校园外卖系统 - 「农职邦」微信原生云开发小程序
![MySQL index [data structure + index creation principle]](/img/11/6bdc8a62e977ffb67be07ded0c8978.png)
MySQL index [data structure + index creation principle]
随机推荐
Detailed explanation of ADB shell top command
GDB的使用
Analysis of cluster component gpnp failed to start successfully in RAC environment
Opening: identification of double pointer instrument panel
MySQL and PgSQL time related operations
./gradlew: Permission denied
kettle庖丁解牛第16篇之输入组件周边讲解
ARGB transparency conversion
Analysis of unused index columns caused by implicit conversion of timestamp
Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
Window function row commonly used for fusion and de duplication_ number
Antd design form verification
Plato farm, a top-level metauniverse game, has made frequent positive moves recently
innobackupex增量备份
Detailed explanation of constraints of Oracle table
Special window function rank, deny_ rank, row_ number
SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
顶级元宇宙游戏Plato Farm,近期动作不断利好频频
[point cloud series] full revolutionary geometric features
sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)