9. Palindrome Number【数学】

2017/3/30 21:49:57


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

版本1:要求不能用额外空间说明不能建立数组存每个数字,只能依靠运算符。

1、需要额外考虑负数情况;

2、从两边开始取出数字依次比较;

时间 O(n)  空间 O(1)

  1. public class Solution {
  2. public boolean isPalindrome(int x) {
  3. if( x < 0 ) return false;
  4. int i = 1 , j = 10 ;
  5. while( x / i >= 10 ) i *= 10 ;
  6. int flag = i;
  7. while( flag >1 ){
  8. if( x/i%j != x%j ) return false;
  9. x /= j;
  10. i /= 100;
  11. flag /= 100;
  12. }
  13. return true;
  14. }
  15. }

版本2(优先版本): 构建给定数字的相反序列,判断两个数字。优先考虑尾数为0的情况。

1、可以优化为只构建一半的序列,需要考虑奇偶位数。

  1. public class Solution {
  2. public boolean isPalindrome(int x) {
  3. if( x < 0 || x!=0 && x%10==0 ) return false;
  4. int build = 0;
  5. while( x > build ){
  6. build = build * 10 + x % 10;
  7. x /= 10;
  8. }
  9. return build == x || build/10 == x ;
  10. }
  11. }
时间: 2024-10-14 09:38:13

9. Palindrome Number【数学】的相关文章

HDU 5062 Beautiful Palindrome Number(数学)

主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can represent as (a1a2-akak-a2a1)10 or (a1a2-ak?1akak?1-a2a1)10 of a 10-based notational system, we always call x is a Palindrome Number. If it satisfies 0<

leetcode_9题——Palindrome Number (数学问题)

Palindrome Number Total Accepted: 57795 Total Submissions: 194286My Submissions Question Solution Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Hide Tags Math Have you met this question in a real i

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 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

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode【9】. Palindrome Number --java的实现

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 spa

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

【LeetCode】Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restri

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr

HDU 4937 Lucky Number (数学,进制转换)

题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last); //把[first0,last0)之间的部分替换成[first,last)之间的字符串 /* 题意: 我们将3,4,5,6认为是幸运数字.给定一个十进制数n. 现在可以讲起任意转

HDU 1018 Big Number 数学题解

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of