LeetCode & Q13-Roman to Integer-Easy
Math String
Description:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
What is Roman Numeral?
my Solution:
public class Solution {
public int romanToInt(String s) {
int[] priority = new int[s.length()];
for (int i = 0; i < s.length(); i++) {
switch (s.charAt(i)) {
case 'I':
priority[i] = 1;
break;
case 'V':
priority[i] = 5;
break;
case 'X':
priority[i] = 10;
break;
case 'L':
priority[i] = 50;
break;
case 'C':
priority[i] = 100;
break;
case 'D':
priority[i] = 500;
break;
case 'M':
priority[i] = 1000;
break;
}
}
int output = 0;
for (int i = 1; i < priority.length; i++) {
if (priority[i] > priority[i - 1]) {
output -= priority[i-1];
} else {
output += priority[i-1];
}
}
output += priority[priority.length - 1];
return output;
}
}
稍好一点的做法:
public static int romanToInt(String s) {
int res = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
switch (c) {
case 'I':
res += (res >= 5 ? -1 : 1);
break;
case 'V':
res += 5;
break;
case 'X':
res += 10 * (res >= 50 ? -1 : 1);
break;
case 'L':
res += 50;
break;
case 'C':
res += 100 * (res >= 500 ? -1 : 1);
break;
case 'D':
res += 500;
break;
case 'M':
res += 1000;
break;
}
}
return res;
}
LeetCode & Q13-Roman to Integer-Easy的更多相关文章
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 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 ,即 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [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 ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 【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 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
随机推荐
- java设计模式-----11、代理模式
Proxy模式又叫做代理模式,是构造型的设计模式之一,它可以为其他对象提供一种代理(Proxy)以控制对这个对象的访问. 所谓代理,是指具有与代理元(被代理的对象)具有相同的接口的类,客户端必须通过代 ...
- 简单的同步Socket程序服务端
首先,Socket是.Net提供的 System.Net.Sockets命名空间的Scoket类为网络通信提供了一套丰富的方法和属性 服务器按照Socket的基本流程 先创建Socket 在用Bind ...
- APIcloud 移动端常用事件
1.监听按键事件 返回键 api.addEventListener({ name:'keyback' }, function(ret, err){ if( ret ){ alert( JSON.str ...
- js--DOM&BOM总结思维导图---2017-03-24
- js 显示数字不断增加
一个小小的函数,很容易看懂,就不过多解释了,只为下次用时直接用就ok了... function countUp(count) { var div_by = 100, speed = Math.roun ...
- EOS 的世界里可能再也没有小偷了
EOS 针对以下两种情况设计了应急措施: 1. 账户被盗(私钥被盗或有权限的其他账户被盗) 2. 账户遗失(私钥遗失或有权限的其他账户遗失) ## 1.账户被盗 EOS 有可能会强制要求所有账户的 O ...
- 解决html5 canvas 绘制字体、图片与图形模糊问题
html5 canvas 绘制字体.图片与图形模糊问题 发生情况 多出现在高dpi设备,这意味着每平方英寸有更多的像素,如手机,平板电脑.当然很多高端台式电脑也有高分辨率高dpi的显示器. canva ...
- 堆排序(Java数组实现)
堆排序:利用大根堆 数组全部入堆,再出堆从后向前插入回数组中,数组就从小到大有序了. public class MaxHeap<T extends Comparable<? super T ...
- 常用css样式颜色值: 64位真彩和256位值
1. background-color: #eee; 2. background-color: #797979; 3. background-color: #007aff; 继续更新中
- 笔记:Maven 下载和安装
Windows 安装 下载 Apache Maven,下载地址为 http://maven.apache.org/ 解压缩下载的 ZIP 文件,复制到安装目录 增加环境变量 M2_HOME ,值为 A ...