[leetcode]412. Fizz Buzz

2023-05-16

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

Solution:

class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> list = new ArrayList<String>();
        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;
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[leetcode]412. Fizz Buzz 的相关文章

  • 背包问题详解

    1 0 1背包问题 问题描述 xff1a 有N件物品和一个容量为V的背包 第i件物品的重量是w i xff0c 价值是p i 求解将哪些物品装入背包可使这些物品的总重量不超过背包容量 xff0c 且价值总和最大 思路 xff1a 每种物品仅
  • 堆排序原理与实现

    堆排序实际上是利用堆的性质来进行排序的 xff0c 我们通常说的堆就是二叉堆 xff0c 二叉堆又称完全二叉树或者近似完全二叉树 堆排序是选择排序的一种 可以利用数组的特点快速定位指定索引的元素 数组可以根据索引直接获取元素 xff0c 时
  • Hashmap1.7和1.8区别+ConcurrentHashmap1.7和1.8区别

    Hashmap JDK1 7中 使用一个Entry数组来存储数据 xff0c 用key的hashcode取模来决定key会被放到数组里的位置 xff0c 如果hashcode相同 xff0c 或者hashcode取模后的结果相同 xff0c
  • TOP k问题

    题目 xff1a 有1千万条短信 xff0c 有重复 xff0c 以文本文件的形式保存 xff0c 一行一条 xff0c 有重复 请用5分钟时间 xff0c 找出重复出现最多的前10条 解析 xff1a 对于本题来说 xff0c 某些面试者

随机推荐

  • CAS操作是怎么实现的

    CAS是compare and swap xff0c 翻译过来就是比较并交换 维护三个变量值 xff0c 一个是内存值V xff0c 一个是期望的旧的值A xff0c 一个是要更新的值B 更新一个变量的时候 xff0c 只有当预期值A与内存
  • liunx在整合springboot 项目时出现错误CLUSTERDOWN The cluster is down

    在运行springboot项目并且把项目利用二级缓存 xff0c 储存到集群中时候在idea下报错显示CLUSTERDOWN The cluster is down 可以进入linux中查看集群配置 连接到某个节点此时显示此时节点明显显示的
  • volatile关键字

    java提供了一种稍弱的同步机制 xff0c volatile变量 xff0c 用来确保将变量的更新操作通知到其他线程 当把变量声明为volatile类型的时候 xff0c 编译器与运行时都会注意到这个变量是共享的 xff0c 所以不会将该
  • MVCC

    在并发读写数据库时 xff0c 读操作可能会不一致的数据 xff08 脏读 xff09 为了避免这种情况 xff0c 需要实现数据库的并发访问控制 xff0c 最简单的方式就是加锁访问 由于加锁会将读写操作串行化 xff0c 所以不会出现不
  • AQS理解

    AbstractQueuedSynchronizer简称AQS xff0c 是一个用于构建锁和同步容器的框架 事实上concurrent包内许多类都是基于AQS构建 xff0c 例如ReentrantLock Semaphere Count
  • B树、B+树及索引

    B树 xff1a 每个节点都存储key和data xff0c 所有节点组成这棵树 xff0c 并且叶子节点指针为null B 43 树 xff1a 只有叶子节点存储data xff0c 叶子节点包含了这棵树的所有键值 xff0c 叶子节点不
  • Arrays.sort和Collections.sort实现原理解析

    Collections sort方法底层就是调用的Arrays sort方法 写一个例子看源码 xff1a public static void main String args List lt String gt strings 61 A
  • Java代理模式之动态代理

    代理模式是设计模式中非常重要的一种类型 代理模式从类型上来说 xff0c 可以分为静态代理和动态代理两种类型 假设一个场景 xff0c 有一个蛋糕店 xff0c 卖的蛋糕都是用蛋糕机做的 xff0c 而且不同种类的蛋糕由不同的蛋糕机来做 x
  • 二叉树镜像

    求二叉树镜像 public class Solution public void Mirror TreeNode root if root 61 61 null return if root left 61 61 null amp amp
  • 设计模式之适配器模式

    适配器模式是作为两个不兼容的接口之间的桥梁 这种类型的设计模式属于结构型模式 xff0c 它结合了两个独立接口的功能 这种模式涉及到一个单一的类 xff0c 该类负责加入独立的或不兼容的接口功能 举个真实的例子 xff0c 读卡器是作为内存
  • 设计模式之单例模式

    1 懒汉式 xff0c 线程不安全 public class Demo1 private static Demo1 instance private Demo1 public static Demo1 getInstance if inst
  • Lambda表达式详解

    Java 8最值得学习的特性就是Lambda表达式 Lambda写的好可以极大减少代码冗余 xff0c 同时可读性也好过冗长的内部类 xff0c 匿名类 举例说明一下 xff1a xff08 1 xff09 创建线程传统写法 xff1a T
  • Android8.0以上实现APP(应用)开机自启动

    一 程序中实现APP开机自启动 可参考 xff1a https www cnblogs com jetereting p 4572302 html 二 设置APP开机自启动权限 小米手机设置开机启动应用权限 xff08 Android9 0
  • 跳台阶问题

    1 输出斐波那契数列的第n项 直接上代码 xff1a public class Fibonacci public static int fibonacci int n if n 61 61 0 return 0 if n 61 61 1 r
  • 字符串编辑距离

    字符串的编辑距离 xff0c 又称为Levenshtein距离 是利用字符操作 xff0c 把字符串A转换成字符串B所需要的最少操作数 其中 xff0c 字符操作包括 xff1a 删除一个字符插入一个字符修改一个字符 例如对于 34 hel
  • [leetcode]807.Max Increase to Keep City Skyline

    In a 2 dimensional array grid each value grid i j represents the height of a building located there We are allowed to in
  • [leetcode]1038. Binary Search Tree to Greater Sum Tree

    Given the root of a binary search tree with distinct values modify it so that every node has a new value equal to the su
  • [leetcode]344. Reverse String

    Write a function that reverses a string The input string is given as an array of characters char Do not allocate extra s
  • [leetcode]136. Single Number

    Given a non empty array of integers every element appears twice except for one Find that single one Note Your algorithm
  • [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 outpu