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. 查询Date时时间查询不出来

    今天项目开发时,遇到了一个小问题,就是使用Hibernate用sql查询时,查询不出来时间部分, 网上没有找到答案,最终使用:to_char(sx.createtime,'yyyy-MM-dd hh2 ...

  2. 关于CoordinatorLayout的用法——复杂交互的克星

    好久没有写博客了,主要还是任务过多哈.在开发的过程当中,也记录了很多东西,但是技术这个事吧,其实,时效性真的事非常强--就比如说,你昨天还津津乐道的一个难点解决方案,你过个几天再回过头去看它,就会有一 ...

  3. ------ 开源软件 Tor(洋葱路由器,构建匿名网络的方案之一)源码分析——主程序入口点(二)------

    ---------------------------------------------------------- 第二部分仅考察下图所示的代码片段--configure_backtrace_han ...

  4. Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解

    题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...

  5. c# redis 操作类库推荐:StackExchange.Redis.Extensions

    StackExchange是一个优秀的c# redis客户端,但是存在操作略为繁琐的弊端,为了简化操作,使用 StackExchange.Redis.Extensions成为了一个非常值得推荐的选择. ...

  6. HBase新的客户端接口

    最近学习接触HBase的东西,看了<Habase in Action>,但里面关于HBase接口都是过时的接口,以下为HBase新的客户端接口: package com.n10k; imp ...

  7. OCR技术浅探:基于深度学习和语言模型的印刷文字OCR系统

    作者: 苏剑林 系列博文: 科学空间 OCR技术浅探:1. 全文简述 OCR技术浅探:2. 背景与假设 OCR技术浅探:3. 特征提取(1) OCR技术浅探:3. 特征提取(2) OCR技术浅探:4. ...

  8. java判断用户输入的是否至少含有N位小数

    判断用户输入的是否至少含有N位小数. 1.当用户输入的是非数字时抛出异常,返回false. 2.当用户输入数字是,判断其数字是否至少含有N位小数,如果不含有,返回false. 3.当用户输入的数字的小 ...

  9. Eclipse CDT开发环境搭建及问题记录(Windows)

    这两天在整Eclipse,在此记录过程中遇到的一些问题. 1.安装JDK,配置系统路径: 2.下载Eclipse 直接访问Eclipse官网(https://www.eclipse.org/downl ...

  10. python列表操作符

    list1=[123,456] list2=[234,234] list1>list2 >>>False#返回False 第一项比较之后直接返回false,第二项不看 #+实现 ...