LeetCode 412. Fizz Buzz

leetcode:412.Fizz Buzz 题目 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/fizz-buzz 给你一个整数 n ,找出从 1 到 n 各个整数的 Fizz Buzz 表示,并用字符串数组 answer(下标从 1 开始)返回结果,其中: answer[i] == “FizzBuzz” 如果 i 同时是 3 和 5 的倍数。 answer[i] == “Fizz” 如果 i 是 3 的倍数。 answer[i] == “Buzz” 如果 i 是 5 的倍数。 answer 阅读详情

Description

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

Code

class Solution {
public:
    vector<string> fizzBuzz(int n) {
        vector<string> result(n,"");
        for (int i=0; i<n;i++){
            if ((i+1) % 15 == 0) {
                result[i] ="FizzBuzz"; 
                continue;
            }
            if ((i+1) % 3 == 0) {
                result[i] ="Fizz"; 
                continue;
            }
            if ((i+1) % 5 == 0) {
                result[i] ="Buzz"; 
                continue;
            }            
            result[i] = to_string(i+1);
        }
        return result;
    }
};

Appendix

Leetcode 412. Fizz Buzz-蜂鸣器 Leetcode 412. Fizz Buzz-蜂鸣器;Given an integern, returna string arrayanswer(1-indexed) where: answer[i] == "FizzBuzz"ifiis divisible by3and5. answer[i] == "Fizz"ifiis divisible by3. answer[i] == "Buzz"ifiis divisible by5. answer[i] == i(a 阅读详情

相关推荐

_leetcode-python.pdf

LeetCode的解题思路 包含问题 1-200. Python version... pdf格式

Fizz Buzz(C语言)

Fizz Buzz(C语言)

elven_prophecy的博客 342

C++11 for Programmers, 2nd Edition

In C++11 for Programmers , the Deitels bring their proven Live Code approach to teaching today’s powerful new version of the C++ language. Like all Deitel Developer titles, they teach the best way possible: via hundreds of complete example C++ programs, with thousands of lines of downloadable C++ source code.

Leecode刷题 412. Fizz Buzz——二级指针、字符串数组、malloc

Leecode刷题 412. Fizz Buzz——二级指针、字符串数组、malloc

electron的博客 599

FizzBuzz编程

Fizz Buzz挺有趣的题目,我现在是按照这个题目简单的实现了编码测试.

leetcode全套解答python版本

leetcode全套解答python版本。包括更新到10月份的的leetcode

buzz fizz 翻译_【Oxford-2】The Fizz-Buzz

原标题:【Oxford-2】The Fizz-Buzz Vocabularyfizz-buzz烟花zip-zap烟花let 让,使go off 爆炸buzz off 文中指烟花没了puff 喷出,张开zip 撕裂声zap 嘶嘶声fizz 嘶嘶声buzz 嗡嗡声puff 膨胀声原文1、The Fizz-Buzz Dad had a Zip-zap.爸爸有一个Zip-zap(烟花)。(Let it o...

weixin_31570865的博客 1万+

fizzBuzzJava

fizzBu​​zzJava 打印数字1-30。对于3个打印Fizz的每一个倍数,5个打印Buzz的每个倍数,以及15个FizzBu​​zz的每个倍数。

习题:Fizz Buzz(C语言) 《初级算法》<数学>部分开始

题目 写一个程序,输出从 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) 链接:http

Mu_Muxi_的博客 994

第四章 汇川技术InoProShop入门之串口自由协议解读

摘要: 串口自由协议是工业自动化中针对特定需求设计的自定义通信协议,具有高度灵活性和针对性,适用于小型设备、实时控制等场景。其核心包括自定义帧结构(起始符、地址码、数据区、校验位等)和通信规则(波特率、交互机制)。相比标准协议,自由协议效率更高但兼容性弱,设计时需注重可靠性、效率和可扩展性,适用于定制化或高实时性需求,如传感器数据采集或老旧设备改造。典型应用需平衡精简帧结构与抗干扰能力,同时预留扩展空间。

zyfucking2010的博客 2234

正点原子ATK-LORA-01无线串口代码移植+STM32F103C8T6(标准库)

目录 一、ATK-LORA-01无线串口资料 二、代码移植 1.分析 (1)usart3(也可以是其他的串口)用于接收数据的串口 usart.h (2)time3(也可以是其他的定时器)用于时序的设置 (3)lora.c 三、演示 1.主机(发送方) (1)主机接线 (2)主机代码(发送方) (3)主机实物 插上无线串口通电即可发送​ 2.从机(接收方) (1)从机接线(LORA与主机相同) (2)从机代码(接收方) (3)从机实物 接上TTL和LORA并通电即...

m0_56942354的博客 8983

FuzzBuzz (C语言)

写一个程序输出字符串代表数1到n,如果数能被3整除,输出"Fizz",能被5整除"Buzz",既能被3也能被5整除输出"FizzBuzz"。 比如: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"

MaxCliff的博客 1363

CST 仿真_NFC仿真

在CST中实现NFC天线单体的仿真

qq_43706810的博客 3979

leetcode412. Fizz Buzz,简单遍历

leetcode412. Fizz Buzz,简单遍历

热爱C++和golang的学生 1372

Edge浏览器IE模式失效?最新142版本隐藏按钮的3种解决方案

本文针对Edge浏览器142版本中IE模式按钮消失的问题,提供了3种实用解决方案,包括注册表修改、手动添加网站到IE模式列表和企业策略部署方案,帮助用户解决兼容性问题,确保老旧系统正常访问。

weixin_28308325的博客 526

【论文笔记】Reaching agreement in the presence of faults (EIG)

1 问题定义 2 交互一致性 3 单点故障 m=1 n=4 4 多点故障(n&amp;gt;=3m+1) 计算过程 m=2 n=7 5 n&amp;lt;3m+1不能达成协定 6 带认证的算法 7 总结 这篇论文在1980年发表,是1982年著名的拜占庭将军问题论文的前身。作者Leslie Lamport是2013图灵奖得主,两篇论文引用次数分别为5000+和2000+。该论文提出的算法现在被...

zjwreal 3002

LeetCode 412 - Fizz Buzz

这道题其实是很多人入门算法时的经典“Hello World”级别的逻辑题——Fizz Buzz。它看起来很简单,但却经常被面试官用来考察你对逻辑判断、代码结构清晰度以及边界条件处理的习惯。 在这篇文章里,我们会从实际开发的思维出发,聊聊怎么用 Swift 实现一个干净、清晰、可扩展的 Fizz Buzz,同时结合一些真实的使用场景,比如日志输出、系统监控状态显示等,帮你理解这类“规则映射”的问题其实在工程里非常常见。

记录从移动端、鸿蒙到物联网与AI的完整实战与思考。专注于分享可复用的工程方案与产品思维,尤其深入鸿蒙生态与全栈开发。以十年工程与管理沉淀,为你提供清晰的技术纵深路径。坚信技术的价值在于“贯通”与“落地”。 649

RK356X linux系统移植RTL8723BU模块遇到的坑

RK356X linux系统移植RTL8723BU模块遇到的坑

目前从事linux驱动开发,多交流多学习吧 961

LeetCode //C - 412. Fizz Buzz

【代码】LeetCode //C - 412. Fizz Buzz。

Made in Code 967

AI智算-K8s如何利用GPFS分布式并行文件存储加速训练or推理

GPFS(General Parallel File System,通用并行文件系统)是由 IBM 开发的高性能分布式文件系统,最初设计于 1990 年代,用于满足高性能计算(HPC)环境对大规模数据存储和访问的需求。GPFS 现已广泛应用于企业级存储、云计算、大数据分析、人工智能和媒体处理等领域。它以其高性能、可扩展性和可靠性著称,能够支持从小型集群到超大规模系统的各种部署场景。

Kason_iWiki 1145

Python 爬虫实战:豆瓣 / IMDB 电影数据抓取(评分 + 剧情 + 演员阵容结构化处理)

在数据为王的时代,电影数据是进行市场分析、推荐系统构建和学术研究的重要资源。豆瓣和 IMDB 作为国内外最具影响力的电影评分和资讯网站,包含了海量高质量的电影数据。本文将带领你从零开始,构建一个健壮的 Python 爬虫,用于抓取豆瓣和 IMDB 的电影信息,并将其中的非结构化数据(如剧情简介、演员列表)进行清洗和结构化处理,最终存储到便于分析的结构化文件(如 CSV)中。

yansideyucsdn的博客 969

LeetCode 412 Fizz Buzz(JAVA)

class Solution { public List<String> fizzBuzz(int n) { List<String> res = new ArrayList<>(n); for (int i = 1; i <= n; i++) { // 首先判断能否被3/5整除 if (i % 3 == 0 && i % 5 == 0) { .

weixin_43170297的博客 376

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 412 Fizz Buzz Java实现

LeetCode412 题目 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”; 如果 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” ] 通过次数45,972提交

BedivereAlter的博客 399

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

面试必考的最简单逻辑题——力扣412.Fizz Buzz

题目要求对1到n的每个数字按特定规则转换为字符串:3的倍数输出"Fizz",5的倍数输出"Buzz",同时是3和5的倍数输出"FizzBuzz",否则输出数字本身。通过遍历1到n,使用条件判断依次处理这些情况即可。Java实现时需要注意判断顺序,先检查3和5的共同倍数。时间复杂度O(n),空间复杂度O(1)。该题考察基础循环和条件判断能力,通过字符串拼接可以优化代码结构。

w1311823723的博客 402

Leetcode 412 Fizz Buzz

Leetcode 412 Fizz Buzz#include <vector> #include <string> using namespace std;class Solution { public: vector<string> fizzBuzz(int n) { vector<string> fizzbuzz; for(int i = 1;i <= n;i +

自己的学习笔记 394

leetcode-412- Fizz Buzz

问题题目:[leetcode-412]思路枚举判断。代码/* 3 : Fizz 5:Buzz 3&&5FizzBuzz */class Solution { public: vector<string> fizzBuzz(int n) { std::vector<std::string> ret; for( int i = 1; i <= n; ++i )

Kang_TJU的博客 564
上一篇: LeetCode 448. Find All Numbers Disappeared in an Array
下一篇: LeetCode 495. Teemo Attacking
sqyx008
博客等级 码龄10年 31粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值