- 浏览: 19065 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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位)
自己的代码
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)); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 297String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 398题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 283题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 419题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 340题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 301题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 326题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 346题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 355题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 322题目描述 Given a string containing ... -
Length of Last Word
2014-10-21 09:41 357题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 307题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 255题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 292题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 232题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 292题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 255题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 317题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 336题目描述 Given a non-negative numbe ... -
Merge Sorted Array
2014-10-18 10:45 396题目描述 Given two sorted integer a ...
相关推荐
Determine whether an integer is a palindrome. Do this without extra space. Java AC版本
LeetCode Palindrome Number解决方案
palindrome number in c
js代码-9. Palindrome Number
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 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
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...
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\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates.cs 问题: 买卖股票的最佳时机 II 代码: Leetcode\MaxProfit\MaxProfit.cs ...
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%。 内存使用:...
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 ...
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...
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 ...
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 ...
Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common Prefix 简单 字串 Valid Parentheses 简单 堆叠 重要 Merge ...
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 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance ...Palindrome Number 49.4% Easy
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
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 ...