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 ...
随机推荐
- json(JavaScript Object Natation)学习
Json必需的包: commons-httpclient-3.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar json-lib-2.2.3-j ...
- Stage3d 由浅到深理解AGAL的管线vertex shader和fragment shader || 简易教程 学习心得 AGAL 非常非常好的入门文章
Everyday Stage3D (一) Everyday Stage3D (二) Triangle Everyday Stage3D (三) AGAL的基本概念 Everyday Stage3D ( ...
- poj 4014 Dice 贪心
//poj 4014 //sep9 #include <iostream> #include <algorithm> using namespace std; int n; s ...
- jQuery知识集锦
CreateTime--2017年2月16日14:00:22Author:MarydonjQuery知识集锦1.empty()与remove()的区别 <select id="ty ...
- webservice系统学习笔记10-使用jax-ws创建基于tomcat类型的容器的ws服务
1.在web-info目录下新建目录wsdl 2.在1步的目录下 新建文件user.wsdl <?xml version="1.0" encoding="UTF-8 ...
- 23、List集合
1.List List接口是Collection的子接口,用于定义线性表数据结构.List是可重复集 2.List自身定义的方法 List处理继承Collection方法外,自己还定义了其它方法,例如 ...
- 1、创建一个空白的xls和xlsx文件
1.创建一个空白的xls文件 Step1:先引入库NPOI.dll文件 Step2: ①:实例化一个workbook,实为在内存表中创建一个xls文件 NPOI.HSSF.UserModel.HSSF ...
- Javascript模块化编程:require.js的用法
摘自:http://blog.jobbole.com/30046/ 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库 ...
- poj----Maximum sum(poj 2479)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30704 Accepted: 9408 Desc ...
- Atom 检测php错误扩展linter-php