[LeetCode][Java] Roman to Integer
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题意:
给定一个罗马数字,将其转化为整数。
给定的输入保证在1-3999之间
算法分析:
* 罗马数字规则:
* 1, 罗马数字共同拥有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。
* 罗马数字中没有“0”。
* 2, 反复次数:一个罗马数字最多反复3次。
* 3, 右加左减:
* 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
* 在较大的罗马数字的左边记上较小的罗马数字。表示大数字减小数字。
AC代码:
public class Solution
{
private int sss; public int romanToInt(String s)
{
Map<Character, Integer> dct=new HashMap<Character, Integer>() ;
dct.put('I', 1);
dct.put('i', 1);
dct.put('V', 5);
dct.put('v', 5);
dct.put('X', 10);
dct.put('x', 10);
dct.put('L', 50);
dct.put('l', 50);
dct.put('C', 100);
dct.put('c', 100);
dct.put('D', 500);
dct.put('d', 500);
dct.put('M', 1000);
dct.put('m', 1000);
int sum = 0, j;
for(int i = 0; i < s.length(); ++i)
{
j = i+1;
if(j < s.length() && dct.get(s.charAt(j)) > dct.get(s.charAt(i)))
{
sum += dct.get(s.charAt(j)) - dct.get(s.charAt(i));
i = j;
}
else
sum += dct.get(s.charAt(i));
}
return sum;
}
}
[LeetCode][Java] Roman to Integer的更多相关文章
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【JAVA、C++】LeetCode 013 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- js实现音量拖拽的效果模拟
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>js ...
- windows10下Anaconda的安装与tensorflow、opencv的安装与环境配置
刚开始学习tensorflow和opencv这一块的知识,所以用博客这个平台来把自己这段学习的经历与感想写下来. tensorflow和opencv则用Anaconda来下载和配置环境. 下载Anac ...
- 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数
<?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...
- CAS机制(多线程)
---- 什么是CAS机制 CAS机制主要是发生于Java中原子操作类(JUC)的底层实现中,其中在CAS机制中包含3个基本参数:内存地址V.旧预期值A.要修改的新值B. 当要更新一个变量的时候,只有 ...
- Http请求封装(对HttpClient类的进一步封装,使之调用更方便。另外,此类管理唯一的HttpClient对象,支持线程池调用,效率更高)
package com.ad.ssp.engine.common; import java.io.IOException; import java.util.ArrayList; import jav ...
- Python数据结构--树遍历算法
''' 遍历是访问树的所有节点的过程,也可以打印它们的值. 因为所有节点都通过边(链接)连接,所以始终从根(头)节点开始. 也就是说,我们不能随机访问树中的一个节点. 这里介绍三种方式来遍历一棵树 - ...
- ES6(对象扩展)
ES6(对象(object)新增特性) 1.简介表示法 o,k 为属性:这种为无方法的情况. 这种为有方法. 2.属性表达式 ES6中,[a]就是 b . 3.新增API 1.数组也是引用类型 2.O ...
- luogu4035 [JSOI2008]球形空间产生器
如果单按照距离相等的话既是高次也没有半径,所以因为给了 \(n+1\) 组点就想到两两做差. 假如一组点是 \(\{a_i\}\) 一组是 \(\{b_i\}\),我们能轻易地得出 \[\sum_{i ...
- [vijos1159]岳麓山上打水
[vijos1159]岳麓山上打水 试题描述 今天天气好晴朗,处处好风光,好风光!蝴蝶儿忙啊,蜜蜂也忙,信息组的同学们更加忙.最近,由于XX原因,大家不得不到岳麓山去提水.55555555~,好累啊. ...
- HDU 3911 区间合并求最大长度的问题
http://vjudge.net/problem/viewProblem.action?id=21557 题目大意: 每进行一次颜色改变都可以把一段区间内的黑石头变成白石头,白石头变成黑石头,最后问 ...