刚刚接触scala语言,所以写的不符合函数式编程风格,拿简单的题目来熟悉下语言。这道题目很简单,只要知道罗马数字是怎么计数的就好。
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
代码:
def romanToInt(s: String): Int = {
val numList = new Array[Int](s.length);
var i = 0
while(i < s.length){
numList(i) = convertRomanLetterToInteger(s.charAt(i))
i += 1
}
var sum = 0;
sum += numList(0);
i = 1
while(i < numList.length){
if(numList(i-1) < numList(i) && isLittleNum(numList(i-1)))
sum -= numList(i-1) * 2
sum += numList(i)
i += 1
}
return sum
}
def convertRomanLetterToInteger(num: Char): Int = {
num match {
case 'I' => 1
case 'V' => 5
case 'X' => 10
case 'L' => 50
case 'C' => 100
case 'D' => 500
case 'M' => 1000
}
}
def isLittleNum(x:Int): Boolean = {
if(x == 1 || x == 10 || x == 100)
true
else
false
}