Love丶FFC's Blog

LintCode_easy:Fizz Buzz问题

2019-08-29 09:15:51
阅读:1237   •   评论:9
标签:,

描述

给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:

规则1.如果这个数被3整除,打印fizz.

规则2.如果这个数被5整除,打印buzz.

规则3.如果这个数能同时被35整除,打印fizz buzz.

规则4.如果这个数既不能被 3 整除也不能被 5 整除,打印数字本身

样例

比如 n = 15, 返回一个字符串数组:

[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"
]

挑战(√)

你是否可以只用一个 if 来实现

编程语言:Java

解题思想:

1.共4种情况需要判断,为了达成挑战,需要考虑怎么使用唯一的一个if。

2.使用条件表达式a?b:c可以实现。

3.首先使用if判断该数能否被3整除,然后在if中使用条件表达式,判断该数是否能被5整除,如能5整除,则仅满足规则3,若不能被5整除,则仅满足规则1。然后使用else(该数不能被3整除),判断该数能否被5整除,如能被5整除,则仅满足规则2,如不能被5整除,则仅满足规则4

时间复杂度:O(n)

IDE代码如下:

  1. package LintCode_Easy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class _9 {
  7.  
  8. public static void main(String[] args) {
  9. int n = 15;
  10. List<String> s = new ArrayList<String>();
  11. for(int i=1;i<=n;i++)
  12. {
  13. if(i%3==0)
  14. {
  15. s.add((i%5==0)?"fizz buzz":"fizz");
  16. }
  17. else
  18. {
  19. s.add((i%5==0)?"buzz":i+"");
  20. }
  21. }
  22. System.out.println(s);
  23. }
  24.  
  25. }
  26. /* s.add((i%5==0)?"fizz buzz":"fizz")等同为
  27.   if(i%5==0)
  28.   {
  29.   s.add("fizz buzz");
  30.   }
  31.   else
  32.   {
  33.   s.add("fizz");
  34.   }
  35.  */

LintCode编辑器代码如下:

  1. public class Solution {
  2. public List<String> fizzBuzz(int n) {
  3. List<String> s = new ArrayList<String>();
  4. for(int i=1;i<=n;i++)
  5. {
  6. if(i%3==0)
  7. {
  8. s.add((i%5==0)?"fizz buzz":"fizz");
  9. }
  10. else
  11. {
  12. s.add((i%5==0)?"buzz":i+"");
  13. }
  14. }
  15. return s;
  16. }
  17. }

评论板

共有 9 条评论

  1. fruinly

    Metatarsal bones from both Ifngr2 О”EC and control embryos were isolated at gestational day 18 canadian pharmacy cialis 20mg We found no evidence for a step change in prescribing, despite the guideline causing a large and immediate increase in the population appropriate for tamoxifen

  2. engarma

    My oncologist wants me to take and I have picked up the meds but so I am so nervous to start because of info I was told and have read about side effects viagra online prescription Allgren RL, Marbury TC, Rahman SN et al

  3. Preliaf

    viagra levitra forum 05, but not the 0

  4. LOumpk

    I was so weak and in great pain order priligy online uk

  5. Ihwczw

    buy fenofibrate online buy tricor 200mg online cheap buy tricor 160mg

  6. Oyeucm

    buy zaditor 1 mg generic order tofranil 75mg online cheap tofranil 25mg pill

  7. Vebijy

    oral tadalafil 5mg buy tadalafil 10mg online sildenafil overnight shipping usa

  8. Aptaep

    acarbose 25mg tablet buy repaglinide 2mg online order fulvicin 250mg without prescription

  9. Khbykx

    buy minoxidil solution for sale buy tadalafil 5mg generic buy ed pills cheap

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:455757