题目描述
罗马数字转整数,包含以下七种字符: I, V, X, L,C,D 和 M。分别对应1,5,10,50,100,500,1000。
- I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
- X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
- C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
思路
将每一个对应罗马数字转换成数字,一个一个加起来。当小数字在大数字前时,需要减法。
eg:罗马数字IV,就是V-I,即5-1=4,其他正常叠加
难点
数字转换
方法
使用数组,给每一个罗马数字对应下标,每个下标在数组中对应值。
int romanToInt(char * s){
int num[26];// 优点在于利用ASCII码,数组空间尽可能小,也更能理解
num['I' - 'A'] = 1;
num['V' - 'A'] = 5;
num['X' - 'A'] = 10;
num['L' - 'A'] = 50;
num['C' - 'A'] = 100;
num['D' - 'A'] = 500;
num['M' - 'A'] = 1000;
int result = 0, len = strlen(s);
for(int i = 0; i < len; i++){
int value = num[s[i] - 'A'];
if(i < len-1 && value < num[s[i+1] - 'A']){
result -= value;
}else{
result += value;
}
}
return result;
}
java版本(题解中一个很好的思路)
该方法避免了在数字相加时进行判断,直接++
class Solution {
public int romanToInt(String s) {
// 优点在于将6种较为特殊的情况提前转化,在switch中进行判断即可,避免了小数字在大数字前的判断,且减少了switch语句判断的次数。
// 缺点在于当特例足够多时,会变得臃肿,不及+-判断精简
s = s.replace("IV","a");
s = s.replace("IX","b");
s = s.replace("XL","c");
s = s.replace("XC","d");
s = s.replace("CD","e");
s = s.replace("CM","f");
int result = 0;
for(int i = 0;i < s.length(); i++){
result += getValue(s.charAt(i));
}
return result;
}
private int getValue(char c) {
switch(c) {
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;
case 'a' : return 4;
case 'b' : return 9;
case 'c' : return 40;
case 'd' : return 90;
case 'e' : return 400;
case 'f' : return 900;
}
return 0;
}
}
吐槽一下个人代码问题
- 习惯于return 0,导致运行结果不符合预期,多return了一个0
- 大小写问题,V和v的问题,导致ASCII码变化,计算结果不符合预期
本文由 szr 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Sep 14, 2021 at 07:44 pm
how to buy levitra buy hydroxychloroquine 200mg generic buy hydroxychloroquine generic
I intended to create you a very small note to say thank you as before for those stunning secrets you have contributed above. This is certainly pretty open-handed of you in giving extensively what exactly a lot of folks would've made available as an e book to get some profit on their own, specifically given that you could have done it in case you desired. The secrets likewise acted as the great way to understand that most people have the identical dream just like my very own to grasp a lot more with reference to this issue. I'm sure there are numerous more pleasurable opportunities in the future for individuals that looked at your site.
A lot of thanks for every one of your effort on this site. My daughter enjoys working on investigations and it is easy to see why. A number of us hear all regarding the lively medium you give precious secrets on this blog and therefore cause participation from other individuals on that situation while our girl is really being taught so much. Enjoy the rest of the year. You are always doing a tremendous job.
|Pay attention to sizes. This means that, no matter what it is, you need to try it on before buying it. Today's sizes aren't based on any standard measurements. Some brands have very different sizes from one another. If you want to buy clothing online, be sure to locate the sizing chart. Also look into their return policy.
I together with my guys were following the good ideas from your site while at once I got an awful suspicion I had not expressed respect to the site owner for those tips. Most of the young boys are actually certainly passionate to read all of them and already have without a doubt been having fun with these things. Thanks for indeed being simply considerate as well as for deciding upon certain superb guides millions of individuals are really wanting to learn about. My very own honest apologies for not saying thanks to sooner.
furosemide 40mg canada buy furosemide 40mg for sale where can i buy albuterol
I simply wished to thank you so much again. I am not sure the things that I would have implemented in the absence of the type of basics documented by you about that theme. Certainly was a real fearsome condition for me personally, nevertheless taking note of the very skilled strategy you resolved that forced me to jump for delight. Now i am grateful for your guidance and expect you really know what a powerful job you are getting into training people through your webpage. I'm certain you haven't encountered all of us.
I have to show some appreciation to this writer for bailing me out of this scenario. After exploring through the internet and finding proposals that were not powerful, I thought my life was done. Living without the presence of answers to the problems you've resolved as a result of the write-up is a critical case, and those which might have adversely damaged my career if I had not come across your web site. Your actual talents and kindness in taking care of a lot of things was invaluable. I'm not sure what I would've done if I hadn't come across such a stuff like this. I can now look ahead to my future. Thanks very much for the high quality and result oriented guide. I will not hesitate to endorse your blog post to anybody who needs guidelines on this subject matter.
azithromycin usa gabapentin 100mg oral order gabapentin generic
Thanks for all your hard work on this site. Kim loves setting aside time for investigations and it's really easy to see why. My partner and i know all about the powerful mode you present reliable guides via your web blog and as well as boost participation from other individuals on the subject so our favorite child is actually becoming educated a lot of things. Take pleasure in the remaining portion of the new year. You're the one conducting a terrific job.