当前位置:包虫病 >> 疾病治疗 >> ARTS

ARTS

Algorithm

本周选择的算法题是:DecodeString[1]。

规则

Givenanencodedstring,returnitsdecodedstring.

Theencodingruleis:k[encoded_string],wheretheencoded_stringinsidethesquarebracketsisbeingrepeatedexactlyktimes.Notethatkisguaranteedtobeapositiveinteger.

Youmayassumethattheinputstringisalwaysvalid;Noextrawhitespaces,squarebracketsarewell-formed,etc.

Furthermore,youmayassumethattheoriginaldatadoesnotcontainanydigitsandthatdigitsareonlyforthoserepeatnumbers,k.Forexample,therewontbeinputlike3aor2[4].

Example1:

Input:s="3[a]2[bc]"Output:"aaabcbc"

Example2:

Input:s="3[a2[c]]"Output:"accaccacc"

Example3:

Input:s="2[abc]3[cd]ef"Output:"abcabccdcdcdef"

Example4:

Input:s="abc3[cd]xyz"Output:"abccdcdcdxyz"

Constraints:

1=s.length=30sconsistsoflowercaseEnglishletters,digits,andsquarebrackets[].sisguaranteedtobeavalidinput.Alltheintegersinsareintherange[1,].Solution

classSolution:defdecodeString(self,s:str)-str:ans,stack,num="",[],0forcharins:ifchar.isdigit():num=num*10+int(char)elifchar=="[":stack.append((num,ans))num,ans=0,""elifchar=="]":last_num,last_chars=stack.pop()ans=last_chars+last_num*anselse:ans+=charreturnansReview

HowtobettermanagebusinesslogicinyourFlutterapps[2]

通篇在讲Monorepo的优势,和Flutter没太大关系,不如改名为:如何用Monorepo更好地管理业务逻辑。

AdvancedPython:9BestPracticestoApplyWhenYouDefineClasses[3]

这篇文章还不错,有作者独特的见解,比如作者在介绍__str__和__repr__之间的区别及使用场景时,并不像大多数文章那样,只是简单一句:__repr__是给开发者看的,__str__是给用户看的,实际上__repr__的字符串是可以通过eval重新构建对象的:

classStudent:def__init__(self,first_name,last_name):self.first_name=first_nameself.last_name=last_namedef__repr__(self):returnf"Student({self.first_name!r},{self.last_name!r})"def__str__(self):returnf"Student:{self.first_name}{self.last_name}"student=Student("David","Johnson")studentStudent(David,Johnson)print(student)Student:DavidJohnson

全文对得起BestPractices的说法。

Tip

通过jekyll-feed[4]插件为博客添加了订阅功能,[订阅地址]({{site.url}}/feed.xml"订阅地址")。

Share

Python程序执行的过程。

参考资料[1]

DecodeString:

转载请注明:http://www.sonphie.com/jbzl/14483.html

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 网站简介| 发布优势| 服务条款| 隐私保护| 广告合作| 网站地图| 版权申明

    当前时间: