Given a roman numeral, convert it to an integer.

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

解题思路:

类似上题,方法多多,本题直接给出上题中字典匹配的代码:

JAVA实现:

static public int romanToInt(String s) {
int num=0;
String Roman[][] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
StringBuilder sb=new StringBuilder(s);
for(int i=Roman.length-1;i>=0;i--){
//由于罗马字母无法表示0,因此,可以认为j>=1
for(int j=Roman[i].length-1;j>=1;j--){
if(sb.length()>=Roman[i][j].length()&&sb.substring(0,Roman[i][j].length()).equals(Roman[i][j])){
num+=j*Math.pow(10, i);
sb.delete(0,Roman[i][j].length());
break;
}
}
}
return num;
}

C++:

 class Solution {
public:
int romanToInt(string s) {
int num = ;
vector<vector<string>> Roman = {
{ "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
{ "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
{ "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
{ "", "M", "MM", "MMM" }
};
string sb = s;
for (int i = Roman.size() - ; i >= ; i--) {
//由于罗马字母无法表示0,因此,可以认为j>=1
for (int j = Roman[i].size() - ; j >= ; j--) {
if (sb.length() >= Roman[i][j].length() && sb.substr(, Roman[i][j].length())==(Roman[i][j])) {
num += j*pow(, i);
sb.erase(,Roman[i][j].length());
break;
}
}
}
return num;
}
};

【JAVA、C++】LeetCode 013 Roman to Integer的更多相关文章

  1. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 【JAVA、C++】LeetCode 018 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  3. 【JAVA、C++】LeetCode 015 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  4. 【JAVA、C++】LeetCode 012 Integer to Roman

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

  5. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

随机推荐

  1. 【CodeForces 624D】Array GCD

    题 You are given array ai of length n. You may consecutively apply two operations to this array: remo ...

  2. Spring的拦截器

    <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>or ...

  3. mint锁屏设置

    心血来潮,给笔记本装了linux,版本是当下最火的mint.唔,使用体验不错-下面记录的是修改mint锁屏相关设置的修改方式. In Linux Mint 13, the screen lock fe ...

  4. 千万不要误用 java 中的 HashCode 方法

    刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...

  5. tp框架查询

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...

  6. Java web小记

    1.Java Web设置页面刷新的方法(两种): response.setHeader("refresh", "0.3," + request.getHeade ...

  7. Nginx一致性哈希模块的Lua实现

    Nginx一致性哈希模块的Lua重新实现 技术背景: 最近在工作中使用了nginx+redis 的架构,redis在后台做分布式存储,每个redis都存放不同的数据,这些数据都是某门户网站通过Hado ...

  8. C# 排序算法记录

    class Program { static void Main(string[] args) { , , , , , , , , -, , , }; //假设一个最小的值 ]; ; i < a ...

  9. WPF 获取IP地址

    List<string> ipList = new List<string>(); foreach (IPAddress _IPAddress in Dns.GetHostEn ...

  10. 我所使用的一个通用的Makefile模板

    话不多说,请看: 我的项目有的目录结构有: dirls/ ├── include │   └── apue.h ├── lib │   ├── error.c │   ├── error.o │   ...