【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 解题思路:将数字 ...
随机推荐
- POJ1737 Connected Graph
Connected Graph Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3156 Accepted: 1533 D ...
- The Honeynet ProjectThe Honeynet Project
catalogue . 蜜罐基本概念 . Kippo: SSH低交互蜜罐安装.使用 . Dionaea: 低交互式蜜罐框架部署 . Thug . Amun malware honeypots . Gl ...
- MySql与Java的时间类型
MySql的时间类型有 Java中与之对应的时间类型date java.sql.DateDatet ...
- Notions of Flow Networks and Flows
这篇随笔是对算法导论(Introduction to Algorithms, 3rd. Ed.)第26章 Maximum Flow的摘录. ------------------------------ ...
- iOS 知识点梳理
OC的理解与特性 OC作为一门面向对象的语言,自然具有面向对象的语言特性:封装.继承.多态.它既具有静态语言的特性(如C++),又有动态语言的效率(动态绑定.动态加载等).总体来讲,OC确实是一门不错 ...
- HD1712ACboy needs your help(纯裸分组背包)
ACboy needs your help Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- threading使用心得
需求:监控多个重要网站(多线程),出现访问异常重试2次,第三次开始告警. 日志模块 import logging import logging.config # 日志配置 LOGCONF_FILENA ...
- Android事件机制之一:事件传递和消费
http://www.cnblogs.com/lwbqqyumidi/p/3500997.html 关于Android中的事件机制,用到的地方还是很多的,并且这个知识点还真有点复杂. 在写这篇文章前, ...
- dede5.7前台插入恶意JS代码
这个问题应该很久了 最近发现有用这个的蠕虫,dede 前台提交友情链接 只用htmlspecialchars简单处理了一下 可以插入代码plus/flink_add.php 提交: 表单中提交 图片地 ...
- jQuery插件 -- Form表单插件jquery.form.js
http://blog.csdn.net/zzq58157383/article/details/7718956 http://my.oschina.net/i33/blog/77250