Check each number in the list, and add the related string into a new list.
def fizzBuzz(self, n): """ :type n: int :rtype: List[str] """ ls= for i in range(1,n+1): if i%3==0 and i%5==0:ls.append("FizzBuzz") elif i%3==0:ls.append("Fizz") elif i%5==0:ls.append("Buzz") else:ls.append(str(i)) return ls
I have a very similar answer to you, but mine is using 3 if statements instead of a large if else if else statement.
when I run mine with n = 5 I only get ['1'] back and nothing else.
But if I run the code alone without the def and solution lines in my own editor I get the correct answer
Do you know if the two lines
makes any difference in how the code runs?
@taylorychin Thank you for your question. I don't know the detailed differences. But the OJ sometimes reports something wrong with the code which runs well on local editor. Therefore, it's recommended to write and test the code just on this website editor. You can also see the output of the test data in this way. Meanwhile, I think perhaps it's necessary to use 'elif' in the codes like mine. By using 'elif' , I can take all the possibility into consideration in one time and will not omit or overlap some conditions :)