Algorithm description:

 

Give you an integer n. Print each number from 1 to n as follows:

  • If this number is divisible by 3, printfizz.
  • If this number is divisible by 5, printbuzz.
  • If this number can both be3and5Divisible, printfizz buzz.
  • If this number can neither be3It can’t be divisible5Divisible, print numbersItself.

The sample

For example, n = 15 returns an array of strings:

[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"
]
Copy the code

My code:

class Solution:
    """
    @param n: An integer
    @return: A list of strings.
    """
    def fizzBuzz(self, n):
        # write your code here
        str_list = []
        for i in range(1,n+1):
            if i%3 == 0:
        		if i%5 == 0:
        			str_list.append('fizz buzz')
        		else:
        		    str_list.append('fizz')
        	else:
        		if i%5 == 0:
        			str_list.append('buzz')
        		else:
        		    str_list.append(str(i))
        return str_list
Copy the code