LeetCode OJ-- Integer to Roman @
@表示有更优解法
https://oj.leetcode.com/problems/integer-to-roman/
将自然数,转换成罗马数字,输入范围为 1 ~ 3999,因为罗马数没有0.
用一个map,表示 1 ~ 9, 10 ~ 90, 100 ~ 900, 1000 ~ 3000,然后,把相应的罗马字母存起来。
之后,求出输入有几个 1000 ,几个 100 ,几个1来。
#include <iostream>
#include <map>
#include <string>
using namespace std; class Solution { public:
string intToRoman(int num) {
map<int,string> I2R;
I2R.insert(make_pair( ,"I"));
I2R.insert(make_pair( ,"II"));
I2R.insert(make_pair( ,"III"));
I2R.insert(make_pair( ,"IV"));
I2R.insert(make_pair( ,"V"));
I2R.insert(make_pair( ,"VI"));
I2R.insert(make_pair( ,"VII"));
I2R.insert(make_pair( ,"VIII"));
I2R.insert(make_pair( ,"IX"));
I2R.insert(make_pair( ,"X"));
I2R.insert(make_pair( ,"XX"));
I2R.insert(make_pair( ,"XXX"));
I2R.insert(make_pair( ,"XL"));
I2R.insert(make_pair( ,"L"));
I2R.insert(make_pair( ,"LX"));
I2R.insert(make_pair( ,"LXX"));
I2R.insert(make_pair( ,"LXXX"));
I2R.insert(make_pair( ,"XC"));
I2R.insert(make_pair( ,"C"));
I2R.insert(make_pair( ,"CC"));
I2R.insert(make_pair( ,"CCC"));
I2R.insert(make_pair( ,"CD"));
I2R.insert(make_pair( ,"D"));
I2R.insert(make_pair( ,"DC"));
I2R.insert(make_pair( ,"DCC"));
I2R.insert(make_pair( ,"DCCC"));
I2R.insert(make_pair( ,"CM"));
I2R.insert(make_pair( ,"M"));
I2R.insert(make_pair( ,"MM"));
I2R.insert(make_pair( ,"MMM")); string ans; int this_time = ;
for(int jinzhi = ; jinzhi > ; jinzhi = jinzhi / )
{
this_time = num / jinzhi;
this_time = this_time * jinzhi;
if(this_time > )
ans = ans + I2R[this_time];
num = num % jinzhi;
} return ans;
}
};
更优解法,来自九章。
/**
* Copyright: NineChapter
* - Algorithm Course, Mock Interview, Interview Questions
* - More details on: http://www.ninechapter.com/
*/ public class Solution {
public String intToRoman(int num) {
if(num <= ) {
return "";
}
int[] nums = {, , , , , , , , , , , , };
String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder res = new StringBuilder();
int digit=;
while (num > ) {
int times = num / nums[digit];
num -= nums[digit] * times;
for ( ; times > ; times--) {
res.append(symbols[digit]);
}
digit++;
}
return res.toString();
}
}
LeetCode OJ-- Integer to Roman @的更多相关文章
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 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 ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- leetcode:Integer to Roman(整数转化为罗马数字)
Question: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the rang ...
- Java [leetcode 12] Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
随机推荐
- Volatile小结
1)Java 中能创建 Volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组,将会受到 vo ...
- Neon Lights in Hong Kong【香港霓虹灯】
Neon Lights in Hong Kong Neon is to Hong Kong as red phone booths are to London and fog is to San Fr ...
- RGB色彩的计算机表示
计算机显示模式[编辑] 24比特模式[编辑] 每像素24位(比特s per pixel,bpp)编码的RGB值:使用三个8位无符号整数(0到255)表示红色.绿色和蓝色的强度.这是当前主流的标准表示方 ...
- 51NOD:1639-绑鞋带
传送门:https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=475129 1639 绑鞋带 基准时间限制:1 秒 空间限制:131 ...
- python-PIL模块的使用
PIL基本功能介绍 from PIL import Image from PIL import ImageEnhance img = Image.open(r'E:\img\f1.png') img. ...
- 图说不为人知的IT传奇故事-5-小型机之王
此系列文章为“图说不为人知的IT传奇故事”,各位大忙人可以在一分钟甚至几秒内了解把握整个内容,真可谓“大忙人的福利”呀!!希望各位IT界的朋友在钻研技术的同时,也能在文学.历史上有所把握.了解这些故事 ...
- Marketing learning-1
Today we start to learn something about marketing together.Sometimes i just propose a question,and i ...
- [转]jQuery DOM Ready
一直以来,各种JS最佳实践都会告诉我们,将JS放在HTML的最后,即</body>之前,理由就是:JS会阻塞下载,而且,在JS中很有可能有对DOM的操作,放在HTML的最后,可以尽可能的保 ...
- MYSQL学习心得(转)
适合有SQL SERVER或ORACLE基础的人看,有对比,学习更有效果 转自:http://www.cnblogs.com/lyhabc/ 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习 ...
- Map的常用方法keySet()、entrySet()
Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中key值的集合:en ...