犯了三个错误

  1. 用字符串向量储存整数,当把整数赋给字符串时,它会把整数转换成对应的ASCII码,所以要在赋值前先将整数转换为字符串

    answer[i] = i;
    //改为
    answer[i] = to_string(i);
  2. 在删除向量元素时
    answer = answer.erase(answer.begin());
    //这里是把删除掉的元素的指针赋给了answer
    //想得到删除元素后的向量,应该
    answer.erase(answer.begin());
  3. if 使用不当
    if ();
    if ();
    if ();
    else ;
    //只执行最后一个if和else
    //正确用法:
    if ();
    else if ();
    else if ();
    else ;

    题解:

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

    tip:和之前计算数组的动态和一样,应该想到用push_back()来动态添加元素。这里字符串的相加很理想

    "Fizz" + "Buzz" = "FizzBuzz"

4/20 Fizz Buzz(412)相关推荐

  1. 【快乐水题】412. Fizz Buzz

    原题: 力扣链接:412. Fizz Buzz 题目简述: 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中 ...

  2. LeetCode 412:Fizz Buzz(3/5倍数)

    Together for a Shared future 一起向未来 今天我们看一道题目<412.FuzzBuzz>. 题目描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fiz ...

  3. 【leetcode】412.Fizz Buzz (三种方法开阔思路,java实现)

    412. Fizz Buzz 难度简单 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz ...

  4. leetcode 412. Fizz Buzz

    Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...

  5. LeetCode(412)——Fizz Buzz(JavaScript)

    写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 如果 n 同时是3和5的 ...

  6. 【跟Leon一起刷LeetCode】412. Fizz Buzz

    Fizz Buzz Description: Write a program that outputs the string representation of numbers from 1 to n ...

  7. 412. Fizz Buzz(java)

    写一个程序,输出从 1 到 n 数字的字符串表示.1. 如果 n 是3的倍数,输出"Fizz":2. 如果 n 是5的倍数,输出"Buzz":3.如果 n 同时 ...

  8. LeetCode412_412. Fizz Buzz

    LeetCode412_412. Fizz Buzz 一.描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果, ...

  9. iOS LeetCode ☞ Fizz Buzz

    给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == "FizzBuzz& ...

最新文章

  1. 【 FPGA 】FIR 滤波器之滤波器的系数数据(Filter Coefficient Data)
  2. 关于面向对象的的设计原则的使用
  3. Spark Stream整合flum和kafka,数据存储在HBASE上,分析后存入数据库
  4. 怎么快速看完700页的论文呢?
  5. 使用BIOS进行键盘输入和磁盘读写---汇编学习笔记
  6. 三连冠!百度PARL 拿下NeurIPS 2020电网调度竞赛双赛道冠军
  7. 将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus Storefront
  8. 使用pkg打包编译nodejs程序,手动设置缓存内容
  9. 根据×××号码识别性别年龄生日的JS代码
  10. Linux下安装Mysql5.7
  11. 电商购物核心架构演进:谁说架构思路会过时?
  12. [51nod]1284 2 3 5 7的倍数
  13. 考软考需要准备什么,需要买哪些学习资料?
  14. 13个开源GIS软件
  15. 网络安全学习中的工具
  16. MySQL 安装 + 入门大全 + 常用命令合集
  17. input file选择图片后显示(FileReader)
  18. LeetCode - 807. 保持城市天际线 - Java
  19. C语言基础——C语言概述——语句和每行代码的含义
  20. 【Python】数理统计理论:常用统计量、经验分布函数

热门文章

  1. Tomcat服务器安装、配置教程
  2. PHP面向对象(一)
  3. 内网环境下CentOs离线安装程序
  4. 【python】openpyxl常用方法
  5. PC端本地存储方案,Windows和Mac双端通用方案
  6. GB28181开发(二) pjsip库SDP协议扩展
  7. JAVA开发方向和就业方向
  8. win10任务栏图标显示异常(重启资源管理器无果)解决方法
  9. 利用c++与opencv将文本、图像加密进另一张图片中
  10. python刷今日头条访问量_Python 自动刷博客浏览量实例代码