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 ...
随机推荐
- 第1章 VMware中安装CentOS7
目录 1.1 下载CentOS7安装包 1.2 VMware中新建虚拟机 1.3 安装操作系统 本章讲解在VMware中安装CentOS虚拟机的步骤.使用的VMware Workstation版本为1 ...
- POJ1426-Find The Multiple(搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42035 Accepted: 176 ...
- Codeforces Round #462 (Div. 2) D. A Determined Cleanup
D. A Determined Cleanup time limit per test1 second memory limit per test256 megabytes Problem Descr ...
- 动态规划:HDU1059-Dividing(多重背包问题的二进制优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- [Uva1642]魔法Gcd(数论)
Description 给定n个数,某个连续区间[L,R]的收益为\(gcd(A_l,A_{l+1},A_{l+2}...A_r)*(r-l+1)\), 求收益最大的区间的收益值 \(1 \leq n ...
- Leetcode 106. 从中序与后序遍历序列构造二叉树
题目链接 https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/descri ...
- HDU 3896 Greatest TC 双连通分量
题意 给一个连通的无向图,有两种询问: \(a, b, c, d\),问如果删掉\(c,d\)之间的边,\(a,b\)之间是否还连通 \(a, b, c\),问如果删掉顶点\(c\),\(a,b\)之 ...
- tensorboard页面显示No dashboards are active for current data set 问题win10系统
如果问题如上所示,可以试下如下方法: 在文件夹中找到你的logs文件, 在空白处按住“shift”键,右键鼠标(注意鼠标不要选中任何文件),点击“Powershell”打开win10powershel ...
- WebApp开发技巧
http://www.cnblogs.com/WhiteCusp/p/4502961.html http://ju.outofmemory.cn/entry/25675 http://www.fron ...
- Careercup - Microsoft面试题 - 24313662
2014-05-12 07:27 题目链接 原题: Convert a number to a number 题目:把二进制数转化成四进制数. 解法:四是二的倍数,所以两位变一位就可以了. 代码: / ...