[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[] = {"", ...
随机推荐
- 为什么npm install在安装时会多安装很多依赖包
比如我安装gulp时,会多出很多无用的包,如下图: 经过查询,原来是npm升级了导致的,在npm3.0以上的版本,包的依赖不再安装在每个架包的node_modules文件夹内,而是安装在顶层的node ...
- ubuntu 下简单录音
找了半天录音工具,甚至都在尝试用 pyAudio 自己写了,结果发现,原来有现成命令行工具用! 就是 sox 工具包.这个工具包有 4 个工具:sox, play, rec, soxi.rec 和 p ...
- Unity 难点目录
1.mesh的任意切割.(难点) 2.扇形区域识别玩家.(解决) 3.NGUI横滑同时竖滑,或滑动同时点击冲突处理.(解决)
- HTTP协议—— 简单认识TCP/IP协议
大学没读计算机专业,所以很多的专业知识都不知道.既然已经从事了IT这个行业,就势必要去了解下网络底层,虽然实际工作中这些东西用不到.高楼大厦,起于平川.不积跬步,无以至千里,不积小流,无以成江海.我现 ...
- BZOJ 4569 萌萌哒
题目传送门 4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 483 Solved: 221 [Submit][S ...
- ngBind ngBindTemplate ngBindHtml
ng-bind: 只能绑定一个变量 在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{titleStr}}</p> 另一种是使用基于属 ...
- Angular.js实现折叠按钮的经典指令.
var expanderModule=angular.module('expanderModule',[]) expanderModule.directive('expander',function( ...
- Migrating an Existing Website from SQL Membership to ASP.NET Identity
Migrating an Existing Website from SQL Membership to ASP.NET Identity public class User : IdentityUs ...
- oracle 11g express 快速入门
创建表空间CREATE TABLESPACE testdb LOGGING DATAFILE 'F:\oracle\app\oracle\oradata\XE\testdb.dbf' SIZE 100 ...
- HTML5视频播放
1.检测浏览器是否支持html5视频播放 <!doctype html> <html> <head> <meta charset="utf-8&qu ...