[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[] = {"", ...
随机推荐
- Python基础7:文件操作
[ 文件操作] 1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧 ...
- IBatisNet使用教程
1.是数据持久层,对应.NET方向的有Ibatis.NET,只要用来处理数据库表结构和程序实体之间映射,ado.net是用来处理和数据库直接通信的,取出数据(object,int,string,da ...
- 分析移动端APP的网络请求抓包
为了方便,本文以 iOS 系统来进行演示. 使用代理 移动操作系统中都有可以设定系统代理的设置,比如在 iOS 中可以通过 Settings->WLAN 看到很多 Networks,通过点击它们 ...
- Jedis测试redis
首先:Jedis是redis的java版本的客户端. public class JedisTest { //单机版测试Jedis,不使用连接池 @Test public void testJedis( ...
- .Net 中的反射(反射特性) - Part.3
反射特性(Attribute) 可能很多人还不了解特性,所以我们先了解一下什么是特性.想想看如果有一个消息系统,它存在这样一个方法,用来将一则短消息发送给某人: // title: 标题:author ...
- 11月7日下午PHP----PDO访问方式操作数据库
MySQLI是专门访问MySQL数据库的,不能访问其它数据库.PDO可以访问多种的数据库,它把操作类合并在一起,做成一个数据访问抽象层,这个抽象层就是PDO,根据类操作对应的数据库.mysqli是一个 ...
- 使用MicroService4Net 快速创建一个简单的微服务
“微服务架构(Microservice Architecture)”一词在过去几年里广泛的传播,它用于描述一种设计应用程序的特别方式,作为一套独立可部署的服务.目前,这种架构方式还没有准确的定义,但是 ...
- C# 异步
private void GetHttpResponse() { var client = new Microsoft.HBase.Client.HBaseClient(new ClusterCred ...
- Pandas-数据探索
Pandas包对数据的常用探索功能,方便了解数据描述性属性. 目录 基础属性 shape indexs columns values dtype/dtypes 汇总和计算描述统计 count() va ...
- vijos1404 遭遇战
描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区的SQC论坛服务器!我们SQC的人誓死不屈,即将于恐怖分子展开激战,准备让一个人守着A区,这样恐怖分子就不能炸掉服务器了.(一 ...