No. 23 - Palindrome Numbers
来源:互联网 发布:街景地图制作软件 编辑:程序博客网 时间:2024/05/21 15:42
No. 23 - Palindrome Numbers
Problem: Please implement a function which checks whether a numberis a palindrome or not. For example, 121 is a palindrome, while 123 is not.
Analysis: Many candidates can get a straightsolution which converts the input number into a string first. However, it isnot what interviewers expect usually.
Solution 1: Convert a Number into a String
It is easy tocheck whether a number is palindrome or not: We can check whether the firstcharacter and the last one are identical, and then check the second characterand the second one from end, and so on. Therefore, we can firstly convert theinput number into a string via the functionsprintf, and then check whether the string is apalindrome.
This solutioncan be implemented as the following code:
bool IsPalindrome_solution1(unsigned int number)
{
const int NUMBER_LENGTH = 20;
char string[NUMBER_LENGTH];
sprintf(string, "%d",number);
return IsPalindrome(string);
}
bool IsPalindrome(const char* const string)
{
bool palindrome = true;
if(string != NULL)
{
int length = strlen(string);
int half = length >> 1;
for(int i = 0; i < half; ++ i)
{
if(string[i] != string[length - 1 - i])
{
palindrome = false;
break;
}
}
}
return palindrome;
}
Usually thesolution above is not the one expected by interviewers. One reason is that itis intuitive while interviewers expect something innovative, and the other isthat it requires auxiliary memory to store the converted string.
Solution 2: Compose a Reversed Number
As we know, itis easy to get digits from right to left via / and % operators. For example,digits in the number 123 are 3, 2 and 1. We can construct a reversed number 321with these three digits. And then we check whether the reverse number isidentical to the original one. If it is, the original number is a palindrome.
Itscorresponding code is shown below:
bool IsPalindrome_solution2(unsigned int number)
{
int reversed = 0;
int copy = number;
while(number != 0)
{
reversed = reversed * 10 + number % 10;
number /= 10;
}
return (reversed == copy);
}
The author HarryHe owns all the rights of this post. If you are going to use part of or thewhole of this ariticle in your blog or webpages, please add a referenceto http://codercareer.blogspot.com/. If you are going to use it in yourbooks, please contact me (zhedahht@gmail.com) . Thanks.
- No. 23 - Palindrome Numbers
- Palindrome Numbers
- ZOJ 2000 Palindrome Numbers
- poj 2402 Palindrome Numbers
- Palindrome Numbers(P2402)
- POJ 2402 Palindrome Numbers
- uva 12050 - Palindrome Numbers
- UVa:12050 Palindrome Numbers
- UVA - 12050 Palindrome Numbers
- zoj2000 Palindrome Numbers
- UVALive 2889 Palindrome Numbers
- LeetCode-Palindrome Numbers
- 9. Palindrome Numbers
- Palindrome Numbers UVALive
- POJ 2402 Palindrome Numbers
- poj 2402 Palindrome Numbers
- POJ-2402(Palindrome Numbers)
- uva 12050 - Palindrome Numbers(数论)
- 我的第一个游戏FoodieThebug完成之后的心得体会
- No. 21 - Push and Pop Sequences ofStacks
- js window.event对象解析
- JSF的学习参考地址
- No. 22 - Turning Number in an Array
- No. 23 - Palindrome Numbers
- 关键词的选取注意事项
- No. 24 - Intersection of Sorted Arrays
- No. 25 - Edit Distance
- No. 26 - Minimal Number of Coins for Change
- No. 27 - Area of Rectangles
- 类名.this 的使用
- No. 28 - A Pair with the Maximal Difference
- No. 29 - Loop in List