leetcode_13_Roman to Integer (easy)

Roman to Integer

题目:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

解题:

首先我们先观察罗马数字的规律

罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。按照下面的规则可以表示任意正整数。
重复数次:一个罗马数字重复几次,就表示这个数的几倍。
右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字。但是,左减不能跨越等级。比如,99不可以用IC表示,用XCIX表示。
加线乘千:在一个罗马数字的上方加上一条横线或者在右下方写M,表示将这个数字乘以1000,即是原数的1000倍。同理,如果上方有两条横线,即是原数的1000000倍。
单位限制:同样单位只能出现3次,如40不能表示为XXXX,而要表示为XL。

MMMCMXCIX,3999

知道了这些,接下来就可以解题了

#include <iostream>

using namespace std;

class Solution {

public:

int romanToInt(string s) {

int a[7]  = {1,5,10,50,100,500,1000};

string p[7] = {"I","V","X","L","C","D","M"};

if(s=="")return 0;

for(int i=0;i<7;i++){

if(s==p[i]){

return a[i];

}

}

char r[7] = {‘I‘,‘V‘,‘X‘,‘L‘,‘C‘,‘D‘,‘M‘};

//I(1),V(5),X(10),L(50),C(100),D(500),M(1000)

int final=0,start=0;

int result = 0;

//截取字符串s2 = s1.substr(0,3);起始位置和截取的长度

int i;

for(i = 7;i>=0;i--){

int flag = 0;

for(int j=0;j<s.length();j++){

if(s.at(j)==r[i]&&flag == 0)//比较自负相等

{

final = j-1;//注意final可能为负数

flag = 1;//跟踪

}

if(flag==1){

if(s.at(j)!=r[i]) break;

start = j+1;//start 可能超出范围

}

}

if(start!=final)

break;

}

unsigned long aLength=0,bLength=0;

aLength = final<0 ?  0:final+1;

bLength = start>s.length()-1  ? 0:s.length()-start;

result = -romanToInt(s.substr(0,aLength))+romanToInt(s.substr(start,bLength))+(start-final-1)*a[i];

return result;

}

};

int main(int argc, const char * argv[]) {

Solution a;

cout<<a.romanToInt("MMMCMXCIX")<<endl;

return 0;

}

最后accepted

哈哈

时间: 2024-12-29 00:46:59

leetcode_13_Roman to Integer (easy)的相关文章

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 013.Roman_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/roman-to-integer/ 代码(github):https://github.com/illuz/leetcode 题意: 把罗马数转为十进制. 分析: 跟 012. I

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

13 Roman to Integer (easy)

Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 简单介绍一下罗马数字,摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算

7. Reverse Integer - Easy

Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only store integers within

LeetCode8 String to Integer (atoi)

题意: Implement atoi to convert a string to an integer.  (Easy) 分析: 就是注意各种特殊情况,边界情况的判断,见代码注释. 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 int start = 0; 5 long long result = 0; 6 int flag = 0; 7 //开头多余空格的处理 8 while (str[start] == ' ') { 9

Leetcode 7. Reverse Integer(水)

7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could on

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i

leetcode算法思想快速一览

最近在看八股文.整理了一下思路,想深入了解还得多去写,无奈时间紧迫的情况下抛砖引玉也不失为下策: 1.Two Sum Easy 给出一个数组,找出其中两个和为目标值的坐标.思路: [1]排序. 和为目标值,一般的思路是先排序,然后取两点坐标分别从首尾向中间移动.若和为目标值则返回两点坐标.若和大于目标值,右端坐标值-1,反之左端坐标值+1:[2]因为需要返回坐标值.需要对键值对进行按值排序.以保留原始坐标. 2.Add Two Numbers Medium有两个反向存储数字的链表,求他们的和.思