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 ...
随机推荐
- 如何用eclipse运行导入的maven项目
1.配置jdk系统环境变量.找到安装的jdk的安装目录,新建系统环境变量,变量名为JAVA_HOME(作为一个引用),变量值为该路径. 找到Path,将%JAVA_HOME%/bin; 添加到变量值的 ...
- java+Mysql大数据的一些优化技巧
众所周知,java在处理数据量比较大的时候,加载到内存必然会导致内存溢出,而在一些数据处理中我们不得不去处理海量数据,在做数据处理中,我们常见的手段是分解,压缩,并行,临时文件等方法; 例如,我们要将 ...
- Alarm机制用于定时服务
获取一个 AlarmManager 的实例: AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE) ...
- windows下的命令
1.cd 现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令. cd /d d:/git/springTest 2.chdir 显示当前目录名或改变当前目录. CHDIR [/D] ...
- excel VBA 将文本数值转换为数字格式(单元格中数据左上角是绿三角,鼠标点上有叹号标示)
Range("A6").SelectSelection.CopyRange("A10:A60").SelectRange(Selection, Selectio ...
- Go语言之并发编程(三)
Telnet回音服务器 Telnet协议是TCP/IP协议族中的一种.它允许用户(Telnet客户端)通过一个协商过程与一个远程设备进行通信.本例将使用一部分Telnet协议与服务器进行通信. 服务器 ...
- axure rp教程(四)动态面板滑动效果
转载自: http://www.iaxure.com/74.html 实现目标: 1. 点击登录滑出登录面板 2. 点击确定滑出动态面板 最终效果如下: 这种效果可以通过两种方法实现: 首先准备需 ...
- Git之2分钟教程
Git之2分钟入门 普通人:“借我1000块钱”.程序猿:“借你1024吧,凑个整”. 今天是1024,是我们程序员的节日,在此,首先祝各位程序猿以及程序媛们节日快乐~然后送出一份节日礼物,没错,就是 ...
- 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- jmeter配置分布式调度:远程启动其他机器实现多台pc一起并发
原文转自:https://www.cnblogs.com/whitewasher/p/6946207.html Jmeter分布式部署测试-----远程连接多台电脑做压力性能测试 在使用Jmeter进 ...