zoukankan      html  css  js  c++  java
  • [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡

    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"
    ]

    很简单的一道题,最基本的思路就是对1~n的每一个数对3,5取模,根据情况写入结果。然后就是有一些极简的写法和思路,可以看看大牛们的写法,也挺受用的。

    Java:  Not use '%' operation

    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> ret = new ArrayList<String>(n);
            for(int i=1,fizz=0,buzz=0;i<=n ;i++){
                fizz++;
                buzz++;
                if(fizz==3 && buzz==5){
                    ret.add("FizzBuzz");
                    fizz=0;
                    buzz=0;
                }else if(fizz==3){
                    ret.add("Fizz");
                    fizz=0;
                }else if(buzz==5){
                    ret.add("Buzz");
                    buzz=0;
                }else{
                    ret.add(String.valueOf(i));
                }
            } 
            return ret;
        }
    }
    

    Java:

    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= n; i++) {
                if (i % 3 == 0 && i % 5 == 0) {
                    list.add("FizzBuzz");
                } else if (i % 3 == 0) {
                    list.add("Fizz");
                } else if (i % 5 == 0) {
                    list.add("Buzz");
                } else {
                    list.add(String.valueOf(i));
                }
            }
            return list;
        }
    }  

    Python:

    def fizzBuzz(self, n):
        return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]
    

    Python:  

    class Solution(object):
        def fizzBuzz(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            return [str(i) if (i%3!=0 and i%5!=0) else (('Fizz'*(i%3==0)) + ('Buzz'*(i%5==0))) for i in range(1,n+1)]   

    Python:

    def fizzBuzz(self, n):
            return ['FizzBuzz'[i % -3 & -4:i % -5 & 8 ^ 12] or repr(i) for i in range(1, n + 1)]
    

    Python:

    class Solution(object):
        def fizzBuzz(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            result = []
            for i in xrange(1, n+1):
                if i % 15 == 0:
                    result.append("FizzBuzz")
                elif i % 5 == 0:
                    result.append("Buzz")
                elif i % 3 == 0:
                    result.append("Fizz")
                else:
                    result.append(str(i))
    
            return result
    

    Python:  

    class Solution(object):
        def fizzBuzz(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            l = [str(x) for x in range(n + 1)]
            l3 = range(0, n + 1, 3)
            l5 = range(0, n + 1, 5)
            for i in l3:
                l[i] = 'Fizz'
            for i in l5:
                if l[i] == 'Fizz':
                    l[i] += 'Buzz'
                else:
                    l[i] = 'Buzz'
            return l[1:] 

    Python: wo

    class Solution(object):
        def fizzBuzz(self, n):
            """
            :type n: int
            :rtype: List[str]
            """
            res = []
            for i in xrange(1, n + 1):
                if i % 3 == 0 and i % 5 == 0:
                    res.append('FizzBuzz')
                elif i % 3 == 0:
                    res.append('Fizz')
                elif i % 5 == 0:
                    res.append('Buzz')
                else:
                    res.append(str(i))
                    
            return res  
    

    C++:

    class Solution {
    public:
        vector<string> fizzBuzz(int n) {
            vector<string> res;
            for (int i = 1; i <= n; ++i) {
                if (i % 15 == 0) res.push_back("FizzBuzz");
                else if (i % 3 == 0) res.push_back("Fizz");
                else if (i % 5 == 0) res.push_back("Buzz");
                else res.push_back(to_string(i));
            }
            return res;
        }
    };
    

      

      

    All LeetCode Questions List 题目汇总

  • 相关阅读:
    443. String Compression
    506. Relative Ranks
    825. Friends Of Appropriate Ages
    447. Number of Boomerangs
    54. Spiral Matrix
    744. Find Smallest Letter Greater Than Target
    HDU-1565 方格取数(1)
    zoj 3672 Gao The Sequence
    ZOJ 3675 Trim the Nails
    poj -2955 Brackets
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9633383.html
Copyright © 2011-2022 走看看