问题描述:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

算法:

/**
* 输入一个罗马数字,返回它的整型表示
* @author admin
* 转换规则:从右向左或者从左向右遍历均可
* 1 相同数字连写,相加 ;2 小数字在大数字右边,相加;3 小数字在大数字左边,大的减去小的
*/

方法一:hashmap 从右向左遍历

//将罗马数字转换为整型值
public static int romanToInt(String s){
    HashMap<Character, Integer> roma_weight = new HashMap<Character, Integer>();
    roma_weight.put('I', 1);
    roma_weight.put('V', 5);
    roma_weight.put('X',10);
    roma_weight.put('L', 50);
    roma_weight.put('C', 100);
    roma_weight.put('D',500);
    roma_weight.put('M',1000);
    char[] roma = s.toCharArray(); //将罗马数字转化为字符数组
    int length = roma.length;
    int preValue = 0; //前一步的value
    int value = roma_weight.get(roma[length - 1]) ;// 当前的value     //从右向左遍历
    for(int i = length - 2; i >= 0 ; i--){
      //小的数字在大数字右边,直接相加
      if( roma_weight.get(roma[i]) >= roma_weight.get(roma[i + 1])) {
        preValue = value;
        value = value + roma_weight.get(roma[i]);
      }
      else { //小的数字在大数字左边,大的减去小的
        value = preValue + roma_weight.get(roma[i + 1]) - roma_weight.get(roma[i]);
      }
    }
    return value;
}

方法二:switch case,从左向右遍历

//与第一种方法基本相同,从左向右
public static int romanToInt1(String s) {
  char[] ss = s.toCharArray();
  int ret = toNumber(ss[0]);
  for (int i = 1; i < ss.length; i++) {
    if (toNumber(ss[i - 1]) < toNumber(ss[i])) {
      //ret - toNumber(ss[i - 1]) = preValue; 然后再进行 preValue + toNumber(ss[i]) - toNumber(ss[i - 1]);
      ret += toNumber(ss[i]) - 2 * toNumber(ss[i - 1]); //相当于方法一中的 preValue + roma_weight.get(roma[i + 1]) - roma_weight.get(roma[i]);
    } else {
      ret += toNumber(ss[i]);
   }
}
return ret;
} //罗马数字权重表,没有空间复杂度,较hashmap要好一些
public static 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;
}

Roman To Integer leetcode java的更多相关文章

  1. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

  2. Roman to Integer -- LeetCode 13

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  3. Roman to Integer [LeetCode]

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  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. Reverse Integer LeetCode Java

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

  6. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  7. [LeetCode][Python]Roman to Integer

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

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

  9. LeetCode: Roman to Integer 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

随机推荐

  1. MySQL 安装步骤

    今天用了一下MySQL,刚好看到之前电保存脑的笔记,于是整理了一下,还是记在博客上方便查询. 1.官网下载https://dev.mysql.com/downloads/mysql/之前安装的是mys ...

  2. Multi-attention Network for One Shot Learning

    Multi-attention Network for One Shot Learning 2018-05-15 22:35:50  本文的贡献点在于: 1. 表明类别标签信息对 one shot l ...

  3. Docker5之Deploy your app

    Make sure you have published the friendlyhello image you created by pushing it to a registry. We’ll ...

  4. cron,linux定时脚本

    Linux的cron和crontab Cron定时执行工具详解 Linux下的crontab定时执行任务命令详解 Linux上启动Cron任务 [linux]解析crontab cron表达式详解 c ...

  5. linux mysql操作命令大全

    1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...

  6. JavaScript(2)

    JavScript在页面上显示时间,首先我们先来了解关于时间的一些简单方法: getFullYear()  获取当前年份,getMonth() 0-n(一月到十二月),getDate() 1-31(月 ...

  7. Lintcode415-Valid Palindrome-Medium

    Given a string, determine if it is a palindrome, considering only alphanumeric(字母和数字) characters and ...

  8. CSS--点击改变样式

    当某个链接或元素被选中时可以时,需要改变其颜色或状态,而stylus中提供&选择器,&指向父选择器,用于判断父元素达到某条件时改变状态,下面的例子中当父元素router-link有被选 ...

  9. Vue--获取数据

    一.Jsonp抓取数据 用 npm 安装 jsonp npm install jsonp 创建 jsonp.js import originJsonp from 'jsonp' export defa ...

  10. 【三】php 数组

    数组 1.数字索引数组:array('a','b','c');  2.访问数组内容 $arr[下标] 3.新增数组元素 $arr[下标]=内容 4.使用循环访问数组 //针对数字索引 $arr=arr ...