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 ...
随机推荐
- Codeforces Round #464 (Div. 2) D. Love Rescue
D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...
- Codeforces Round #460 (Div. 2)-C. Seat Arrangements
C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- SSH无密码登录及远程拷贝命令SCP的使用
SSH无密码登录 1.生成密钥对(公钥和私钥) $ cd /home/cen/.ssh $ ssh-keygen -t rsa #生成密钥,使用rsa方式进行加密,四个回车 $ ssh-copy-id ...
- Appium环境搭建及“fn must be a function”问题解决
由于appium在线安装比较困难,大多数应该是由于FQ造成的吧,索性直接下载appium安装包:http://pan.baidu.com/s/1bpfrvjD nodejs下载也很缓慢,现提供node ...
- 13,scrapy框架的日志等级和请求传参
今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy ...
- MD5碰撞
if ( $_POST['param1'] !==$_POST['param2'] && md5($_POST['param1']) === md5($_POST['param2']) ...
- Python+Selenium框架设计篇之-什么是自动化测试框架
1.什么是自动化测试框架 简单来说,自动化测试框架就是由一些标准,协议,规则组成,提供脚本运行的环境.自动化测试框架能够提供很多便利给用户高效完成一些事情,例如,结构清晰开发脚本,多种方式.平台执行脚 ...
- Python+Selenium中级篇之-二次封装Selenium中几个方法
本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法.我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类 ...
- CSU-2049 象棋
CSU-2049 象棋 Description 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子.一天,小度在棋盘上摆起了许多車--他想知道,在一共N×M个点的矩形棋盘中摆最多 ...
- Linux抓包工具tcpdump命令详解
1.简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具. tcpdump可以将网络中 ...