LeetCode - 412. Fizz Buzz - ( c++ ) - 解题报告

1.题目大意

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

给定一个数n,要求写出代表1到n的字符串,其中能被3整除的输出 “Fizz”,能被5整除的输出 “Buzz”,同时被3和5整除的输出 “FizzBuzz”。

2.思路

思路非常简单。是个人应该都能做出来。但有几点可以学习一下,一个是C++中vector的应用(可以参考这篇文章),另一个是C++中int转string的方法。

下面是int转string的几种思路:

(1)利用stringstream:

使用stringstream的时候要注意加#include"sstream"。比如说我要把int类型的23转为string类型,那么我可以这样实现:

    int a=23;
    stringstream ss;
    ss<<a;
    string s1 = ss.str();

(2)利用sprintf int->char[]

(3)利用itoa int->char[]

(4)利用to_string (详见本题AC代码)

3.代码

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> s;
        for(int i=1;i<=n;i++)
           {
               if(i%15==0)
                s.push_back("FizzBuzz");
               else if(i%3==0)
                s.push_back("Fizz");
               else if(i%5==0)
                s.push_back("Buzz");
               else
                s.push_back(to_string(i));
           }
        return s;
    }
};

  

时间: 2024-10-12 15:32:13

LeetCode - 412. Fizz Buzz - ( c++ ) - 解题报告的相关文章

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: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the

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

【LeetCode】Word Break II 解题报告

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, given s = "catsanddog", dict = ["cat", "cats&quo

letecode [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