①Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
1.比较常规的解法,直接求出其回文数,然后跟给定数据比较:
class Solution {
public:
bool isPalindrome(int x) {
int t = 0, ret = x;
if (x < 0)
{
return false;
}
else
{
while (x > 0)
{
t = t *10 + x % 10;
x /= 10;
}
}
if (t != ret)
return false;
else
return true;
}
};
2.比较巧妙地解法:
首先还是比较是否为负数,若为负数则返回false,这里还可以加一个判断,因为我们知道整数的最高位不能是0,所以回文数的最低位也不能为0,数字0除外,所以如果发现某个正数的末尾是0了,也直接返回false即可
这种解法呢主要是将数字后半部分取出来,然后翻转,看跟前段数字是否相同(具体做法是,每次通过对10取余,取出最低位的数字,然后加到取出数的末尾,就是将revertNum乘以10,再加上这个余数,这样我们的翻转也就同时完成了,每取一个最低位数字,x都要自除以10。这样当revertNum大于等于x的时候循环停止。由于回文数的位数可奇可偶,如果是偶数的话,那么revertNum就应该和x相等了;如果是奇数的话,那么最中间的数字就在revertNum的最低位上了,我们除以10以后应该和x是相等的)
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0 || (x % 10 == 0 && x != 0)) return false;
int revertNum = 0;
while (x > revertNum) {
revertNum = revertNum * 10 + x % 10;
x /= 10;
}
return x == revertNum || x == revertNum / 10;
}
};
②Roman to Integer
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: "III"
Output: 3
Example 2:
Input: "IV"
Output: 4
Example 3:
Input: "IX"
Output: 9
Example 4:
Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
1.特别常规的一般的不动“脑子”的解法:
class Solution {
public:
int romanToInt(string s) {
int sum = 0;
for (int i = 0; i < s.size(); ++i)
{
switch (s[i])
{
case 'I':
if (s[i + 1] == 'V')
{
sum += 4;
++i;
}
else if (s[i + 1] == 'X')
{
sum += 9;
++i;
}
else
sum += 1;
break;
case 'V':
sum += 5;
break;
case 'X':
if (s[i + 1] == 'L')
{
sum += 40;
++i;
}
else if (s[i + 1] == 'C')
{
sum += 90;
++i;
}
else
sum += 10;
break;
case 'L':
sum += 50;
break;
case 'C':
if (s[i + 1] == 'D')
{
sum += 400;
++i;
}
else if (s[i + 1] == 'M')
{
sum += 900;
++i;
}
else
sum += 100;
break;
case 'D':
sum += 500;
break;
case 'M':
sum += 1000;
break;
}
}
return sum;
}
};
2.使用unordered_map存储数据,然后考虑两种情况:
第一,如果后面的数字比前面的数字小或者是最后一个数字时,加上当前数字
第二,其他情况减去当前数字
class Solution {
public:
int romanToInt(string s) {
int res = 0;
unordered_map<char, int> mp{{'I',1},{'V',5},{'X',10},{'L',50},
{'C',100},{'D',500},{'M',1000}};
for (int i = 0; i < s.size(); ++i)
{
int val = mp[s[i]];
if (i == s.size() - 1 || mp[s[i + 1]] <= mp[s[i]]) res += val;
else
res -= val;
}
return res;
}
};