美文网首页
412. Fizz Buzz

412. Fizz Buzz

作者: namelessEcho | 来源:发表于2017-09-09 16:53 被阅读0次

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”.

没啥好说的,一个替代关系。 仔细写case就好了

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

相关文章

  • LeetCode 411-430

    412. Fizz Buzz[https://leetcode-cn.com/problems/fizz-buzz...

  • Leetcode PHP题解--D40 412. Fizz Bu

    412. Fizz Buzz 题目链接 412. Fizz Buzz 题目分析 这个题目也很简单。 从1逐个输出到...

  • Leetcode 412 Fizz Buzz

    题目链接:412. Fizz Buzz 题目描述 Write a program that outputs the...

  • 412. Fizz Buzz

    一、题目原型: 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”;如果 ...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    题目分析 题目链接,登录 LeetCode 后可用思路比较简单,直接遍历1 到 n 的每个数,依次判断每种情况即可...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

  • 412. Fizz Buzz

网友评论

      本文标题:412. Fizz Buzz

      本文链接:https://www.haomeiwen.com/subject/atcajxtx.html