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 题目汇总

  • 相关阅读:
    微博CacheService架构浅析 对底层协议进行适配
    Lucene 查询原理 传统二级索引方案 倒排链合并 倒排索引 跳表 位图
    Linux kernel 同步机制
    对话 CTO〡用声音在一起,听荔枝 CTO 丁宁聊 UGC 声音互动平台的技术世界 原创 王颖奇 极客公园 2018-12-01
    当中台遇上DDD,我们该如何设计微服务?
    京东技术沙龙系列之二 | 深度解析京东微服务组件平台
    gRPC设计动机和原则
    微信全文搜索优化之路
    门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结 提高响应性能的手段归根结底就是三板斧:队列(Queue)、缓存(Cache)和分区(Sharding)
    现加减乘除4则运算
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9633383.html
Copyright © 2011-2022 走看看