[leetcode] 12. Integer to Roman
关于罗马数字:
I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000
字母可以重复,但不超过三次,当需要超过三次时,用与下一位的组合表示:
I: 1, II: 2, III: 3, IV: 4
C: 100, CC: 200, CCC: 300, CD: 400
提取每一位digit,然后convert to 罗马数字
public class Solution {
private static char[][] chars = {{'I', 'V'}, {'X', 'L'}, {'C', 'D'}, {'M', 'O'}};
public String intToRoman(int num) {
int copy_num = num;
int count = 0;
StringBuilder result = new StringBuilder();
while (copy_num != 0) {
int x = copy_num / 10;
int digit = copy_num - x * 10;
switch (digit) {
case 3: result.append(chars[count][0]);
case 2: result.append(chars[count][0]);
case 1: result.append(chars[count][0]);
case 0: break;
case 4: result.append(chars[count][1]); result.append(chars[count][0]); break;
case 8: result.append(chars[count][0]);
case 7: result.append(chars[count][0]);
case 6: result.append(chars[count][0]);
case 5: result.append(chars[count][1]); break;
case 9: result.append(chars[count + 1][0]); result.append(chars[count][0]); break;
}
count++;
copy_num = x;
}
result = result.reverse();
return result.toString();
}
}
[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 ☆☆
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 ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- [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[] = {"", ...
随机推荐
- 【BZOJ-4082】Surveillance 树链剖分 LCA + 贪心
4082: [Wf2014]Surveillance Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 260 Solved: 100[Submit][ ...
- Java内存与垃圾回收调优
Java(JVM)内存模型 正如你从上面的图片看到的,JVM内存被分成多个独立的部分.广泛地说,JVM堆内存被分为两部分——年轻代(Young Generation)和老年代(Old Generat ...
- awk删除文件第一列
awk删除文件第一列 1.采用awk awk '{$1="";print $0}' file 2.采用sed sed -e 's/[^]* //' file sort -R fil ...
- C++内存动态分配
https://www.percona.com/blog/2012/07/05/impact-of-memory-allocators-on-mysql-performance/ https://su ...
- 【09-03】java泛型学习笔记
静态方法的泛型 /** * @description 静态方法的泛型无法使用类定义的泛型,因为类在实例化时才确定具体的泛型类,因此静态方法要使用泛型需要使用泛型方法的方式 */ public clas ...
- JSP分页显示
首先要定义四个变量: int pageSize: //每页显示多少条记录 int pageNow: //希望显示第几页 int pageCount: //一共有多少页 int rowCount: // ...
- jsp动作元素之forward指令
forward指令用于将页面响应转发到另外的页面.既可以转发到静态的HTML页面,也可以转发到动态的JSP页面,或者转发到容器中的Servlet. forward指令格式如下: <jsp:for ...
- Xshell个性化设置,解决Xshell遇到中文显示乱码的问题
在同事的推荐下,今天开始使用Xshell连接Linux,但是发现一个“遇到中文显示乱码”的问题, 同事的解决方案如下: 平常给Linux上传文件之前,先把文件转换成UTF-8编码形式, 然后设置Xsh ...
- JavaScript操作JSON的方法总结,JSON字符串转换为JSON对象
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...
- python %s深入解析
默认我们通常用字符串填充它 'Keep %s, and you will aways make %' % ('moving', 'it') 如果你就此止步,那就错过了一些神乎其技的用法 比如: arr ...