【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 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的更多相关文章
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【JAVA、C++】LeetCode 002 Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- 【JAVA、C++】LeetCode 022 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【JAVA、C++】LeetCode 010 Regular Expression Matching
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 【JAVA、C++】LeetCode 007 Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...
随机推荐
- scanf的一些技巧
一.scanf和gets 1.不同点: char string[50]; scanf("%s",string); //当遇到回车,空格和tab键会自动在字符串后面添加'\0',但是 ...
- 【CodeForces 626C】Block Towers
题意 给你n,m,如果 n个2的倍数和m个3的倍数,这n+m个数各不相同,那么求最大的数的最小值. 分析 方法1:枚举最大值为i,直到 i/2+i/3-i/6(不重复的2或3的倍数)≥n+m,并且要i ...
- Oracle创建DBLink的方法
文章从http://blog.csdn.net/davidhsing/article/details/6408770拷贝过来的 1.如果需要创建全局 DBLink,则需要先确定用户有创建 dblink ...
- python 学习5--matplotlib画图实践
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 学习参考: http://www.cnblogs.com/vamei/archive/2013/01/30/28 ...
- python学习笔记4(对象/引用;多范式; 上下文管理器)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 21. 动态类型:对象/引用 对象和引用: 对象是储存在内存中的实体,对象名只是指向这一对象的引用(refere ...
- iOS开发编码建议与编程经验
作者:乞力马扎罗的雪(GitHub) 原文 在开发过程中,我们不仅要去看别人的代码,也要让别人看我们的代码.那么,有一个良好的编码习惯将会非常重要.下面将会罗列使用Objective-C来开发iOS的 ...
- HD1281棋盘游戏(匹配+好题)
棋盘游戏 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- easyui form 方式提交数据
http://ldzyz007.iteye.com/blog/2067540 <form id="ff" method="post"> . ...
- std::thread join和detach区别
thread detach, join 线程有两种状态,joinable或者detachable,pthread默认创建的线程是joinable的,也可以指定atrribute创建成一个detacha ...
- 用过SQL语句获取织梦DedeCMS每个栏目各有多少文章
我对SQL语句不是很精通,这个SQL调用语句是我在一个模板里面看到了,特来和大家分享,大家在制作模板的过程中有需要可以用得到. 显示效果: 共有会员:31 名 本月更新:39 ...