Leetcode---Palindrome Number

来源:互联网 发布:阿里云主机80端口打开 编辑:程序博客网 时间:2024/06/02 02:28

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. bool isPalindrome(int x) {
  2.         if(x<0)
  3.             return false;
  4.         int t=x;
  5.         int length=0;
  6.         while(t){
  7.             t=t/10;
  8.             length++;
  9.         }
  10.         for(int i=0;i<length/2;i++){
  11.             if(f(x,i)!=f(x,length-1-i))
  12.                 return false;
  13.         }
  14.         return true;
  15.     }
  16.     int f(int x, int n){
  17.         for(int i=0;i<n;i++){
  18.             x=x/10;
  19.         }
  20.         return x%10;
  21.     }


<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2) | 评论(0) | 转发(0) |
0

上一篇:Leetcode---Valid Palindrome

下一篇:Leetcode---Palindrome Partitioning

相关热门文章
  • Leetcode---Binary Tree Maxim...
  • Leetcode---Path Sum
  • Leetcode---Path Sum II
  • Leetcode---Validate Binary S...
  • Leetcode---Recover Binary Se...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • 谁能够帮我解决LINUX 2.6 10...
  • 现在的博客积分不会更新了吗?...
  • shell怎么读取网页内容...
  • ssh等待连接的超时问题...
  • curl: (56) Recv failure: Con...
给主人留下些什么吧!~~