zoukankan      html  css  js  c++  java
  • 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 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.

    一种方法可以避免造成溢出,就是直接安装PalidromeString的方法,就直接判断第一个和最后一个,循环往复。这样就不会对数字进行修改,而只是判断而已。

     1 public boolean isPalindrome(int x) {
     2         //negative numbers are not palindrome
     3         if (x < 0)
     4             return false;
     5  
     6         // initialize how many zeros
     7         int div = 1;
     8         while (x / div >= 10) {
     9             div *= 10;
    10         }
    11  
    12         while (x != 0) {
    13             int left = x / div;
    14             int right = x % 10;
    15  
    16             if (left != right)
    17                 return false;
    18  
    19             x = (x % div) / 10;
    20             div /= 100;
    21         }
    22  
    23         return true;
    24     }

     Reference:

    http://www.programcreek.com/2013/02/leetcode-palindrome-number-java/ 

    http://www.cnblogs.com/springfor/p/3889214.html

  • 相关阅读:
    mysql 删除与安装
    mysql 中文乱码 或 问号
    系统锁屏
    技术相关
    织梦 php 网站建设
    linux mysql 安装与使用
    技术收集
    批处理 关闭 进程
    二十三种设计模式
    三星(samsung)手机i699内容:解锁boot loader,刷recovery,刷机(刷rom),root综合教程
  • 原文地址:https://www.cnblogs.com/hygeia/p/4797513.html
Copyright © 2011-2022 走看看