`
huntfor
  • 浏览: 201675 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Palindrome Number

 
阅读更多

新博文地址:[leetcode]Palindrome Number

http://oj.leetcode.com/problems/palindrome-number/

 
Determine whether an integer is a palindrome. Do this without extra space.

Some hints:

Could negative integers be palindromes? (ie, -1)

If you are thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

 

写道
Hint:
Don’t be deceived by this problem which seemed easy to solve. Also note the restriction of doing it without extra space. Think of a generic solution that is not language/platform specific. Also, consider cases where your solution might go wrong.

Solution:
First, the problem statement did not specify if negative integers qualify as palindromes. Does negative integer such as -1 qualify as a palindrome? Finding out the full requirements of a problem before coding is what every programmer must do. Here, we define negative integers as non-palindromes.

The most intuitive approach is to first represent the integer as a string, since it is more convenient to manipulate. Although this certainly does work, it violates the restriction of not using extra space. (ie, you have to allocate n characters to store the reversed integer as string, where n is the maximum number of digits). I know, this sound like an unreasonable requirement (since it uses so little space), but don’t most interview problems have such requirements?

Another approach is to first reverse the number. If the number is the same as its reversed, then it must be a palindrome.This seemed to work too, but did you consider the possibility that the reversed number might overflow?
We could construct a better and more generic solution. One pointer is that, we must start comparing the digits somewhere. And you know there could only be two ways, either expand from the middle or compare from the two ends. 

no more extra space?我还以为一点的空间都不能有呢,木想到还是有o(1)个空间的。看了提示之后,算法自然就想到了:(不过还是人家的代码好看。。。。贴出来晒晒)

	public boolean isPalindrome(int x) {
		if(x < 0){
			return false;
		}
		int divisor = 1;
		//为了得到最高位的数字,要选择合适的除数
		while(x / divisor >= 10){
			divisor *= 10;
		}
		while(x != 0){
			int high = x/divisor;
			int low = x%10;
			if(high != low){
				return false;
			}
			x = (x - high * divisor) / 10;
			divisor /= 100;
		}
		return true;	
	}

 

分享到:
评论

相关推荐

    LeetCode Palindrome Number解决方案

    LeetCode Palindrome Number解决方案 在本节中,我们将讨论如何确定一个整数是否是一个回文数。一个整数是一个回文数当它从左到右读取和从右到左读取相同。 问题描述: 给定一个整数,判断它是否是一个回文数。 ...

    LeetCode9 Palindrome Number

    《LeetCode9:判断回文数——Java实现详解》 在编程的世界里,LeetCode是一个广受欢迎的在线平台,它提供了各种算法问题供程序员们挑战和提升技能。今天我们要探讨的是LeetCode第9题——“回文数”。这道题目要求...

    c语言-leetcode 0009-palindrome-number.zip

    c c语言_leetcode 0009_palindrome_number.zip

    java-leetcode题解之009-Palindrome-Number

    java入门 java_leetcode题解之009_Palindrome_Number

    js-leetcode题解之9-palindrome-number.js

    js js_leetcode题解之9-palindrome-number.js

    C语言-leetcode题解之09-palindrome-number.c

    c语言入门 C语言_leetcode题解之09-palindrome-number.c

    leetcode卡-leetcode:一些leetcode问题的解决方法,请注意license!

    Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates.cs 问题: 买卖股票的最佳时机 II 代码: Leetcode\MaxProfit\MaxProfit.cs ...

    程序员面试宝典LeetCode刷题手册

    9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid ...

    LeetCode最全代码

    260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...

    LeetCode 刷题汇总1

    * 回文数(Palindrome Number):判断数字是否为回文数。 3. 数学运算: * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为...

    LeetCode_java_leetcode_刷题_

    3. **字符串处理**:Java中的String类是常考主题,如"Valid Palindrome"要求判断一个字符串是否为回文,这涉及字符串的反转和比较操作。 4. **排序与查找**:快速排序、归并排序、二分查找等经典算法经常出现在...

    Leetcode book刷题必备

    根据提供的文件信息,我们能提炼出一系列IT相关知识点,主要是围绕LeetCode这本电子书的主题——即编程面试题目解答。此电子书内容丰富,涵盖了算法和数据结构中常见的问题及其解决方案,非常适合准备技术面试的读者...

    LeetCode C++全解

    1. Introduction 2. Array i. Remove Element ii. Remove Duplicates from Sorted Array ... Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element

    leetcode答案-LeetCode:Swift中的LeetCode

    Palindrome Number Easy #13 Roman to Integer Easy #21 Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say ...

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: ...Palindrome Number JavaScript O(n) O(1) Easy 19 Remove Nth Node From End of List JavaScript O(n) O(1) Medium 21 Merge Two Sorted Lists JavaScript O(n)

    Leetcode题目+解析+思路+答案.pdf

    - **Palindrome Number**:判断一个数字是否是回文数。 - **Search a 2D Matrix**:在二维矩阵中搜索目标值。 - **Search for a Range**:在一个排序数组中查找目标值的第一个和最后一个位置。 - **Search ...

    Python_leetcode.zip

    "palindrome-pairs.py"是关于回文串和字符串匹配的题目。找到可能形成回文串的字符串对,需要对字符串进行操作并考虑各种情况,Python的字符串处理和双重循环在此类问题中起到了关键作用。 "intersection-of-two-...

    LeetCode前400题Java精美版

    9. **Palindrome Number** (Easy): 判断一个整数是否是回文数。可以将数字转换为字符串后进行比较,也可以通过数学方法进行判断。 10. **Regular Expression Matching** (Hard): 实现正则表达式的匹配功能,涉及...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix 最长公共前缀 20 Valid Parentheses 有效的括号 26 Remove Duplicates from ...

Global site tag (gtag.js) - Google Analytics