LeetCode: Integer to Roman 解题报告
Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
SOLUTION 1:
从大到小的贪心写法。从大到小匹配,尽量多地匹配当前的字符。
罗马数字对照表:
http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
package Algorithms.string; public class IntToRoman {
public String intToRoman(int num) {
int nums[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] romans = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; StringBuilder sb = new StringBuilder(); int i = 0;
// 使用贪心法。尽量拆分数字
while (i < nums.length) {
if (num >= nums[i]) {
sb.append(romans[i]);
num -= nums[i];
} else {
i++;
}
} return sb.toString();
}
}
2015.1.12 redo:
public String intToRoman(int num) {
int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; /*
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X" IX V, IV, I
*/
String[] strs = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int i = 0;
StringBuilder sb = new StringBuilder();
// greedy.
while (i < nums.length) {
// bug 1: should use ">="
if (num >= nums[i]) {
sb.append(strs[i]);
// bug 2: should remember to reduce the nums[i].
num -= nums[i];
} else {
i++;
}
} return sb.toString();
}
请至主页君GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/IntToRoman.java
LeetCode: Integer to Roman 解题报告的更多相关文章
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
随机推荐
- CentOS 中文乱码
同事刚装的一台CentOS服务器,SSH登录乱码: 猜测应该是安装时选择的是简体中文,因为发现/etc/sysconfig/i18n文件里面是zh_CN. LANG="zh_CN.UTF-8 ...
- 3DTouch - iOS新特性
概述 3DTouch是一种立体触控技术,被苹果称为新一代多点触控技术. 详细 代码下载:http://www.demodashi.com/demo/10708.html 6s和6s plus之后特有效 ...
- 利用Aspose.BarCode生成条码
生成条码有很多控件,大部分好的控件,做出来的条码精确清晰,但是往往该部分的控件费用都很高,这里推荐大家使用6.0版本的破解版,可以生成无水印,下方也可以不显示文字. 1.下图是 onbarcode 控 ...
- [转载]在rhel 6 x86_64 上安装oracle 11g xe
原文地址:在rhel 6 x86_64 上安装oracle 11g xe作者:pccom Oracle 11g xe for linux目前只有x86_64 版本,没有i386, i686 版本,如果 ...
- ORM轻量级框架---ActiveAndroid
ORM即Object-Relational Mapping,对象关系映射.简单理解就是把我们Java的对象与数据库里面的记录进行映射,可以把实体对象持久化到数据库中,也能把查询到的记录映射成Java对 ...
- CSS:关于CSS Hack
CSS Hack由于不同厂商的浏览器,如Internet Explorer,Safari,Mozilla Firefox,Chrome 等,或者是同一厂商的浏览器的不同版本,如IE6和IE7,对CSS ...
- JSEclipse—Eclipse上的JavaScript开发工具
http://blog.csdn.net/qiaogang2003/article/details/3035056原来js开发仅仅使用ue,不过开发效率比较低下. 找到一个Eclipse下的js开发工 ...
- Android四款系统架构工具
开发者若想开发出一款高质量的应用,一款功能强大的开发工具想必是不可或缺的.开发工具简化了应用的开发流程,也能使开发者在应用开发本身投入更多的精力.本文就为大家带来4款实用的Android应用架构工具. ...
- ASP.NET Web API使用示例
原文地址:https://blog.csdn.net/chinacsharper/article/details/21333311 上篇博客讲解rest服务开发时,曾经提到过asp.net mvc中的 ...
- 【Linux】Linux文件属性
既然说要了解Linux的文件属性,那么有个重要的也是常用的指令就必须要先介绍一下:就是『 ls 』这一个查看文件的命令!在以root的身份登入Linux之后,下达『 ls -al 』,结果如下所示: ...