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的更多相关文章

  1. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  2. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  3. 【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 ...

  4. 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 ,即 ...

  5. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  6. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  7. [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 ...

  8. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  9. 【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 ...

  10. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

随机推荐

  1. CentOS 静态IP分配,提示Error, some other host already uses address解决办法

    (一)第一 修改 ifup-eth vi /etc/sysconfig/network-scripts/ifup-eth #if ! /sbin/arping -q -c 2 -w 3 -D -I $ ...

  2. Android OpenGL ES 开发(九): OpenGL ES 纹理贴图

    一.概念 一般说来,纹理是表示物体表面的一幅或几幅二维图形,也称纹理贴图(texture).当把纹理按照特定的方式映射到物体表面上的时候,能使物体看上去更加真实.当前流行的图形系统中,纹理绘制已经成为 ...

  3. Promise,我们来实战

    Promise的基础我就不说了,我更加关心的是Promise能带给我什么便利?这也是本文的内容所在. Promise.all 并发执行多个异步方法,统一返回结果,结果按照参数的顺序返回. const ...

  4. SqlServer之like、charindex、patindex 在有无索引的情况下分析

    1.环境介绍 测试环境 SQL2005 测试数据 200W条 2.环境准备 2.1建表 CREATE TABLE [dbo].[Depratments](         [Dep_id] [int] ...

  5. select函数的介绍和使用

    我们所使用的I/O模型一共有五种. 分别为阻塞I/O,非阻塞I/O,I/O复用,信号驱动I/O,异步I/O. 所谓I/O复用就是指管理多个I/O文件描述符,一般会使用(select,poll,epol ...

  6. Samba原理和配置

    Samba原理和配置 个人原创,转载请注明,否则追究法律责任. 一,原理及安装 1,Samba是在Linux和UNIX系统上实现在局域网上共享文件一种通信协议,它为局域网内的不同计算机之间提供文件等资 ...

  7. python函数名称空间

    一.命名空间概念 命名空间(name space),若变量x=1,1存放在内存中,命名空间是存放名字x.x与1绑定关系的地方.命名空间分三种: locals:函数内的名称空间,包括局部变量和形参 gl ...

  8. [Tarjan 学习笔记](无向图)

    今天考试因为不会敲 Dcc 的板子导致没有AK(还不是你太菜了),所以特地写一篇博客记录 Tarjan 的各种算法 无向图的割点与桥 (各种定义跳过) 割边判定法则 无向边 (x,y) 是桥,当且仅当 ...

  9. 【jQuery】 JQ和HTML以及JQ遍历元素

    JQ & HTML JQ作为一个JS库,很好地继承了JS和HTML互动的特点,并且还给出了包装得更好,操作层次更高的方法.和之前JQ的内容一样,方法的表现形式是$(selector).acti ...

  10. Flashing Back a Failed Primary Database into a Physical Standby Database(闪回FAILOVER失败的物理备库)

    文档操作依据来自官方网址:https://docs.oracle.com/cd/E11882_01/server.112/e41134/scenarios.htm#SBYDB4888 闪回FAILOV ...