leetcode解决问题的方法||Integer to Roman问题
problem:
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
将1-3999的整数转换为罗马数字
thinking:
(1)
对比举例
个位数举例
Ⅰ,1 】Ⅱ。2】 Ⅲ,3】 Ⅳ,4 】Ⅴ。5 】Ⅵ,6】Ⅶ。7】 Ⅷ,8 】Ⅸ。9 】
十位数举例
Ⅹ。10】 Ⅺ,11 】Ⅻ,12】 XIII,13】 XIV,14】 XV,15 】XVI,16 】XVII,17 】XVIII,18】 XIX,19】 XX,20】 XXI,21 】XXII,22 】XXIX,29】 XXX,30】 XXXIV,34】 XXXV,35 】XXXIX,39】 XL,40】 L,50 】LI,51】 LV,55】 LX,60】 LXV,65】 LXXX,80】 XC,90 】XCIII,93】 XCV,95 】XCVIII,98】 XCIX,99 】
百位数举例
C,100】 CC,200 】CCC,300 】CD,400】 D,500 】DC,600 】DCC,700】 DCCC,800 】CM,900】 CMXCIX,999】
千位数举例
M,1000】 MC,1100 】MCD,1400 】MD,1500 】MDC,1600 】MDCLXVI,1666】 MDCCCLXXXVIII,1888 】MDCCCXCIX,1899 】MCM,1900 】MCMLXXVI,1976】 MCMLXXXIV,1984】 MCMXC,1990 】MM,2000 】MMMCMXCIX,3999】
(2)避免N多条件推断的技巧:利用罗马数字自身的规律。减去一个基数。来简化复杂度
code:
class Solution {
public:
string intToRoman(int num)
{
int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
string result;
for (int i = 0; i < 13; i++) {
while (num >= values[i]) {
num -= values[i];
result.append(numerals[i]);
}
}
return result;
}
};
leetcode解决问题的方法||Integer to Roman问题的更多相关文章
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- leetcode第12题--Integer to Roman
Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- LeetCode(12)Integer to Roman
题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- [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] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
随机推荐
- php类 静态变量赋值 static $name="abc"
<?php class test { static $name="abc"; } echo test::$name; 1.巴斯科范式 class_statement: var ...
- 解锁windowsphone设备遇到的错误:检查Miscrosoft账户凭据、请重新注册 0x80004005 解决方案
本篇文章主要讲在解锁windowsphone设备时遇到的错误 Error1:登录windowsphone开发人员中心时出错,请检查您的miscrosoft账户凭据 Error2:注册您的手机时出现未知 ...
- PopupWindow-弹窗的界面
1 效果图 2 知识点 PopupWindow(View contentView, int width, int height) //创建一个没有获取焦点.长为width.宽为height,内容为 ...
- 理解java reference
Java世界泰山北斗级大作<Thinking In Java>切入Java就提出“Everything is Object”.在Java这个充满Object的世界中,reference是一 ...
- LINQ,EF联合查询join
public object GetListAdmin() { //return db_C56.Admins // .Where(a => a.Status != "D") ...
- jdk 1.5新特性说明
“JDK1.5”的一个重要主题就是通过新增一些特性来简化开发,这些特性包括泛型,for-each循环,自动装包/拆包,枚举,可变参数,静态导入.使用这些特性有助于我们编写更加清晰,精悍,安全的代码. ...
- mysql远程连接出现 ERROR 2003 (HY000): Can't connect to MySQL server on IP
修改了如下两个位置,解决了这个问题: 修改/etc/mysql/my.cof配置文件:因为mysql默认只允许本机连接 修改远程连接用户权限:远程连接的用户被设置为不允许远程连接 首先修改/etc/m ...
- Flash AIR14导出ipa到Mac上的iOS模拟器测试
没错!你没看错!俺这篇博客就是关于Flash AIR的! ----------------无聊的分割线------------------ 朋友最近学习Flash AIR for iOS开发,想找我帮 ...
- IE兼容性问题解决方案4--form表单在IE下重复提交
遇到过一种情况,点击提交按钮的时候,在IE下重复提交,而在其他浏览器下正常. 原因:button按钮不设置type时,在IE下被浏览器默认解析为type="submit",用js提 ...
- C/C++:原码、反码和补码
正数的原码.反码和补码是一模一样的. 负数的反码的符号位跟原码一样,但其余各位取反. 负数的补码是其反码的最末位加1得到,即原码取反加1. 补码的补码就是原码. 浮点数的存储格式随着机器的 ...