leetcode--笔记8 Fizz Buzz

题目要求:

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”.

my answer:

测试错误分析

1 range(n)的使用,记住range(2)代表0,1 是从0开始的,所以上述遍历之后的 i 要变为 i+1

2 s%3==0 , 对于小于3的数也是成立的,所以这里分开讨论了

3 首先if 同时整除3和5的数,否则会出错

4 如果 output.append(“u”),直接加双引号会出错,输出"u" ,而不输出遍历的数字,所以需要用函数 str( )

时间: 2024-10-13 17:47:39

leetcode--笔记8 Fizz Buzz的相关文章

LeetCode之412. Fizz Buzz

-------------------------------------------- 虽然是从最简单的开始刷起,但木有想到LeetCode上也有这么水的题目啊... AC代码: public class Solution { public List<String> fizzBuzz(int n) { List<String> res=new ArrayList<>(); for(int i=1;i<=n;i++){ if(i/3*3==i &&

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 output "Fizz" instead of the number and for the multiples of five output "Buzz". For numbers which are multiples of bot

[LeetCode] Fizz Buzz 嘶嘶嗡嗡

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 fiv

Leetcode: Fizz Buzz

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 bot

LeetCode - Fizz Buzz Multithreaded

Write a program that outputs the string representation of numbers from 1 to n, however: If the number is divisible by 3, output "fizz". If the number is divisible by 5, output "buzz". If the number is divisible by both 3 and 5, output

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

412. Fizz Buzz

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 fiv

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

LintCode (9)Fizz Buzz

下面是AC代码,C++风格: 1 class Solution { 2 public: 3 vector<string> fizzBuzz(int N) { 4 vector<string> Answer; 5 for(int i = 1;i <= N;i++) { 6 if(i % 15 == 0) { 7 Answer.push_back("fizz buzz"); 8 } else if(i % 3 == 0) { 9 Answer.push_bac

[leetcode笔记] Remove Duplicates from Sorted List II

问题描述: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2-&