Roman to Integer & Integer to Roman
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解析:
这题没兴趣做,抄答案
http://blog.csdn.net/jellyyin/article/details/13165731
 class Solution {
 public:
     int romanToInt(string s) {
         // Note: The Solution object is instantiated only once and is reused by each test case.
         int result=;
         map<char,int> roman;
         roman['I']=;
         roman['V']=;
         roman['X']=;
         roman['L']=;
         roman['C']=;
         roman['D']=;
         roman['M']=;
         for(int i=s.length()-;i>=;i--)
         {
             if(i==s.length()-)
             {
                 result=roman[s[i]];
                 continue;
             }
             if(roman[s[i]] >= roman[s[i+]])
                 result+=roman[s[i]];
             else
                 result-=roman[s[i]];
         }
         return result;
     }
 };
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解析:
 class Solution {
 public:
     string intToRoman(int num) {
         const int radix[] = {, , , , , ,
             , , , , , , };
         const string symbol[] = {"M", "CM", "D", "CD", "C", "XC",
             "L", "XL", "X", "IX", "V", "IV", "I"};
         string roman;
         for (size_t i = ; num > ; ++i) {
             int count = num / radix[i];
             num %= radix[i];
             for (; count > ; --count) roman += symbol[i];
         }
         return roman;
     }
 };
Roman to Integer & Integer to Roman的更多相关文章
- 【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 ...
 - LeetCode:Roman to Integer,Integer to Roman
		
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
 - Roman to Integer && Integer to Roman 解答
		
Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...
 - [string]Roman to Integer,Integer to Roman
		
一.Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...
 - list<Integer>,Integer[],int[]之间的互转(jdk1.8)
		
偶然在开发过程中需要将int[] 转成 List<Integer>,采用了遍历的方式,写的代码实在太多. List<Integer> list = new ArrayList& ...
 - java面试基础题------》int Integer Integer.valueOf
		
在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...
 - PostgresException: 42883: function ifnull(integer, integer) does not exist
		
原因在于PostGresql并没有自带IFNULL函数,可以用COALESCE来替代IFNULL,且COALESCE功能更强大,可以输入更多参数,顺序判断并返回第一个非null值. 例如: SELEC ...
 - [LeetCode] Integer to Roman 整数转化成罗马数字
		
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
 - 【leetcode】Integer to Roman
		
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
 
随机推荐
- Django summernote 富文本
			
Summernote is a simple WYSIWYG editor. GITHUB:https://github.com/summernote/django-summernote SETUP ...
 - Centos7中使用ipset
			
1.禁用firewalld systemctl stop firewalld systemctl disable firewalld 2.安装ipset yum -y install ipse ...
 - TypeError: only integer scalar arrays can be converted to a scalar index
			
TypeError: only integer scalar arrays can be converted to a scalar index 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...
 - shell多进程的实现
			
需求:多个脚本彼此互不干涉,同时运行,节省时间 菜鸟级实现: #!/bin/sh dir="/data/test" $dir/sbin/test1.sh >> $dir ...
 - angularjs结合plupload实现文件上传
			
转载注明:(罗志强的博客) angularjs的指令directive非常好使,可以很方便的结合各种插件,实现很强大的功能. 今天用到了plupload,就拿它举例吧. 正常的plupload用法应该 ...
 - Scrollbar的样式
			
.test{ /*立体滚动条凸出部分的颜色*/ scrollbar-face-color:#FEFAF1; /*滚动条空白部分的颜色*/ scrollbar-highlight-color:#FEFA ...
 - [php]php错误处理机制
			
1.判断文件是否存在,file_exists("文件名") or die("no such file");2.set_error_hanlder("错 ...
 - 【LibreOJ】#6298. 「CodePlus 2018 3 月赛」华尔兹 BFS
			
[题意]给定n*m的网格,起点和终点位置,一些格指定下一步的方向,一些格任意.要求为方向任意的格确定方向,使起点可以走到终点.n,m<=50. [算法]BFS [题解]这道题最好用BFS,因为D ...
 - 【CodeForces】708 B. Recover the String 数学构造
			
[题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...
 - JS 判断是否是微信浏览器 webview
			
原理很简单,就是判断 ua 中是否有字段 “micromessenger" 代码如下: function isWechat () { var ua = window.navigator.us ...