fizz

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

故事扮演 提交于 2020-02-02 09:39:04
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 >

USACO 2019 December Contest, Silver 题解

佐手、 提交于 2019-12-17 22:34:46
1.MooBuzz Description Farmer John 的奶牛们最近成为了一个简单的数字游戏“FizzBuzz”的狂热玩家。这个游戏的规则很简单:奶牛们站成一圈,依次从一开始报数,每头奶牛在轮到她的时候报一个数。如果一头奶牛将要报的数字是 3 的倍数,她应当报“Fizz”来代替这个数。如果一头奶牛将要报的数字是 5 的倍数,她应当报“Buzz”来代替这个数。如果一头奶牛将要报的数字是 15 的倍数,她应当报“FizzBuzz”来代替这个数。于是这个游戏的开始部分的记录为: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16 由于词汇的匮乏,奶牛们玩的 FizzBuzz 中用“Moo”代替了 Fizz、Buzz、FizzBuzz。于是奶牛版的游戏的开始部分的记录为: 1, 2, Moo, 4, Moo, Moo, 7, 8, Moo, Moo, 11, Moo, 13, 14, Moo, 16 给定 N(1≤N≤1e9),请求出这个游戏中第 N 个被报的数。 A possible solution 利用容斥原理+二分判断。我们假设 n 个数里共有 cnt 个被跳过的数,那么报的第 n 个数就是 n+cnt ,由于 n 和 cnt 都具有单调性,所以我们可以利用二分判定来解决

Leetcode 412: Fizz Buzz

橙三吉。 提交于 2019-12-11 12:45:53
题目描述: 写一个程序,输出从 1 到 n 数字的字符串表示。 1. 如果 n 是3的倍数,输出“Fizz”; 2. 如果 n 是5的倍数,输出“Buzz”; 3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。 示例: n = 15, 返回: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/fizz-buzz 这是一道基本的条件判断语句的题目,每个学习程序语言的入门时候都应该写过类似的题目。。。 /** * @param {number} n * @return {string[]} */ var fizzBuzz = function(n) { let res = []; for(let i = 1; i <= n; i++) { if(i % 3 === 0 && i % 5 === 0) { res.push("FizzBuzz"); } else if(i % 3 === 0) { res.push("Fizz"); } else if(i % 5 === 0) { res.push("Buzz")

LeetCode|412. Fizz Buzz

亡梦爱人 提交于 2019-12-06 08:54:48
题目描述 等级: 简单 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”; 如果 n 是5的倍数,输出“Buzz”; 如果 n 同时是3和5的倍数,输出 “FizzBuzz”。 示例: n = 15, 返回: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] 思路 对于字符串的考察,对于这道题直接用最简单的思路处理了,如果碰到复杂的问题,需要用Map存储n与Fizz或Buzz的映射,之后进行字符串拼接。 答案 class Solution { public List<String> fizzBuzz(int n) { List<String> res = new ArrayList<>(); for(int i=1;i<=n;i++){ String str = ""; if((i%3==0)&&(i%5==0)){ str= "FizzBuzz"; }else if(i%3==0){ str= "Fizz"; }else if(i%5==0){ str= "Buzz"; }else{ str = String.valueOf(i); } res.add(str); }

数到3时打印Fizz,5时打印Buzz Fizz Buzz

懵懂的女人 提交于 2019-12-06 00:45:47
问题: 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" ] 解决: ① 直接解决,3的倍数输出Fizz,5的倍数输出Buzz。 class Solution { //6ms public List<String> fizzBuzz(int n) { List<String> res = new ArrayList<>(); for (int i = 1;i <= n;i ++){ String tmp = ""; if (i % 3 !=

LeetCode_412. Fizz Buzz

我是研究僧i 提交于 2019-12-05 14:47:35
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

LeetCode|412. Fizz Buzz

喜夏-厌秋 提交于 2019-12-04 03:34:41
题目描述 等级: 简单 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”; 如果 n 是5的倍数,输出“Buzz”; 如果 n 同时是3和5的倍数,输出 “FizzBuzz”。 示例: n = 15, 返回: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] 思路 对于字符串的考察,对于这道题直接用最简单的思路处理了,如果碰到复杂的问题,需要用Map存储n与Fizz或Buzz的映射,之后进行字符串拼接。 答案 class Solution { public List<String> fizzBuzz(int n) { List<String> res = new ArrayList<>(); for(int i=1;i<=n;i++){ String str = ""; if((i%3==0)&&(i%5==0)){ str= "FizzBuzz"; }else if(i%3==0){ str= "Fizz"; }else if(i%5==0){ str= "Buzz"; }else{ str = String.valueOf(i); } res.add(str); }

How to auto populate line number in vba editor? [closed]

匿名 (未验证) 提交于 2019-12-03 02:43:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a macro that can auto populate the line number for a vba editor? I want to line number for debugging reasons as to know where the error occured. 回答1: You don't want line numbers. Not for "debugging reasons", not for anything. Line numbers are deprecated for a reason: they're a relic of an ancient time before procedures even existed, and GOTO the only way to get anywhere. Erl only returns the last encountered line number before an error was raised . This can mean misleading error logs, if you're logging errors: Sub DoSomething() 10

412. Fizz Buzz

巧了我就是萌 提交于 2019-11-29 10:07:11
412. Fizz Buzz 题目描述 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”; 如果 n 是5的倍数,输出“Buzz”; 如果 n 同时是3和5的倍数,输出 “FizzBuzz”。 示例: n = 15, 返回: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] 实现 class Solution { public List < String > fizzBuzz ( int n ) { List < String > res = new ArrayList < > ( ) ; if ( n <= 0 ) { return res ; } for ( int i = 1 ; i <= n ; i ++ ) { String sub = "" ; if ( i % 3 == 0 ) { sub = "Fizz" ; } if ( i % 5 == 0 ) { sub = sub + "Buzz" ; } if ( ! sub . equals ( "" ) ) { res . add ( sub ) ; continue ; } res . add ( sub + i )