`
huntfor
  • 浏览: 195209 次
  • 性别: 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

    Determine whether an integer is a palindrome. Do this without extra space. Java AC版本

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

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

    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刷题手册

    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 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-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 ...

    leetcode题库-LeetCode:力码

    Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest 中等 17 Letter Combinations of a ...

    lrucacheleetcode-leetcode:leetcode

    Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to Integer 14. Longest Common Prefix 15. 3Sum 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses ...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance ...Palindrome Number 49.4% Easy

    leetcode打不开-Leet:40多个LeetCode解决方案组合。README中包含内存使用和运行时性能指标!

    leetcode打不开利特 我对 Leet 码问题的所有解决方案都可以在这里找到 9.回文数(已解决) 判断一个整数是否是回文。 当一个整数向后读与向前读相同时,它就是回文。 示例 1: Input: 121 Output: true 示例 2: ...

    Leetcode回文串拼接-leetcode_note:用于记录leetcode题目的解析

    9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode and Decode TinyURL 560 565 566 Maximum ...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode ...Palindrome Number 56.7% 简单 10 Regular Expression Matching 25.3% 困难 11 Container With Most Water 59.3% 中等 12 Integer to Roman 61.8% 中等 13 Roman to In

    leetcode530-algorithm:算法

    Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum 016 3Sum Closest 017 Letter Combinations of...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode实现strstr-leetcode-js:js刷leetcode

    leetcode实现strstr leetcode-js 记录刷leetcode分析过程,希望一点点进步! leetcode地址 刷题顺序 按照顺序刷第一遍,记录实现思路,自己的优化方案,并研究高票大佬的...Palindrome Number 双指针 2019/09/17 0010

Global site tag (gtag.js) - Google Analytics