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 ...
随机推荐
- day 71 Django基础六之ORM中的锁和事务
Django基础六之ORM中的锁和事务 本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update(no ...
- 网络编程协议(TCP和UDP协议,粘包问题)以及socketserver模块
网络编程协议 1.osi七层模型 应用层 表示层 会话层 传输层 网络层 数据链路层 物理层 2.套接字 socket 有两类,一种基于文件类型,一种基于网络类型 3.Tcp和udp协议 ...
- bs4的简单应用之防止xss攻击和文本截断
BeautifulSoup可以过滤html标签,根据这个功能我们可以防止xss攻击和进行文本过滤 1. 安装 pip install beautifulsoup4 2.导入.使用 from bs4 i ...
- 2017 ACM-ICPC EC-Final ShangHai(思维乱搞赛)
感觉全是思维乱搞题. Gym - 101775J Straight Master 给你n种扑克,你每次可以出连续的3 ~ 5 张,问你能否出完. Sample Input 2 13 1 2 2 1 0 ...
- Python-S9-Day124-爬虫&微信
01 今日内容概要 02 内容回顾:flask上下文 03 内容回顾:多app应用 04 内容回顾:面向对象和数据库 05 内容回顾:爬虫 06 Web微信:获取二维码(一) 07 Web微信:获取二 ...
- 【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, ...
- Leetcode 516.最长回文子序列
最长回文子序列 给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 示例 1:输入: "bbbab" 输出: 4 一个可能的最长回文子序列为 " ...
- 【两种方式 Service References和 web References 】手把手教你引入webservice 服务
1.对于一个webservie服务我们如何引入到自己的项目中去呢 第一种方法[Service References]:鼠标移到属性上 右键添加服务引用 然后在地址栏输入webservice 地址 点击 ...
- POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。
POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbe ...
- angular2多组件通信流程图
知识点1:组件属性的双向绑定,需要在属性+Change 作为Output方法返回即可. 知识点2:更新子组件的值,不会引起output触发,父组件不会更新绑定的值 知识点3:属性的双向绑定,只会子组件 ...