public class Solution
{
private int ChangeToInt(char c)
{
var number = ;
string s = c.ToString();
switch (s)
{
case "I":
number = ;
break;
case "V":
number = ;
break;
case "X":
number = ;
break;
case "L":
number = ;
break;
case "C":
number = ;
break;
case "D":
number = ;
break;
case "M":
number = ;
break;
default:
number = ;
break;
}
return number;
} private int[] CalculateRange(int num)
{
int[] ary = new int[];
if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
}
else if (num == )
{
ary[] = ;
ary[] = ;
} return ary;
} public int RomanToInt(string s)
{
//重复数次:一个罗马数字重复几次,就表示这个数的几倍。
//右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。
//在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字。
//但是,左减不能跨越等级。比如,99不可以用IC表示,用XCIX表示。
//加线乘千:在一个罗马数字的上方加上一条横线或者在右下方写M,表示将这个数字乘以1000,即是原数的1000倍。
//同理,如果上方有两条横线,即是原数的1000000倍。
//单位限制:同样单位只能出现3次,如40不能表示为XXXX,而要表示为XL。 var list = new List<int>();//用于存放每组的初始数据
var list2 = new List<int>();//用于存放每组的计算结果,用于最终计算
var lastnum = ;
for (int i = ; i < s.Length; i++)
{
var c = s[i];
var num = ChangeToInt(c);
if (lastnum == )//存储第一个值
{
lastnum = num;
list.Add(num);
}
else//第一个值之后的值
{
if (num <= lastnum)//当前的值,比之前的值小,可以放在右边
{
//list中的值可以放入list2
lastnum = num;
var partsum = ;
for (int j = ; j < list.Count; j++)
{
partsum += list[j];
}
list2.Add(partsum); list.Clear();
list.Add(num); }
else
{
//出现了左小,进行减法
var partsum = ;
for (int j = ; j < list.Count; j++)
{
partsum += list[j];
}
partsum = num - partsum;
list.Clear();
list.Add(partsum);
lastnum = partsum;
}
}
} if (list.Any())//处理最后一部分的值
{
var partsum = ;
for (int j = ; j < list.Count; j++)
{
partsum += list[j];
}
list2.Add(partsum);
list.Clear();
} var result = ;
foreach (var d in list2)
{
result += d;
}
Console.WriteLine(result);
return result;
}
}

https://leetcode.com/problems/roman-to-integer/#/description

leetcode13的更多相关文章

  1. leetcode-13罗马字符转整数

    leetcode-13罗马字符转整数 算法:转换的规律是先逐字符按照对应的阿拉伯数字累加,然后对于特殊的(I.X.C出现在左侧)要处理.处理方法:出现特殊字符组合减去双倍的左侧字符(在开始的处理中已经 ...

  2. 罗马数字转整数Leetcode13

    该题较为简单,但是需要知道罗马数字的表示以及取值.用了一下map,其实之前没用过,但仔细看了一下跟python的字典实际上差不多,扫了一遍函数就直接可以用了. class Solution { pub ...

  3. LeetCode13 Roman to Integer

    题意: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  4. [Swift]LeetCode13. 罗马数字转整数 | Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. LeetCode13.罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  6. LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)

    1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...

  7. LeetCode13.罗马数字转整数 JavaScript

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  8. leetcode-13双周赛-1256-加密数字

    题目描述:编码 方法一: class Solution(object): def encode(self, n): if n == 0: return "" n -= 1 A = ...

  9. leetcode-13双周赛-1257-最小公共区域

    题目描述: 方法: class Solution(object): def findSmallestRegion(self, regions, region1, region2): parent = ...

随机推荐

  1. day33 python学习 多线程

    线程的概念 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才是cpu上的执行单位. 三 线程与进程的区别 1 1.线程的创建开销小(无需申请内存空间或者资源),创建线程的 ...

  2. gphoto2 canon eos450d

    hjs@ubuntu:~$ gphoto2 --capture-image-and-download                                                   ...

  3. FineUI 3升级4.1.1时,SingleClickExpand属性改什么了? (树控件单击展开)

    private Tree InitTreeMenu(List<Menu> menus) { Tree treeMenu = new Tree(); treeMenu.ID = " ...

  4. bzoj1055玩具取名

    区间dp.记录可行性即可. #include<iostream> #include<cstdio> #include<cstring> using namespac ...

  5. JMeter--使用HTTP信息头管理器

    使用HTTP信息头管理,可以帮助测试人员设定JMeter发送的HTTP请求头所包含的信息.HTTP信息头中包含有”User-Agent".“Pragma".”Referer&quo ...

  6. MyBatis持久层框架使用总结 转载

    MyBatis持久层框架使用总结   MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google co ...

  7. Keepalived stable tarball

    Keepalived stable tarball Keepalived for Linux - Version 1.3.5 - March 19, 2017 Keepalived for Linux ...

  8. java IO切割流合并流

    切割流,将一个较大的文件,切割成多个小文件存储

  9. phpstorm xdebug

    xdebug安装 https://xdebug.org/wizard.php http://blog.csdn.net/zhyh1986/article/details/45172685 http:/ ...

  10. SpringCloud中接收application/json格式的post请求参数并转化为实体类

    @CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod ...