leetcode_13_Roman to Integer(C++)(easy)

13. Roman to Integer [easy] (Python) 题目链接https://leetcode.com/problems/roman-to-integer/题目原文 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题目翻译给定一个罗马数字,将其转成整数。输入限制在1到3999之 阅读详情

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

思路一:

首先观察罗马数字Ⅰ、Ⅱ、Ⅲ、Ⅳ、Ⅴ、Ⅵ、Ⅶ、Ⅷ、Ⅸ、Ⅹ、Ⅺ、Ⅻ(1-12)。对于Ⅳ,当1出现在5的左侧时是4,即做减法。对于Ⅵ,1出现在5右侧是6,做加法。那么对于一个给定的罗马数字,我们可以对其累加,当相邻的两个字符,前一个小于后一个时减去2倍的前一个数(因为本该减1的地方加1了,所以减2倍)。


class Solution {
public:
    int romanToInt(string s) {
        
        if(s == "\0")
            return false;
        int result = 0;
        for(int i = 0; i < s.size(); ++i)
        {
            result += toInt(s[i]);
        }
        
        
        
        for(int i = 1; i < s.size(); ++i)
        {
            int pre = toInt(s[i - 1]);
            int cur = toInt(s[i]);
            if(pre < cur)
            {
                result -= 2 * pre;
            }
            
        }
        return result;
    }
    
    int toInt(char c)
    {
        switch(c)
        {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
};

无需云服务器:飞牛云NAS+Docker搭建本地WordPress站点全球访问 在数字化浪潮中,网站搭建已成为个人和企业在推广自身品牌与业务的首要选择。飞牛云新推出的fnOS,这款集多功能于一身的nas系统,以其卓越的性能和灵活性,成为理想的本地网站部署平台。本文将带你一探如何在飞牛云NAS上通过Docker部署WordPress,并利用cpolar实现全球访问。 阅读详情

相关推荐

802.1AS时间同步机制深度解析:从Sync报文到多域冗余设计

本文深度解析了802.1AS时间同步机制,从Sync与Follow_Up报文的核心交互原理入手,详细阐述了其如何通过精确的时钟频率比测量、链路延迟计算和驻留时间补偿来实现微秒乃至纳秒级同步。文章进一步探讨了802.1AS-2020版本引入的多时钟域冗余设计,为构建高可靠的车载TSN网络提供了关键见解和实战配置指南。

pytorch8learner的博客 468

LeetCode#13 Roman to Integer罗马数字转整数

题目描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D ...

IT圈的01羊的博客 262

华强北手表256G真相大揭秘:ADB命令实测内存与存储(附完整调试教程)

本文通过ADB命令实战揭秘华强北智能手表256G存储等硬件参数的真实性。文章提供了从开启调试模式到使用ADB命令查询系统属性、验证内存与存储容量的完整教程,帮助用户识别参数虚标,评估设备真实性能与潜在风险。

e3f4g5的博客 1270

LeetCode 13.Roman to Integer(罗马数字转整数) 数学,字符串/easy

文章目录1.Description2.Example3.Solution优化: 1.Description 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12

押切徹的博客 340

Star CCM+中的图片自动保存设置

图片自动保存设置

2201_75375391的博客 3455

开启虚拟人物互动新时代:探索相芯Avatar SDK驱动功能(Android)

相芯SDK提供了男孩和女孩Avatar驱动模型,这些模型具备半身驱动和全身驱动的功能,使得虚拟角色能够根据用户的面部表情和动作实时模仿和响应。半身驱动功能: 相芯SDK的男孩和女孩Avatar驱动模型具备上身驱动功能,即能够通过用户的面部表情和动作来驱动虚拟角色的上半身行为。这意味着用户的面部表情、头部转动和上半身姿态等动作可以直接映射到虚拟角色上,使得虚拟角色能够栩栩如生地模仿用户的动作。例如,用户微笑时,虚拟角色会展现出笑容;用户转动头部时,虚拟角色也会相应地转动头部。

Faceunity2023的博客 637

力扣(LeetCode)13. 罗马数字转整数(C++)

【墨染】哈希表编码。模拟。

墨染 571

AI数字人:口播与唇形同步的福音,支持本地部署/批量生成/口齿清晰

你也可以直接输入语音,虚拟化身会根据语音的节奏和语调,同步做出相应的动作和表情,表现更加生动。不过这依然是个值得关注的工具。借助先进的 AI 算法,Heygem 高精度捕捉五官、轮廓等特征,构建逼真的虚拟模型。同时,它还能克隆语音,捕捉人声的细微特征,支持多种语音参数设置,打造高度相似的音色效果。打开设置,调整镜像存储位置,镜像文件比较大70~80G,选择合适的盘,设置完成后点击“Apply & Restart”。数字人视频画面与声音高度同步,口型匹配自然流畅,智能优化音视频效果,带来极佳的视听体验。

m0_72580657的博客 5871

LeetCode第13题:Roman to Integer(C++)详解

Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 1...

WangHuanToBeBest 775

带有HSE组件的S32系列芯片中各子系统如何依次启动?

仔细阐述了上电后各个子系统的启动顺序,并给出了详细的启动步骤流程图。

车载系统攻城狮 1708

搞懂 SAPUI5 Fiori 应用的 OData 授权维护:从 IWSG、IWSV 到 S_SERVICE 的完整实践

本文探讨了SAP Fiori项目中常见的授权问题及其解决方案。文章指出,开发人员常误将OData服务访问问题归咎于前端代码或配置,而实际上多因授权默认值未正确带入角色所致。详细分析了IWSG(前端服务组元数据)和IWSV(后端业务服务)两个关键对象的作用机制,强调应通过PFCG角色的Authorization Default入口维护服务对象,而非直接修改S_SERVICE授权对象。文章还提供了前后端授权配置的具体操作步骤,并特别提醒需同时配置前端和后端授权,才能确保应用完整可用性。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 223

13. 罗马数字转整数

罗马数字包含以下七种字符:I,V,X,L,C,D和M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做II,即为两个并列的 1。1...

YQianLiang的博客 569

Open3D 计算点云模型的表面积和体积【2025最新版】

python编程实现 计算点云模型的表面积和体积,博客长期更新,本文最近一次更新时间为:2025年4月4日。

点云侠的博客 13万+

Leetcode13 Roman to Integer

leetcode13 roman to Integer

Hobo1992的博客 151

深度学习工具包 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")

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

水果君の日常 3716

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 13. Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 意思是把罗马数字转成数字,那我们首先就要了解罗马数字是怎么组成的。 基本字符 I V X L

淡然坊 370

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

[Leetcode 13, Easy] Roman To Integer

Roman To Integer

算法的实践 477

LeetCode刷题实战(13):Roman to Integer

  题目描述: 13 Roman to Integer       49.5% Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V ...

10点43的博客 331

Leetcode OJ 13 Roman to Integer [Easy]

题目描述: Given a roman numeral, convert it to aninteger. Input is guaranteed to be within the rangefrom 1 to 3999. 题目理解: 给定一个罗马数字(字符串的形式),将其转换成一个整数,范围是1到3999 分析: 1.    罗马数字1到10为:I,II,III,IV,V,VI,VI

大吱的博客 310

leetcode13 Roman to Integer

13. Roman to Integer Total Accepted: 75285 Total Submissions: 196334 Difficulty: Easy Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from

葡萄架的程序猿 440

LeetCode (力扣) 13. Roman to Integer ( C ) - Easy- 暴力解

同步发于JuzerTech网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。

JuzerTech的博客 160

LeetCode Top Interview Questions 13. Roman to Integer (Java版; Easy)

welcome to my blog LeetCode Top Interview Questions 13. Roman to Integer (Java版; Easy) 题目描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I ...

littlehaes的博客 208

LeetCode 13 Roman to Integer (easy)——python

题目来源:                  https://leetcode.com/problems/roman-to-integer/description/  题目分析: 1.罗马数字介绍      本题需要对题目意思进行一定的解读,首先需要了解罗马数字的计数情况。      罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(10

revivre的博客 268

LeetCode #13. Roman to Integer-Easy

罗马数字转为普通数字表示,其中需要注意的是特殊数字的表示方法,比如和4,9相关的表示方法,因为都是非单个字符表示,如果遍历转换会出现麻烦 speciallist——特殊的罗马数字列表 valuedic——罗马数字与普通数字对应关系的字典 步骤:1.排查是是否有speciallist中的内容,有就记录下位置,通过valuedic得出对应的数字 2.将speciallis...

swedishsunset的专栏 235

LeetCode 13. Roman to Integer -- Easy

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 实现罗马数字到阿拉伯数字的转换。

Sublele的专栏 526

leetcode : 13. Roman to Integer

题目标签 string 题目难度 easy 题目地址: https://leetcode.com/problems/roman-to-integer/ 题目描述 : Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1...

gjwcsdn的博客 211
上一篇: leetcode_169_Majority Element(C++)(easy)
下一篇: leetcode_204_Count Primes(C++)(easy)
Lan_MuLan
博客等级 码龄14年 1粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值