【LeetCode从零单刷】Roman to Integer

题目:

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)。
  • 右加左減: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數字加小數字。 在較大的羅馬數字的左邊記上較小的羅馬數字,表示大數字减小數字。
  • 之后的事情就是查表,并且不考虑进制问题,一位一位的计数

    比较与右侧字母的大小关系,如果大于等于右侧字母则总和加上这个字母;否则减去这个字母。

    class Solution {
    public:
        int romanToInt(string s) {
            std::map<char, int>table;
            table['I'] = 1;
            table['V'] = 5;
            table['X'] = 10;
            table['L'] = 50;
            table['C'] = 100;
            table['D'] = 500;
            table['M'] = 1000;
    
            int len = s.size();
            int ans = 0;
            for(int i = 0; i<len - 1; i++)
            {
                if(table[s[i]] < table[s[i+1]])
                    ans = ans - table[s[i]];
                else
                    ans = ans + table[s[i]];
            }
            ans += table[s[len - 1]];
    
            return ans;
        }
    };
    时间: 2024-08-24 00:34:14

    【LeetCode从零单刷】Roman to Integer的相关文章

    【LeetCode从零单刷】Binary Tree Inorder Traversal

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

    【LeetCode从零单刷】Integer to Roman

    题目: 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 thousand[4] = {"", "M", "MM", &

    【LeetCode从零单刷】Maximum Depth of Binary Tree

    没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given a binary tree, find its maximum depth.  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高

    【LeetCode从零单刷】Same Tree

    没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解答: 验证两棵树是否相等.使

    leetcode第13题--Roman to Integer

    Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数.其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000} class Solution { public: int romanToInt(string s)

    LeetCode记录之13——Roman to Integer

    能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 给定一个罗马数字,将其转换为整数. 输入保证在1到3999之间. 1 class Solution { 2 public int romanToInt(

    【LeetCode从零单刷】Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

    【LeetCode从零单刷】Find the Duplicate Number

    题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modif

    【LeetCode从零单刷】Search a 2D Matrix

    题目: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previou