[LeetCode]13. Roman to Integer
2016-03-06 11:15
267 查看
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:建立一个与字符串等长的数组存放标志位,遍历整个字符串,若该的字符所表示的数字比其右边相邻的字符表示的数字小,则标志位记为-1;
否则记为1
将对应的字符表示的数字与相应的标志位相乘,做和得出结果
Input is guaranteed to be within the range from 1 to 3999.
思路:建立一个与字符串等长的数组存放标志位,遍历整个字符串,若该的字符所表示的数字比其右边相邻的字符表示的数字小,则标志位记为-1;
否则记为1
将对应的字符表示的数字与相应的标志位相乘,做和得出结果
<span style="white-space:pre"> </span>public int romanToInt(String s) { <span style="white-space:pre"> </span>if (s == null) return 0; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>char [] temp = s.toCharArray(); <span style="white-space:pre"> </span>int [] it = new int[temp.length]; <span style="white-space:pre"> </span>int [] a = new int[temp.length]; <span style="white-space:pre"> </span>int sum = 0; <span style="white-space:pre"> </span>final char I = 'I'; <span style="white-space:pre"> </span>final char X = 'X'; <span style="white-space:pre"> </span>final char L = 'L'; <span style="white-space:pre"> </span>final char C = 'C'; <span style="white-space:pre"> </span>final char D = 'D'; <span style="white-space:pre"> </span>final char M = 'M'; <span style="white-space:pre"> </span>final char V = 'V'; <span style="white-space:pre"> </span>for (int i = 0; i < temp.length; i++) { <span style="white-space:pre"> </span>char t = temp[i]; <span style="white-space:pre"> </span>switch(t) { <span style="white-space:pre"> </span>case I : it[i] = 1; break; <span style="white-space:pre"> </span>case V : it[i] = 5; break; <span style="white-space:pre"> </span>case X : it[i] = 10;break; <span style="white-space:pre"> </span>case L : it[i] = 50; break; <span style="white-space:pre"> </span>case C : it[i] = 100; break; <span style="white-space:pre"> </span>case D : it[i] = 500; break; <span style="white-space:pre"> </span>case M : it[i] = 1000; break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>for (int j = 0; j < it.length-1; j++) { <span style="white-space:pre"> </span>if (it[j] < it[j+1]) a[j] = -1; <span style="white-space:pre"> </span>else a[j] = 1; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>a[temp.length-1] = 1; <span style="white-space:pre"> </span>for(int m = 0;m < temp.length; m++) { <span style="white-space:pre"> </span>sum = sum + it[m]*a[m]; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return sum; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>}
相关文章推荐
- Linux ftp安装
- 43. Multiply Strings 字符串表示的大数乘法
- HDU 5636 Shortest Path 分治+搜索剪枝
- 软件工程第一个程序:像阿超那样,花20分钟写一个能自动生成小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算。
- MyBatis generator 的使用
- leetcode:Container With Most Water 【Java】
- 修改属性
- 学习进度条(1-3周)
- SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-005- 异常处理@ResponseStatus、@ExceptionHandler、@ControllerAdvice
- 《构建之法》阅读笔记01-快速阅读后的几个问题。
- Java 回顾笔记_包
- 用Eclipse编写Java时调用algs4.jar和stdlib.jar解决StdOut和StdIn无法被识别的问题
- 原码,反码,补码详解
- LDA模型
- SQL SERVER 下载地址
- 排序算法之冒泡排序
- 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法
- 阻止冒泡封装
- C++面向对象高级编程笔记01--GeekBand
- 2016-3-6 linux基础学习13——条件判断、文件测试、bash变量类型