罗马数字转整数 · Roman to Integer
13. Roman to Integer
[抄题]:
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
- 没有想到罗马字是逆序的情况
- 没有想到要先用toCharArray()方法把字符串拆成一个字符串数组
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- toInt函数要用。否则不能直接给字母比大小
- 不是void类型的函数就要返回默认值,return 0
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
toInt函数要用。否则不能直接给字母比大小
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
直接背英文对应的数字就行了
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/*
* @param s: Roman representation
* @return: an integer
*/
public int romanToInt(String s) {
char[] sc = new char[s.length()];
sc = s.toCharArray();
int ans = toInt(sc[0]);
for (int i = 1; i < s.length(); i++) {
ans += toInt(sc[i]);
if (toInt(sc[i - 1]) < toInt(sc[i])) {
ans -= 2 * toInt(sc[i - 1]);
}
}
return ans;
}
//toInt
private int toInt (char s) {
switch(s) {
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
}
return 0;
}
}
整数转罗马字
[抄题]:
[暴力解法]:
时间分析:
空间分析:
[思维问题]:
觉得可能有很多种分解方法:应该数位分离,把千百十位分别挑出来,就只有一种了
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 把不同的罗马字拼起来也是写+号
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 记住罗马字符数组的顺序是M C X I即可
- String M[] = {"", "M", "MM", "MMM"};写法不同 String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
[复杂度]:Time complexity: O() Space complexity: O()
[英文数据结构或算法,为什么不用别的数据结构或算法]:
无
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
273. Integer to English Words 差不多,小于20的数单独列出来即可
[代码风格] :
public class Solution {
/**
* @param n: The integer
* @return: Roman representation
*/
public String intToRoman(int n) {
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};//XL
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[(n / 1000) % 10] + C[(n / 100) % 10] + X[(n / 10) % 10] + I[n % 10];
}
}
罗马数字转整数 · Roman to Integer的更多相关文章
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- [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 ...
- 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 ,即 ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
随机推荐
- vcenter修改用户密码的方法
https://192.168.x.x:9443登录,必须用administrator@vsphere.local登录,不能用root用户登录. 主页-系统设置- Single Sing-On-用户和 ...
- Linux命令详解-Apache网站服务器配置和管理
1.Apache网站服务器配置和管理 1.源码包安装 2.rpm包安装 rpm –a | grep httpd 3.启动服务 service httpd start 4.配置文件: /etc/http ...
- Ubuntu16.04LTS中使用 apt-get install 出现错误 Could not get lock /var/lib/dpkg/lock 的解决方案
背景 近期,在Ubuntu 16.04 LTS 的操作系统中,安装MySQL-python的时候出现缺少依赖包的情况: 当使用命令 # sudo apt-get install xxx 安装依赖包的时 ...
- blktrace 深度了解linux系统的IO运作
http://blog.yufeng.info/archives/751 我们在Linux上总是要保存数据的,数据要么保存在文件系统里(如ext3),要么就在裸设备里面.我们在使用这些数据的时候都是通 ...
- Linux命令行下如何终止当前程序
Linux命令行下如何终止当前程序 快捷键: Ctrl+c 在命令行下起着终止当前执行程序的作用, Ctrl+d 相当于exit命令,退出当前shell Ctrl+s 挂起当前shell(保护作用很明 ...
- 图解http pdf
扫加公众号,回复“图解HTTP”,免费获取此书.
- 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165233
Exp1 PC平台逆向破解 实验内容 一.基础知识点 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP指令即"空指令",执行到NOP指令时,CPU什么也不做,机 ...
- 2. java获取下周日-下周六的时间
String[] arrDate = new String[7]; String[] arrWeek = new String[7]; int mondayPlus = 0; Calendar cd ...
- jpa-入门级测试
- java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
String[] 转换成 ArrayList 报的错. String[] str = {"A","B"}; ArrayList<String> li ...