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 ...
随机推荐
- windows彻底删除Oralce
以下是彻底删除Oralce的步骤:1. 开始->设置->控制面板->管理工具->服务停止所有Oracle服务. 2. 开始->程序->Oracle - OraHom ...
- cydia源
http://apt.91.com http://apt.178.com Flex 2的官方安装源是 getdelta.co
- systemctl的常用命令
#systemctl #systemctl --all #systemctl list-units --type=sokect #systemctl list-units --type=service ...
- exception Access restriction: The type 'BASE64Encoder' is not API
Created by Marydon on 1.情景展示 在eclipse中切换jdk版本后,报错信息为:exception Access restriction: The type 'BASE6 ...
- SettingsJDK
迁移时间:2017年5月20日23:38:40 Author:Marydon 1.双击安装,更改安装路径为D:\ProgramFiles\Java\jdk1.7.0_55: 注意事项: 1.1 将 ...
- div最小高度的2种写法
1.第一种写法: 原理:在IE6中,使用CSS定义div的高度的时候经常遇到这个问题,就是当div的最小高度小于一定的值以后,就会发现,无论你怎么设置最小高度,div的高度会固定在一个值不再发生变动, ...
- office-word去掉效验红色的波浪线
工作中,总是能发现不足.能再次学习到知识和经验!
- PHP-表达式
最精确的定义一个表达式的方式就是"任何有值的东西" $a = 5; 1 > 2;等
- beanUtils的用法
举例1:使用BeanUtils工具封装用户提交的数据. public static void main(String[] args)throws Exception { // 模拟用户的输入的数据如下 ...
- OGG_GoldenGate安装和环境搭建(案例)
2014-03-02 Created By BaoXinjian