【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】

【013-Roman to Integer (罗马数字转成整数)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Given a roman numeral, convert it to an integer.

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

题目大意

  给定一个罗马数字,将其转换成对应的整数。

  输入的数字在1-3999之间。

解题思路

  根据罗马数字与整数数字对应关系进行加法操作,如果前一个数字比后一个大就相减,否则进行相加。

代码实现

public class Solution {
    public int romanToInt(String s) {

        int result = 0;
        int prev = 0; // 记录前一个数字的值

        for (int i = s.length() - 1; i > -1; i--) {
            switch (s.charAt(i)) {
                case ‘I‘: // 1
                    if (1 < prev) {
                        result -= 1;
                    } else {
                        result += 1;

                    }
                    prev = 1;
                    break;

                case ‘V‘: // 5

                    if (5 < prev) {
                        result -= 5;
                    } else {
                        result += 5;
                    }

                    prev = 5;

                    break;
                case ‘X‘: // 10
                    if (10 < prev) {
                        result -= 10;
                    } else {
                        result += 10;
                    }

                    prev = 10;
                    break;
                case ‘L‘: // 50
                    if (50 < prev) {
                        result -= 50;
                    } else {
                        result += 50;
                    }

                    prev = 50;
                    break;
                case ‘C‘: // 100
                    if (100 < prev) {
                        result -= 100;
                    } else {
                        result += 100;
                    }

                    prev = 100;
                    break;
                case ‘D‘: // 500
                    if (500 < prev) {
                        result -= 500;
                    } else {
                        result += 500;
                    }

                    prev = 500;
                    break;
                case ‘M‘: // 1000
                    result += 1000;
                    prev = 1000;
                    break;
            }
        }

        return result;
    }

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46963377

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-22 20:10:11

【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】的相关文章

[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. 思路:有关罗马数字的相关知识可见博客Integer to roman.从左往右遍历字符串,比较当前为和下一位的值,若是当前数字比下一位大,或者当当前数字为最后时,则加上当前数字,否则减去当前数字.这就遇到一个问题,罗马数字的字符串不是按26个字母顺序来的,如何确定大小.解决办法一:写

[LintCode] Roman to Integer 罗马数字转化成整数

Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Have you met this question in a real interview? Yes Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals htt

【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】

[007-Reverse Integer(翻转整数)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目大意 输入一个整数对其进行翻转 解题思路 通过求余数求商法进行操作. 代码实现 public class Solution { public int reverse(int x)

【LeetCode-面试算法经典-Java实现】【012-Integer to Roman(数字转罗马字符)】

[012-Integer to Roman(数字转罗马字符)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题目大意 输入一个数字,将它转成一个罗马数字,输入的数字在[1, 3999]之间. 罗马数字的表示: 个位数举例: (I, 1) (II, 2)

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

LeetCode 013 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题意] 把罗马数转换为整数 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 大体思路是每个罗马字母对应的值相加即可, 但需要处理900, 400, 90, 40, 9, 4这几个特殊值(左

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】

[064-Minimum Path Sum(最小路径和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either

【LeetCode-面试算法经典-Java实现】【056-Merge Intervals(区间合并)】

[056-Merge Intervals(区间合并)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 题目大意 给定一个区间集合,合并有重叠的区间. 解题思路 先对区间进行排序.按開始