【Leetcode-easy】Roman to Integer

罗马数字转化为整数
* 1、基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
* 2、不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
* 3、V 和 X 左边的小数字只能用 Ⅰ;
* 4、L 和 C 左边的小数字只能用X;
* 5、D 和 M 左边的小数字只能用 C。

思路:顺序读取字符,如果前面后面一位大于前面一位,则减去2倍的前面一位,因为前面加过一次。否则加上该位对应的数字。

 1     public int romanToInt(String s) {
 2          int result=toNum(s.charAt(0));
 3          for(int i=1;i<s.length();i++){
 4              if(toNum(s.charAt(i-1))<toNum(s.charAt(i))){
 5                  result+=toNum(s.charAt(i))-2*toNum(s.charAt(i-1));    //为什么要减去2倍的前一位,是因为前面加过一次。可以分析 XIX=19;
 6              }else{
 7                  result+=toNum(s.charAt(i));
 8              }
 9          }
10         return result;
11     }
12     int toNum(char ch){    //或是通过存储在HashMap中。
13         int n=0;
14         switch(ch){
15             case ‘I‘: return 1;
16             case ‘V‘: return 5;
17             case ‘X‘: return 10;
18             case ‘L‘: return 50;
19             case ‘C‘: return 100;
20             case ‘D‘: return 500;
21             case ‘M‘: return 1000;
22         }
23         return n;
24     }
时间: 2024-08-01 22:47:59

【Leetcode-easy】Roman to Integer的相关文章

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【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. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【leetcode系列】3Sum

这个题我最开始的思路是:先一个数定下来,然后在除这个数之外的集合里面找另外两个数,最后计算和.如此反复,对于N个数,需要进行N-2次循环. 我遇到的问题就是怎么找另外两个数,其实我想过参照Two Sum里面的解法,就是用Hashtable存,键值对的结构是<任意两个数的和,<下标1,下标2>>,但是构造这个Hashtable就需要O(N^2),后面真正解的时候有需要O(N^2). 参考了大牛的解法后,明白了找两个数还是用两个下标同时往中间移动比较好,下面上代码. import ja

【leetcode系列】Two Sum

解法一,我自己想的,有点弱,时间复杂度O(n^2). public class Solution { public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] + numbers[j] == t

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new

【leetcode 简单】第十七题 二进制求和

实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842...,   由于返回类型是整数,小数部分将被舍去. #define PF(w) ((w)*(w)) int mySqrt(int x) { int start = 0; int end = x; double mid = 0; i