Onee's Den
GithubTwitter
  • 简介
  • 项目
    • onee-framework
    • crypto-zombies
  • 博客
    • 以太坊区块数据详解
    • 区块链技术科普
    • Apollo 配置中心实战场景
      • 动态变更日志输出级别
    • JS 树形插件 jsTree 使用小记
    • MySQL 数据库的隔离级别
    • 优化 Lucene 搜索速度的一点建议
    • LeetCode 刷题之旅
      • 344. Reverse String
      • 412. Fizz Buzz
      • 461. Hamming Distance
      • 463. Island Perimeter
    • RabbitMQ 初次尝试
    • 记一次搭建 OpenVPN 过程
Powered by GitBook
On this page

Was this helpful?

  1. 博客
  2. LeetCode 刷题之旅

412. Fizz Buzz

题目大意:给定一个n,输出一个1到n的数组,但是遇到3的倍数,则用“Fizz”代替,遇到5的倍数,则用“Buzz”代替,同时是3和5的倍数,则用“FizzBuzz”代替,例如输入:15,返回数组:["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]

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

public List<String> fizzBuzz(int n) {
    List<String> tList = new ArrayList<>();
    for (Integer i = 1; i <= n; i++) {
        if (i % 15 == 0) {
            tList.add("FizzBuzz");
            continue;
        }
        if (i % 3 == 0) {
            tList.add("Fizz");
            continue;
        }
        if (i % 5 == 0) {
            tList.add("Buzz");
            continue;
        }
        tList.add(i.toString());
    }
    return tList;
}
Previous344. Reverse StringNext461. Hamming Distance

Last updated 3 years ago

Was this helpful?