【Integer To Roman】cpp
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
代码:
class Solution {
public:
string intToRoman(int num) {
if ( num< ) return NULL;
const int size = ;
std::string symbol_ori[size] = {"M","D","C","L","X","V","I"};
int value_ori[size] = {,,,,,,};
// gain extra symbol and value pair
std::vector<std::string> symbol_extra;
std::vector<int> value_extra;
for ( int i = ; i < size-; ++i ){
symbol_extra.push_back(symbol_ori[i]);
value_extra.push_back(value_ori[i]);
if ( !(i & ) ){
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
else{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
}
symbol_extra.push_back(symbol_ori[size-]);
value_extra.push_back(value_ori[size-]);
std::string result;
for ( size_t i = ; i < symbol_extra.size(); ++i )
{
int k = num / value_extra[i];
while ( k )
{
result += symbol_extra[i];
k--;
}
num = num % value_extra[i];
}
return result;
}
};
tips:
根据罗马数字进位的特殊规则,预先补上不能正常进位的symbol和value,这样代码可以非常consice
================================================
第二次过这道题,重写一遍加深印象。
class Solution {
public:
string intToRoman(int num) {
if ( num< ) return NULL;
const int size = ;
string symbol_ori[size] = {"M","D","C","L","X","V","I"};
int value_ori[size] = {,,,,,,};
vector<string> symbol_extra;
vector<int> value_extra;
for ( int i=; i<size-; ++i ){
symbol_extra.push_back(symbol_ori[i]);
value_extra.push_back(value_ori[i]);
if ( !( & i) )
{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
else
{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
}
symbol_extra.push_back(symbol_ori[size-]);
value_extra.push_back(value_ori[size-]);
string ret = "";
for ( int i=; i<value_extra.size(); ++i )
{
int count = num / value_extra[i];
while ( count--> ) ret = ret + symbol_extra[i];
num = num % value_extra[i];
}
return ret;
}
};
【Integer To Roman】cpp的更多相关文章
- LeetCodeOJ刷题之12【Integer to Roman】
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【Count and Say】cpp
题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Power of Two】cpp
题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { pu ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
随机推荐
- 自动计算尺寸列表功能案例ios源码
源码HTKDynamicResizingCell,HTKDynamicResizingCell提供自动计算尺寸的TableViewCell/CollectionViewCel,只要设置了合适AutoL ...
- 用AsyncTask 来实现下载图片在android开发中
Android使用AsyncTask 有如下好处: 1. 线程的开销较大,如果每个任务都要创建一个线程,那么应用程序的效率要低很多: 2. 线程无法管理,匿名线程创建并启动后就不受程序的控制了,如果有 ...
- vim编辑器编程配置
打开/etc/vim/vimrc 添加命令: set cindent "使用C样式的缩进 syntax on "语法高亮 set tabstop=4 set softtabs ...
- NotePad++相关设置
Notepad++去掉红色下划线: 插件->DSpellCheck->Auto-check Document 前面的勾去掉 Notepad++自动换行: 视图(View)——>自动换 ...
- 日期转换(用DateTime的ParseExact方法解析特殊的日期时间)
今天遇到一个特别的需求,需要从下面的字符串中转换成一个DateTime对象: [07-13 15:50:42] 主要问题是这个时间不是标准的时间,而是自定义的格式,即开头是月-日,然后是时间. 使用最 ...
- c#操作Zip压缩文件
SharpZipLib 文件/文件夹压缩 一.ZipFile ZipFile类用于选择文件或文件夹进行压缩生成压缩包. 常用属性: 属性 说明 Count 文件数目(注意是在ComitUpdat之后才 ...
- 关于Raw,Assets的使用
Raw,Assets下文件区别: 相同点:两个目录下的文件在打包后都会原封不动的保存到apk中,不会被编译成二进制. 不同点:Raw下文件不能使用目录结构, 有些格式的会被压缩,能够通过R.raw方便 ...
- 用VBA计算WPS 表格ET EXCEL中的行数和列数的多重方法
用VBA计算WPS 表格ET EXCEL中的行数和列数 每种方法中上面的是Excel的行数,下面的是Excel的列数. 方法1: ActiveSheet.UsedRange.Rows.Count Ac ...
- C# 基础 计算平均值的方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Sorl之.net操作
http://www.cnblogs.com/zhangweizhong/category/771055.html 插入: SolrNet.Startup.Init<Movie>(&quo ...