zoukankan      html  css  js  c++  java
  • 9. Palindrome Number 回文 my second leetcode 20170807

    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.

    判断一个整形数字是否为回文串 

     1 public class Solution {
     2     public boolean isPalindrome(int x) {
     3         int res =0;
     4         int temp = 0;
     5         int begin = x;
     6         if(x<0) return false;
     7         while(x!=0){
     8             temp = temp * 10 + x  % 10;
     9             if(temp>Integer.MAX_VALUE) return false;
    10             res = temp;
    11             x /=10;
    12         }
    13         if(begin == res )
    14             return true;
    15         else 
    16             return false;
    17     }
    18 }

    总结:这个题和reverse ingeter 非常类似,区别在于他没有负数,同时如果你的反过来超过了最大值那么肯定不是回文数,如果没超过的话再去对比和原数是否相等,相等的话才是回文

    不积跬步无以至千里,千里之堤毁于蚁穴。 你是点滴积累成就你,你的丝丝懒惰毁掉你。 与诸君共勉
  • 相关阅读:
    坑爹的微信支付v3,其实没有那么坑
    Mysql探究之null与not null
    Mysql的空值与NULL的区别
    Java编程思想(第4版) 中文清晰PDF完整版
    URI和URL的区别
    html 文本输入框效果大汇集
    HTTP状态码大全
    Silverlight ModelView中调用UI进程
    appium部分api
    appium元素定位
  • 原文地址:https://www.cnblogs.com/haoHaoStudyShare/p/7302465.html
Copyright © 2011-2022 走看看