Java [leetcode 12] Integer to Roman
题目描述:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解题思路:
这道题没什么技术含量,一个一个除10以及取模运算就行。
代码如下:
public class Solution {
public String intToRoman(int num) {
String Roman[][] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};
int remainder;
int carry = num;
int index = 0;
String result = new String();
while(carry != 0){
remainder = carry % 10;
result = Roman[index][remainder] + result;
index++;
carry = carry / 10;
}
return result;
}
}
Java [leetcode 12] Integer to Roman的更多相关文章
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode——12. Integer to Roman
一.题目链接:https://leetcode.com/problems/integer-to-roman/ 二.题目大意: 给定一个整数,返回它的罗马数字的形式. 三.题解: 要想做出这道题目,首先 ...
- LeetCode 12 Integer to Roman (整数转罗马数字)
题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ...
- [leetcode] 12. Integer to Roman
关于罗马数字: I: 1V: 5X: 10L: 50C: 100D: 500M: 1000字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:I: 1, II: 2, III: 3, ...
随机推荐
- 程序使用嵌套的for循环找出2〜100中的素数
#import <Foundation/Foundation.h> int main () { /* local variable definition */ int i, j; ; i& ...
- eclipse里maven项目An error occurred while filtering resources解决办法(转载)
转自:http://liyanjie918.blog.163.com/blog/static/20227290201581143110105/ 在使用eclipse构建maven项目时,突然出现错误提 ...
- java和php实现RSA加密互通-b
java和PHP RSA加密实现互通 1:通过openssl 生成公钥和密钥文件(linux) (1) 生产私钥文件命令 openssl genrsa -out rsa_private_key.pe ...
- 强连通分量Tarjan模板
#include<iostream> #include<stdio.h> #include<string.h> #include<stack> #inc ...
- iOS sqlite 增删改查 简单封装(基于 FMDB)
/** * 对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * * 基于 FMDB * * 操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...
- 简介Java反射基础
[参考资料: 疯狂Java讲义 Chapter 18] 1.类加载.连接.初始化 当Java程序需要某一个类时,如果该类尚未加载到内存中,系统会通过加载.连接.初始化三个步骤将该类加载到内存,并完成初 ...
- python语法-[with来自动释放对象]
python语法-[with来自动释放对象] http://www.cnblogs.com/itech/archive/2011/01/13/1934779.html 一 with python中的w ...
- 1989-C. 数字三角形
描述 如图所示,是一个数字搭成的三角形. 若起始位置在三角形的顶端,结束位置在三角形底边,每一步只能向下方或向右下角移动一格.请编程计算一条路径,使得路径上经过的数字和最大.(图中路径7→3→8→7→ ...
- FreeMarker 乱码解决方案 生成静态html文件
读取模板的时候有一个编码: Template template = this.tempConfiguration.getTemplate(templatePath,"UTF-8") ...
- jquery的ajax向后台servlet传递json类型的多维数组
后台运行结果: 前台运行结果: ...