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 解题报告的更多相关文章

  1. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  2. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  3. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  4. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  5. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. 【LeetCode】3Sum Closest 解题报告

    [题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...

随机推荐

  1. bg和fg命令

    linux提供的fg和bg命令,可以让我们轻松调度正在运行的任务   假如你发现前天运行的一个程序需要很长的时间,但是需要干前天的事情,你就可以用ctrl-z挂起这个程序,然后可以看到系统的提示: [ ...

  2. okhttp进行网络传输文件

    其中使用了RXJava. public class HttpDataManager { private static HttpDataManager INSTANCE; private Request ...

  3. 搭建Weblogic服务器

    安妮,我的小熊熊在ne....... 01.安全设置 service iptables stop chkconfig iptables off    #关闭防火墙,只是建议,为了简便操作 setenf ...

  4. VC线程同步方法

    VC MFC中线程同步对象的区别 临界区 CCriticalSection,在用户模式工作,适用于保护线程间共享资源,一个线程可以多次Lock不会出错.不支持在多进程之间工作.互斥量 CM ...

  5. 如何在网上隐藏自己的IP地址(转)

    摘自:http://net.yesky.com/396/3082396.shtml 在某些场合(例如论坛发贴时)有些网友希望能隐藏自己的IP地址,以免IP被记下,为安全起见,QQ聊天时也不希望别人知道 ...

  6. WordPress网站搬家经验总结

    http://cnzhx.net/blog/move-wordpress-site-step-by-step/也许很多人都有跟我类似的经历:因为某种原因需要将自己的WordPress站点从一个空间转移 ...

  7. Android开发学习之SQLite数据存取浅析

    一.SQLite的介绍 1.SQLite简介 SQLite是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入 式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低 ...

  8. Device trees, Overlays and Parameters of Raspberry Pi

    Raspberry Pi's latest kernels and firmware, including Raspbian and NOOBS releases, now by default us ...

  9. C#中遍历DataTable类型并删除行数据

    从数据库中读取出了DataSet类型的数据,通过dataSet.Tables[0]获得DataTable类型的数据. 这时候如果想批量修改dataTable中的内容,比如要删除dataTable中co ...

  10. .net mvc前台如何接收和解析后台的字典类型的数据 二分搜索算法 window.onunload中使用HTTP请求 网页关闭 OpenCvSharp尝试 简单爬虫

    .net mvc前台如何接收和解析后台的字典类型的数据   很久没有写博客了,最近做了一个公司门户网站的小项目,其中接触到了一些我不会的知识点,今日事情少,便记录一下,当时想在网上搜索相关的内容,但是 ...