zoukankan      html  css  js  c++  java
  • LeetCode_412. Fizz Buzz

    412. Fizz Buzz

    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"
    ]
    package leetcode.easy;
    
    public class FizzBuzz {
    	public java.util.List<String> fizzBuzz1(int n) {
    
    		// ans list
    		java.util.List<String> ans = new java.util.ArrayList<String>();
    
    		for (int num = 1; num <= n; num++) {
    
    			boolean divisibleBy3 = (num % 3 == 0);
    			boolean divisibleBy5 = (num % 5 == 0);
    
    			if (divisibleBy3 && divisibleBy5) {
    				// Divides by both 3 and 5, add FizzBuzz
    				ans.add("FizzBuzz");
    			} else if (divisibleBy3) {
    				// Divides by 3, add Fizz
    				ans.add("Fizz");
    			} else if (divisibleBy5) {
    				// Divides by 5, add Buzz
    				ans.add("Buzz");
    			} else {
    				// Not divisible by 3 or 5, add the number
    				ans.add(Integer.toString(num));
    			}
    		}
    
    		return ans;
    	}
    
    	public java.util.List<String> fizzBuzz2(int n) {
    		// ans list
    		java.util.List<String> ans = new java.util.ArrayList<String>();
    
    		for (int num = 1; num <= n; num++) {
    
    			boolean divisibleBy3 = (num % 3 == 0);
    			boolean divisibleBy5 = (num % 5 == 0);
    
    			String numAnsStr = "";
    
    			if (divisibleBy3) {
    				// Divides by 3, add Fizz
    				numAnsStr += "Fizz";
    			}
    
    			if (divisibleBy5) {
    				// Divides by 5, add Buzz
    				numAnsStr += "Buzz";
    			}
    
    			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;
    	}
    
    	public java.util.List<String> fizzBuzz3(int n) {
    
    		// ans list
    		java.util.List<String> ans = new java.util.ArrayList<String>();
    
    		// Hash map to store all fizzbuzz mappings.
    		java.util.HashMap<Integer, String> fizzBizzDict = new java.util.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;
    	}
    
    	@org.junit.Test
    	public void test() {
    		System.out.println(fizzBuzz1(15));
    		System.out.println(fizzBuzz2(15));
    		System.out.println(fizzBuzz3(15));
    	}
    }
    
  • 相关阅读:
    优化tableView加载cell与model的过程
    java.net.UnknownHostException: master
    Give root password for maintenance(or type control -D to continue)
    软件自动化部署脚本
    关于yum网络版仓库(本地yum仓库的安装配置,如果没网了,做一个局域网内的yum仓库)
    一脸懵逼学习keepalived(对Nginx进行热备)
    一脸懵逼学习Nginx及其安装,Tomcat的安装
    一脸懵逼学习Linux的Shell编程
    一脸懵逼学习KafKa集群的安装搭建--(一种高吞吐量的分布式发布订阅消息系统)
    一脸懵逼学习Storm的搭建--(一个开源的分布式实时计算系统)
  • 原文地址:https://www.cnblogs.com/denggelin/p/11929071.html
Copyright © 2011-2022 走看看