LeetCode--No.013 Roman to Integer
13. Roman to Integer
- Total Accepted: 95998
- Total Submissions: 234087
- Difficulty: Easy
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
思路:
根据上一篇的关于罗马数字的解释,我们可以知道,在罗马数字中左减右加的原则,所以当较小的数在较大的数前面时,表示是相减的,所以此题就显得很简单了
public int romanToInt(String s) { char [] arr = s.trim().toCharArray() ;
int res = toNumber(arr[0]) ;
for(int i = 1 ; i < arr.length ; i++){
if(toNumber(arr[i-1]) < toNumber(arr[i])){
res += toNumber(arr[i]) - 2*toNumber(arr[i-1]) ;
}else{
res += toNumber(arr[i]) ;
}
}
return res ; } private int toNumber(char ch) {
switch (ch) {
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;
}
return 0;
}
LeetCode--No.013 Roman to Integer的更多相关文章
- 【LeetCode】013. Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- 【JAVA、C++】LeetCode 013 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [Leetcode]013. Roman to Integer
public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
随机推荐
- mui页面传值
以下代码全部在script标签内 一.通过mui.openWindow()打开新页面(若目标页面为已预加载成功的页面,则在openWindow方法中传递的extras参数无效): mui.openWi ...
- django + nginx + uwsgi
server{ listen ; server_name 0.0.0.0; charset utf-; access_log /var/log/nginx/access.log main; locat ...
- ACM-ICPC 2018 南京赛区网络预赛 J.sum(欧拉筛)
题目来源:https://nanti.jisuanke.com/t/A1956 题意:找一个数拆成无平方因子的组合数,然后求前缀和. 解题思路:我们可以把某个数分解质因数,如果某个数可以分解出三个相同 ...
- 关于react的一些东西
Facebook 官方推出Create-React-App脚手架,基本可以零配置搭建基于webpack的React开发环境,内置了热更新等功能. 使用的原因以及特性: 无需配置:集成了对 React, ...
- [leetcode]205. Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- win7启动时怎么自动进入桌面
1.按Win+R组合键,打开“运行”对话框.(Win是键盘下方左右两边的两个印有微软标志的键) 2.Windows XP/2003/2008/2008R2输入"control userpas ...
- mysql空值排序
SELECT * FROM lzh_topic_channel_product ORDER BY order_id is null , order_id 其中的ORDER BY order_id i ...
- Chapter3_操作符_直接常量和指数计数法
(1)直接常量 在程序中使用直接常量,相当于指导编译器,告诉它要生成什么样的类型,这样就不会产生模棱两可的情况.比如flaot a = 1f等,后缀表示告诉编译器想生成的类型.常用的后缀有l/L(lo ...
- 编译搭建lnmp+zabbix
搭建nginx 1)基础依赖包安装 yum -y install gcc gcc-c++ vim tree make cmake autoconf yum -y install openssl ope ...
- 连续子数组和的最大值plus
package wodeshiyao; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStre ...