【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 ...
随机推荐
- CentOS学习笔记--MySQL安装
MySQL安装 Linux中使用最广泛的数据库就是MySQL,使用在线yum的方式安装的版本落后MySQL网站好几个小版本,本节亲自测试安装新版的MySQL. 测试机器环境: VMware Works ...
- div+css+jQuery图片横向滚动代码(带左右点击按钮)
首先感谢Blue老师的javascript教程,给了我很多的启发,这是我在看完10 - 定时器的使用 - 2这节视频后,自己试着用jQuery重新改写了一下代码,感觉至少比百度搜出来的那一坨靠谱多了, ...
- Linux Shell脚本编程的注意事项
Linux下(Shell脚本 http://www.jbxue.com/jb/shell/)编程的一些注意事项,如编程风格.命名风格等. 一.常用技巧 ssh user@server bash < ...
- centos6.7下网络设置
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static" # ...
- Delphi For Android 开发笔记-附:如何Delphi中同时实现Windows、Android版的GetModuleFileName函数
在Windows中开发DLL时,经常会需要获取当前DLL所在目录以便读取同目录下的其他文件,而目前Delphi在开发android时,其实没多大必要获取,因为整个工程只有一个so文件,而这个so文件也 ...
- delphi 基础之二 面向对象概念初步
面向对象概念初步 •类自动生成 快捷键:ctrl+shift+c 1.类的定义 类是用户创建的数据类型,包括状态.表达式和一些操作.有3个组成部分,即字段.方法和属性.字段是类的内部数据变量,方法就是 ...
- C# 委托简单使用方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Generate List and Table via ng-repeat
<div ng-app ng-controller='StudentListController'> <ul> <li ng-repeat='student in stu ...
- java 中 java.lang.ArrayIndexOutOfBoundsException: 0 异常
package test; public class Test { public static void main(String[] args) { final int num2 = Integer. ...
- java学习资源汇总
http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm