Last active
October 8, 2017 18:29
-
-
Save cixuuz/aab5b4d43e0ea1ce0bf76bb668238868 to your computer and use it in GitHub Desktop.
[412. Fizz Buzz] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
// O(n) O(n) | |
def fizzBuzz(self, n): | |
""" | |
:type n: int | |
:rtype: List[str] | |
""" | |
res = [] | |
for i in range(1, n+1): | |
three = i % 3 == 0 | |
five = i % 5 == 0 | |
if three and five: | |
res.append("FizzBuzz") | |
elif three: | |
res.append("Fizz") | |
elif five: | |
res.append("Buzz") | |
else: | |
res.append(str(i)) | |
return res | |
class Solution(object): | |
def fizzBuzz(self, n): | |
""" | |
:type n: int | |
:rtype: List[str] | |
""" | |
return ["Fizz"*(i%3==0) + "Buzz"*(i%5==0) or str(i) for i in range(1, n+1)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment