leetcode — palindrome-number
2018-02-28 09:08
453 查看
import org.lep.leetcode.parseint.IntegerParser; /** * Source : https://oj.leetcode.com/problems/palindrome-number/ * * Created by lverpeng on 2017/7/5. * * 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. * */ public class IntPalindrome { /** * 负数可以自己决定是否进行判断,这里不进行判断,归为不是 * 依次取出整数的第一位和最后一位比较 * 第一位:整除 * 最后一位:对10求余 * * @param num * @return */ public boolean isPalindrome (int num) { if (num < 0) { return false; } int base = 1; while (num / (base) >= 10) { base *= 10; } while (num != 0) { int left = num / base; int right = num % 10; if (left != right) { return false; } num = (num % base) / 10; base /= 100; } return true; } public static void main(String[] args) { IntPalindrome palindrome = new IntPalindrome(); System.out.println(palindrome.isPalindrome(0)); System.out.println(palindrome.isPalindrome(-101)); System.out.println(palindrome.isPalindrome(1001)); System.out.println(palindrome.isPalindrome(1234321)); System.out.println(palindrome.isPalindrome(2147447412)); System.out.println(palindrome.isPalindrome(5155)); System.out.println(palindrome.isPalindrome(5552)); } }
相关文章推荐
- Leetcode Palindrome Number
- Palindrome Number leetcode 判断一个整数是否是回文
- leetcode Palindrome Number
- 4.22 leetcode -22 palindrome-number
- Leetcode Palindrome Number
- Palindrome Number java leetcode(leetcode 回形数 java版本)
- leetcode Palindrome Number 2.4 难度系数2
- 【python】【leetcode】【算法题目9—Palindrome Number】
- Leetcode-palindrome-number
- leetcode Palindrome Number
- LeetCode——Palindrome Number
- LeetCode Palindrome Number
- LeetCode(9)--Palindrome Number
- 9. Palindrome Number LeetCode
- LeetCode Palindrome Number
- LeetCode-9-Palindrome Number
- [leetcode]Palindrome Number C 语言
- Leetcode_009_Palindrome_Number
- [Leetcode]problem 9之Palindrome Number
- leetcode Palindrome Number