4/20 Fizz Buzz(412)
犯了三个错误
- 用字符串向量储存整数,当把整数赋给字符串时,它会把整数转换成对应的ASCII码,所以要在赋值前先将整数转换为字符串
answer[i] = i; //改为 answer[i] = to_string(i);
- 在删除向量元素时
answer = answer.erase(answer.begin()); //这里是把删除掉的元素的指针赋给了answer //想得到删除元素后的向量,应该 answer.erase(answer.begin());
- 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)相关推荐
- 【快乐水题】412. Fizz Buzz
原题: 力扣链接:412. Fizz Buzz 题目简述: 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中 ...
- LeetCode 412:Fizz Buzz(3/5倍数)
Together for a Shared future 一起向未来 今天我们看一道题目<412.FuzzBuzz>. 题目描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fiz ...
- 【leetcode】412.Fizz Buzz (三种方法开阔思路,java实现)
412. Fizz Buzz 难度简单 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz ...
- leetcode 412. Fizz Buzz
Write a program that outputs the string representation of numbers from 1 to n. But for multiples of ...
- LeetCode(412)——Fizz Buzz(JavaScript)
写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz": 如果 n 同时是3和5的 ...
- 【跟Leon一起刷LeetCode】412. Fizz Buzz
Fizz Buzz Description: Write a program that outputs the string representation of numbers from 1 to n ...
- 412. Fizz Buzz(java)
写一个程序,输出从 1 到 n 数字的字符串表示.1. 如果 n 是3的倍数,输出"Fizz":2. 如果 n 是5的倍数,输出"Buzz":3.如果 n 同时 ...
- LeetCode412_412. Fizz Buzz
LeetCode412_412. Fizz Buzz 一.描述 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果, ...
- iOS LeetCode ☞ Fizz Buzz
给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == "FizzBuzz& ...
最新文章
- 【 FPGA 】FIR 滤波器之滤波器的系数数据(Filter Coefficient Data)
- 关于面向对象的的设计原则的使用
- Spark Stream整合flum和kafka,数据存储在HBASE上,分析后存入数据库
- 怎么快速看完700页的论文呢?
- 使用BIOS进行键盘输入和磁盘读写---汇编学习笔记
- 三连冠!百度PARL 拿下NeurIPS 2020电网调度竞赛双赛道冠军
- 将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus Storefront
- 使用pkg打包编译nodejs程序,手动设置缓存内容
- 根据×××号码识别性别年龄生日的JS代码
- Linux下安装Mysql5.7
- 电商购物核心架构演进:谁说架构思路会过时?
- [51nod]1284 2 3 5 7的倍数
- 考软考需要准备什么,需要买哪些学习资料?
- 13个开源GIS软件
- 网络安全学习中的工具
- MySQL 安装 + 入门大全 + 常用命令合集
- input file选择图片后显示(FileReader)
- LeetCode - 807. 保持城市天际线 - Java
- C语言基础——C语言概述——语句和每行代码的含义
- 【Python】数理统计理论:常用统计量、经验分布函数