分析

把具体的情况一个一个实现即可,没有什么幺蛾子。

代码

class Solution {
public int romanToInt(String s) {
int ans = 0;
for (int i=0; i!=s.length(); ++i)
{
switch(s.charAt(i))
{
case 'I':
if(i<s.length()-1 &&
(s.charAt(i+1)=='X' || s.charAt(i+1)=='V'))
{
ans--;
break;
}
ans++;
break;
case 'V':
ans+=5;
break;
case 'X':
if(i<s.length()-1 &&
(s.charAt(i+1)=='L' || s.charAt(i+1)=='C'))
{
ans-=10;
break;
}
ans+=10;
break;
case 'L':
ans+=50;
break;
case 'C':
if(i<s.length()-1 &&
(s.charAt(i+1)=='D' || s.charAt(i+1)=='M'))
{
ans-=100;
break;
}
ans+=100;
break;
case 'D':
ans+=500;
break;
case 'M':
ans+=1000;
break;
default: break;
}
}
return ans;
}
}

更好的代码

精彩的代码就是能够把所表达的意思用更少的代码跑更快的速度。以下代码无疑实现了这个目标:

class Solution {

    public static int romanToInt(String s) {
int num = 0;
int n = s.length(); for (int i = 0; i < n-1; i++) {
int curr = map(s.charAt(i));
int next = map(s.charAt(i+1));
num = curr < next ? num - curr : num + curr;
} num += map(s.charAt(n-1)); return num;
} private static int map(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;
}
}
}

「Leetcode」13. Roman to Integer(Java)的更多相关文章

  1. 「Leetcode」975. Odd Even Jump(Java)

    分析 注意到跳跃的方向是一致的,所以我们需要维护一个数接下来跳到哪里去的问题.换句话说,就是对于一个数\(A_i\),比它大的最小值\(A_j\)是谁?或者反过来. 这里有两种方案,一种是单调栈,简单 ...

  2. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

  3. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

  4. 13. Roman to Integer (JAVA)

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

  5. 「Leetcode」976. Largest Perimeter Triangle(C++)

    分析 好久不刷题真的思维僵化,要考虑到这样一个结论:如果递增的三个数\(x_i,x_{i+1},x_{i+2}\)不符合题意,那么最大的两边之差一定大于等于第一条边,那么任何比第一条边小的都不能成立. ...

  6. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  7. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  8. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  9. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

随机推荐

  1. robotframwork的WEB功能测试(一)—切换window窗口

    selenium2library提供的切换到新窗口的关键字,只有select window,而且也只能根据title.name.url去定位.如下图所示,明显在实际使用中是不够的. 所以这里总结了一下 ...

  2. 一起玩树莓派3+使用Gitlab搭建专业Git服务

    http://bbs.eeworld.com.cn/thread-505256-1-1.html https://packages.gitlab.com/gitlab/raspberry-pi2 ht ...

  3. JSP基本指令

    jsp命令指令用来设置与整个jsp页面相关的属性,它并不直接产生任何可见的输出,而只是告诉引擎如何处理其余JSP页面.其一般语法形式为: <%@ 指令名称 属性=“值”%> 三种命令指令分 ...

  4. 用 S5PV210 学习 Linux (一) 刷机(一)

    简介: 习惯了 用 keil 或者 IAR  一键下载 (烧写) 代码,S5PV210 貌似就不能这么简单用 仿真器的 方式 下载代码了,因此 学习 S5PV210 的第一步就是 学习怎么下载代码,下 ...

  5. 【星云测试】开发者测试(3)-采用精准测试工具对springcloud微服务应用进行穿透测试

    1.微服务简介 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及部署,并通过各自暴露的API接 ...

  6. 我告诉你 ,一个 window免费系统下载的网站!

    一个 window免费系统下载的网站! https://msdn.itellyou.cn/

  7. Machine Learning In Action

    The mind-road of "Machine Learning In Action". Read though the book totally by English!!

  8. bfs,队列

    bfs bfs=队列 队列的操作 头文件 #include<deque> 声明方法: 1.普通声明 queue<int>q; 2.结构体 struct node { int x ...

  9. MongoDB模糊查询 工具

    {"Exception":{$regex:"定时发送邮件"}}    //模糊查询条件 {"DateTime":-1}         // ...

  10. ASP.net 加载不了字体Failed to load resource: the server responded with a status of 404 (Not Found)

    在bootstrap下加载不了字体内容.出现下列错误. 1.打开IIS找到部署的网站,点击MIME类型,把.woff和.woff2两个类型分别添加到新类型中,重启网站即可.