首页 > 其他分享 >leetcode 412. Fizz Buzz

leetcode 412. Fizz Buzz

时间:2023-05-30 18:05:34浏览次数:46  
标签:FizzBuzz Buzz 412 ans i% leetcode multiples Fizz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]
class Solution(object):
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        #return ["FizzBuzz"*(i%15==0) or "Buzz"*(i%5==0) or "Fizz"*(i%3==0) or str(i) for i in range(1, n+1)]
        m = p = 0       
        ans = []
        for i in range(1, n+1):
            m += 1
            p += 1
            s = str(i)
            if m == 3 and p == 5:
                s = "FizzBuzz"
                m = p = 0
            elif m == 3:
                s = "Fizz"
                m = 0
            elif p == 5:
                s = "Buzz"
                p = 0
            ans.append(s)            
        return ans

分别是用求余版本和不用求余版本。

记住 if else完全可以用and or 替代!


标签:FizzBuzz,Buzz,412,ans,i%,leetcode,multiples,Fizz
From: https://blog.51cto.com/u_11908275/6381141

相关文章

  • leetcode 669. Trim a Binary Search Tree
    GivenabinarysearchtreeandthelowestandhighestboundariesasLandR,trimthetreesothatallitselementsliesin[L,R](R>=L).Youmightneedtochangetherootofthetree,sotheresultshouldreturnthenewrootofthetrimmedbinarys......
  • leetcode 500. Keyboard Row
    GivenaListofwords,returnthewordsthatcanbetypedusinglettersofalphabetononlyonerow'sofAmericankeyboardliketheimagebelow.  Example1:Input:["Hello","Alaska","Dad","Peace"]Output:[&q......
  • leetcode 344. Reverse String
    Writeafunctionthattakesastringasinputandreturnsthestringreversed.Example:Givens="hello",return"olleh".classSolution(object):defreverseString(self,s):""":types:str......
  • leetcode 557. Reverse Words in a String III
    Givenastring,youneedtoreversetheorderofcharactersineachwordwithinasentencewhilestillpreservingwhitespaceandinitialwordorder.Example1:Input:"Let'stakeLeetCodecontest"Output:"s'teLekatedoCteeLtsetno......
  • leetcode 476. Number Complement
    Givenapositiveinteger,outputitscomplementnumber.Thecomplementstrategyistoflipthebitsofitsbinaryrepresentation.Note:Thegivenintegerisguaranteedtofitwithintherangeofa32-bitsignedinteger.Youcouldassumenoleadingzerobiti......
  • leetcode 561. Array Partition I
    Givenanarrayof2nintegers,yourtaskistogrouptheseintegersintonpairsofinteger,say(a1,b1),(a2,b2),...,(an,bn)whichmakessumofmin(ai,bi)forallifrom1tonaslargeaspossible.Example1:Input:[1,4,3,2]Output:4Explanation:......
  • leetcode 728. Self Dividing Numbers
    Aself-dividingnumberisanumberthatisdivisiblebyeverydigititcontains.Forexample,128isaself-dividingnumberbecause128%1==0,128%2==0,and128%8==0.Also,aself-dividingnumberisnotallowedtocontainthedigitzero.Givenal......
  • leetcode 657. Judge Route Circle
    Initially,thereisaRobotatposition(0,0).Givenasequenceofitsmoves,judgeifthisrobotmakesacircle,whichmeansitmovesbackto theoriginalplace.Themovesequenceisrepresentedbyastring.Andeachmoveisrepresentbyacharacter.The......
  • leetcode 461. Hamming Distance
    The Hammingdistance betweentwointegersisthenumberofpositionsatwhichthecorrespondingbitsaredifferent.Giventwointegers x and y,calculatetheHammingdistance.Note:0≤ x, y <231.Example:Input:x=1,y=4Output:2Explanation:1......
  • leetcode 771. Jewels and Stones
    You'regivenstrings J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwanttoknowhowmanyofthestonesyouhavearealsojewels.Thelettersin J areg......