`
淡淡的一抹
  • 浏览: 19065 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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.

解题思路
本题主要的问题是需要使用O(1)的空间复杂度,因此不能使用转换为字符串的方式做。并且在解题中需要注意一些非常特殊的情况,在代码中已经标出。

相关知识点
(1)Java中int的取值范围
四个字节,-2147483648~2147483647(一共10位

自己的代码
package leetcode;

public class PalindromeNumber {
	public boolean isPalindrome(int x) {
		if(x < 0) return false;
		if(x >= 0 && x <= 9) return true;
		if( x >= 10 && x <=99){
			if(x/10 == x%10) return true;
			else return false;
		}
		int bigBase = 100;
		int smallBase = 10;
		while(true){
			if(x < bigBase){
				bigBase /= 10;
				break;
			}
			bigBase *= 10;
			//处理整数越界
			if(bigBase == 1000000000) break;
		}
		boolean palindrome = true;
		while(x >= 100){
			int high = x/bigBase;
			int low = x%smallBase;
			if(high != low) {
				palindrome = false;
				break;
			}
			x = (x - high*bigBase - low)/10;
			bigBase /= 100;
		}
		
		//还要处理100021这种特殊情况
		if(bigBase > 100) {
			//处理100001这种情况
			if(x == 0) return true;
			return false;
		}

		if( x >= 10 && x <=99){
			if(x/10 != x%10) palindrome =false;
		}
		
        return palindrome;
    }
	
	public static void main(String[] args) {
		//int x= 0;
		//int x= 12;
		//int x= 11;
		//int x= 123;
		//int x= 121;
		//int x= 1221;
		//int x= 1234;
		//int x= 12321;
		//int x= 12211;
		//int x= 123321;
		//int x= 122113;
		//int x= 1874994781;
		//int x = 1000021;
		int x = 1000001;
		
		PalindromeNumber pn = new PalindromeNumber();
		System.out.println(pn.isPalindrome(x));
	}
}
分享到:
评论

相关推荐

    LeetCode9 Palindrome Number

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

    LeetCode Palindrome Number解决方案

    LeetCode Palindrome Number解决方案

    Palindrome-number-in-c.rar_number

    palindrome number in c

    js代码-9. Palindrome Number

    js代码-9. Palindrome Number

    程序员面试宝典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 ...

    Coding Interview In Java

    leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 ...245 Palindrome Number 593

    cpp-算法精粹

    Palindrome Number Insert Interval Merge Intervals Minimum Window Substring Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle II Spiral Matrix Spiral Matrix...

    LeetCode最全代码

    The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) ...

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

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

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

    palindrome. 示例 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 运行时间:7 毫秒,比 Java 在线提交的 Palindrome Number 快 80.06%。 内存使用:...

    leetcode浇花-LCSolutions:我的力扣解决方案

    Palindrome Number #0035 - Search Insert Position #0058 - Length of Last Word #0066 - Plus One #0083 - Remove Duplicates from Sorted List #0118 - Pascal's Triangle #0121 - Best Time to Buy and Sell ...

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

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

    leetcode338-LeetCode:LeetCode刷题总结

    9.Palindrome Number 10.Regular Expression Matching 11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter ...

    leetcode双人赛-LeetCode:力扣笔记

    Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge ...

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

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

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

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

Global site tag (gtag.js) - Google Analytics