LeetCode Top Interview Questions 412. Fizz Buzz (Java版; Easy)

welcome to my blog

LeetCode Top Interview Questions 412. Fizz Buzz (Java版; Easy)

题目描述

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

第一次做; 不需要取余操作, 核心: 理解a是b的因数这句话意味着什么

//a是b的因数, 说明b里面有x个a, 也就是让x个a相加便能得到b
class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> res = new ArrayList<>();
        if(n<1)
            return res;
        //
        int fizz=3, buzz=5;
        for(int i=1; i<=n; i++){
            if(i==fizz && i==buzz){
                res.add("FizzBuzz");
                fizz+=3;
                buzz+=5;
            }
            else if(i==fizz){
                res.add("Fizz");
                fizz+=3;
            }
            else if(i==buzz){
                res.add("Buzz");
                buzz+=5;
            }
            else{
                res.add(String.valueOf(i));
            }
        }
        return res;
    }
}

第一次做; 核心:判断a是不是b的因数; if else共4个分支, 先写约束条件最多的分支if(isFactor(i, 3) && isFactor(i, 5)), 再写约束条件次多的分支

//判断a是不是b的因数
class Solution {
    public List<String> fizzBuzz(int n) {
        List<String> res = new ArrayList<>();
        if(n<1)
            return res;
        //
        for(int i=1; i<=n; i++){
            if(isFactor(i, 3) && isFactor(i, 5))
                res.add("FizzBuzz");
            else if(isFactor(i, 3))
                res.add("Fizz");
            else if(isFactor(i, 5))
                res.add("Buzz");
            else
                res.add(String.valueOf(i));
        }
        return res;
        
    }
    private boolean isFactor(int a, int f){
        return a%f==0;
    }
}

LeetCode最优解 不用取余操作

    public List<String> fizzBuzz(int n) {
        
        List<String> result = new ArrayList<>();
        
        if(n < 1) return result;
        
        for(int i = 1, fizz = 3, buzz = 5; i <= n; i++) {
        
            String addVal = null;
            
            if(i == fizz && i == buzz) {
                addVal = "FizzBuzz"; 
                fizz += 3;
                buzz += 5;
            } else if(i == fizz) {
                addVal = "Fizz";
                fizz += 3;
            } else if(i == buzz) {
                addVal ="Buzz";
                buzz += 5;
            } else
                addVal = String.valueOf(i);
            
            result.add(addVal); 
        }
        
        return result;
    }

力扣官方题解 给出的将映射关系存在散列表里的思路也很好, 可以方便地处理各种映射关系; 遍历哈希表得到映射关系

放弃使用之前的联合判断,取而代之依次判断是否能被给定的数整数。这道题中,就是依次判断能不能被 3 整除,
能不能被 5 整除。如果能被 3 整除,就把对应的 Fizz 连接到答案字符串,如果能被 5 整除,就把 Buzz 连接
到答案字符串。
class Solution {
  public List<String> fizzBuzz(int n) {

    // ans list
    List<String> ans = new ArrayList<String>();

    // Hash map to store all fizzbuzz mappings.
    HashMap<Integer, String> fizzBizzDict =
        new HashMap<Integer, String>() {
          {
            put(3, "Fizz");
            put(5, "Buzz");
          }
        };

    for (int num = 1; num <= n; num++) {

      String numAnsStr = "";
      # 遍历哈希表得到映射关系
      for (Integer key : fizzBizzDict.keySet()) {

        // If the num is divisible by key,
        // then add the corresponding string mapping to current numAnsStr
        if (num % key == 0) {
          numAnsStr += fizzBizzDict.get(key);
        }
      }

      if (numAnsStr.equals("")) {
        // Not divisible by 3 or 5, add the number
        numAnsStr += Integer.toString(num);
      }

      // Append the current answer str to the ans list
      ans.add(numAnsStr);
    }

    return ans;
  }
}

你可能感兴趣的:(LeetCode,Top,Interview,Questions,leetcode,数据结构,java)