[leetcode]_Integer to Roman
题目:对应之前那道将罗马数字转换整型数字的题目。反过来。
思路:刚开始做的时候,想着用程序进行判断,复杂的要死。网络了别人代码,非常清晰。
代码:
1 public String intToRoman(int num) {
String[] alpha = {"M" ,"CM" , "D" , "CD" , "C" ,"XC" , "L" , "XL" , "X" ,"IX" , "V" , "IV" , "I"};
int[] value = new int[]{1000 ,900 , 500 , 400 , 100 , 90 , 50 , 40 ,10 , 9 , 5 , 4 , 1};
String result = new String();
for(int i = 0 ; num != 0 ; i++){
while(num >= value[i]){
num -= value[i];
result += alpha[i];
}
}
return result;
}
用贪心的思路,直接AC,very nice~
[leetcode]_Integer to Roman的更多相关文章
- leetcode第一刷_Integer to Roman
这道题当时不会写,是參照discuss写的. 首先要弄明确罗马数字的规则,这个在国外难道是常识吗.为什么题干一点都没讲.. 4000以下一共同拥有以下几种符号:"M", " ...
- [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】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an 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】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 【算法】LeetCode算法题-Roman To Integer
这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...
随机推荐
- [Flex] PopUpButton系列 —— 设置弹出菜单与主按钮之间的间隔
<?xml version="1.0" encoding="utf-8"?><!--设置弹出菜单与主按钮之间的间隔 PopUpButtonPo ...
- /dev/shm
/dev/shm/是linux下一个特殊的目录,因为这个目录不在硬盘上,而是在内存里. /dev /shm/需要注意的一个是容量问题,在linux下,它默认最大为内存的一半大小,使用df -h命令可以 ...
- ZOJ 2404 Going Home 【最小费用最大流】
思路: 把房子和人看成点,加上源点和汇点. 源点和每个人连容量为1,权值为0的边. 每个人和每个房子连容量为1,权值为距离的边. 每个房子和汇点连容量为1,权值为0的边. #include<st ...
- Js获取当前日期时间及时间相关操作
Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- Temporary Segments: What Happens When a Sort Occurs (文档 ID 102339.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.7.4 to 11.2.0.1 [Release 8.1.7 to 11. ...
- 在iis中注册.net framework
首先定位到文件夹:cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 执行命令:aspnet_regiis.exe -i
- Centos安装软件小结-20160325
三种安装包 bin包 rpm包 源码包 1.bin包 1.先赋予权限: chmod 777 *.bin 2.开始安装: ./.bin 2.rpm包(以jdk为例)\ yum search jdk\ y ...
- Redis附加功能之Redis流水线pipeline
流水线功能的目的:通过减少客户端与服务器之间的通信次数来提高程序的执行效率. 一.通信 在一般情况下, 用户每执行一个 Redis 命令,客户端与服务器都需要进行一次通信:客户端会将命令请求发送给服务 ...
- jmeter做接口测试
jmeter做接口测试有两种方式: 1. 2.
- c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)
//c语言描述 将2进制转化为10进制 #include <stdio.h> #include <stdlib.h> #include <math.h> #defi ...