【Roman To Integer】cpp

题目:

Given a roman numeral, convert it to an integer.

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

代码:

class Solution {
public:
    int romanToInt(string s) {
        const int size = 7;
        std::map<char, int> symbol_value;
        char symbol_ori[size] = {‘M‘,‘D‘,‘C‘,‘L‘,‘X‘,‘V‘,‘I‘};
        int value_ori[size] = {1000,500,100,50,10,5,1};
        for ( size_t i = 0; i < size; ++i )
            symbol_value[symbol_ori[i]]=value_ori[i];

        int result = symbol_value[s[0]];
        for ( size_t i = 1; i < s.size(); ++i )
        {
            if ( symbol_value[s[i]] > symbol_value[s[i-1]] )
            {
                result += symbol_value[s[i]] - 2*symbol_value[s[i-1]];
            }
            else
            {
                result += symbol_value[s[i]];
            }
        }
        return result;
    }
};

tips:

根据Roman数字的构造规则:

如果当前的symbol比前一个symbol大,则当前symbol的值减去二倍之前的那个值,再累加到result中。

(为什么要减2倍,因为这个值之前已经被累加一遍了,所以减去2倍就正好了)

时间: 2024-11-05 22:55:17

【Roman To Integer】cpp的相关文章

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector

【Reverse Integer】cpp

题目: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought th

【Power of Two】cpp

题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { public: bool isPowerOfTwo(int n) { if ( n<=0 ) return false; int countOne = 0; for ( int i=0; i<sizeof(n)*8 && countOne<=1; ++i ) { countOne

【Spiral Matrix II】cpp

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 代码: class Solution { public: vector<vector<

【First Missing Positive】cpp

题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 代码: class Solution { public: int firstMissingPos

【Count and Say】cpp

题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211. Given

【Merge Sorted Array】cpp

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f