[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[] = {"", ...
随机推荐
- 搭建vpn环境:centos7+openvpn
vpn的含义:virtual private network vpn的作用/使用场景:最常见的一个作用,你通过公网来访问某个局域网里的主机/服务,其实就是搭建一个隧道,用公网传递你的数据包,等数据包到 ...
- java 随机数 优惠码 生成 随机字串
package test; import java.util.HashSet; import java.util.Random; import java.util.Set; public class ...
- HTTP协议 -- 认清协议常用状态码
HTTP协议作为web服务的基础,理所应当受到重视,但是周围的同事能够讲清楚HTTP协议的凤毛麟角.既然是基础,就应该早一点掌握,所以近半年(2016-2月——2016年6月),不准备学习新技术了.首 ...
- poj 2724 Purifying Machinef
poj 2724 Purifying Machinef 题意 每一个01串中最多含有一个'*','*'既可表示0也可表示1,给出一些等长的这样的01串,问最少能用多少个这样的串表示出这些串.如:000 ...
- Jquery动态添加的元素绑定事件的3种方法
假设我们点击li标签,弹出他的文本,如果是动态添加的li,点击是没有效果的,压根弹不出来文本. 下面博主分享一下为动态添加的元素绑定事件的三种方法,网上一般都是两种,我在这里多增加了一种. 事件案例: ...
- animate对颜色设置不起作用
今天了解了一下stop的使用方法,但是实例中加入color:red的时候,动画效果没有实现,具体实例如下: http://jsbin.com/fezaroyene/edit?html,js,outpu ...
- SaaS模式的软件
SaaS是Software-as-a-Service(软件即服务)的简称,随着互联网技术的发展和应用软件的成熟, 在21世纪开始兴起的一种完全创新的软件应用模式.它与"on-demand ...
- Hive时间操作[转]
时间字段格式化 from_unixtime(unix_timestamp(VisitTime),'yyyy-MM-dd') 日期函数UNIX时间戳转日期函数: from_unixtime语法: f ...
- Android 四大组件之Service
---恢复内容开始--- 1,Service的生命周期
- Swift 3.0 【Swift 3.0 相较于 Swift 2.2 的变化】
一.编译器和语法变化 函数或方法参数 调用函数或方法时从第一个参数开始就必须指定参数名 在Swift的历史版本中出现过在调用函数时不需要指定任何函数参数(或者从第二个参数开始指定参数名),在调用方法时 ...