Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

  1. Input: 121
  1. Output: true

Example 2:

  1. Input: -121
  1. Output: false
  1. Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

  1. Input: 10
  1. Output: false
  1. Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Coud you solve it without converting the integer to a string?

  1. public boolean isPalindrome(int x) {
  2. int[] nums=new int[32];
  3. int i=0;
  4. if(x<0)//负数或 最后一位为0(后半句不严谨,单独0是对称的)
  5. return false;
  6. while(x/10>0){
  7. nums[i]=x%10;
  8. i++;
  9. x=x/10;
  10. }
  11. nums[i]=x%10;
  12. int len=0;
  13. for(i=31;i>=0;i--){
  14. if(nums[i]>0){
  15. len=i;//数字位数
  16. break;
  17. }
  18. }
  19. for(i=0;i<=len/2;i++){
  20. if(nums[i]!=nums[len-i])
  21. return false;
  22. }
  23. return true;
  24. }

 

LeetCode 9. Palindrome Number(回文数)的更多相关文章

  1. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  2. Leetcode 3——Palindrome Number(回文数)

    Problem: Determine whether an integer is a palindrome. Do this without extra space. 简单的回文数,大一肯定有要求写过 ...

  3. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  4. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

  5. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  6. 【LeetCode】9. Palindrome Number 回文数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...

  7. [LeetCode] Prime Palindrome 质数回文数

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...

  8. Palindrome Number 回文数

    判断一个数字是否是回文数,尝试不用其他额外空间. 注意: 负数也有可能成为回文数吗? 如果你想让int转为string,注意不用其他空间这个约束. 你也可以翻转一个int,但是有可能会溢出.     ...

  9. 【LeetCode】9 Palindrome Number 回文数判定

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  10. 9. Palindrome Number 回文数的判断

    [抄题]: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the sam ...

随机推荐

  1. 【转】Java学习---Java中volatile关键字实现原理

    [原文]https://www.toutiao.com/i6592879392400081412/ 前言 我们知道volatile关键字的作用是保证变量在多线程之间的可见性,它是java.util.c ...

  2. 56_实现类似spring的可配置的AOP框架

    > config.properties  配置文件   key=类名 > BeanFactory  Bean工厂,负责得到bean  getBean("xxx") &g ...

  3. git 学习汇总

    生成gitignore 文件: https://gitignore.io/ git 版本回退 git reset --hard HEAD^ 上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然 ...

  4. Alpha冲刺报告(6/12)(麻瓜制造者)

    今日已完成 邓弘立: 看github上的开源库 确定了几个对UI改进有帮助的第三方库 符天愉: 部署了用户修改信息,修改头像的接口,并且完成两个接口的api文档,复习了PHP的无限分类来实现商品的发布 ...

  5. 死磕nginx系列--nginx 限流配置

    限流算法 令牌桶算法 算法思想是: 令牌以固定速率产生,并缓存到令牌桶中: 令牌桶放满时,多余的令牌被丢弃: 请求要消耗等比例的令牌才能被处理: 令牌不够时,请求被缓存. 漏桶算法 算法思想是: 水( ...

  6. 【转】PHP----JS相互调用

    JS调用PHP 1.取值: 执行html,得到一个弹窗,提示:I from PHP <script type="text/javascript" src="http ...

  7. trufflesuite/truffle-hdwallet-provider

    https://github.com/trufflesuite/truffle-hdwallet-provider/blob/master/index.js 实现代码 truffle-hdwallet ...

  8. python+requests实现接口测试 - cookies的使用 (转载)

    出自:https://www.cnblogs.com/nizhihong/p/6699492.html 在很多时候,发送请求后,服务端会对发送请求方进行身份识别,如果请求中缺少识别信息或存在错误的识别 ...

  9. linux固定ip地址

    最近自己搭jenkins发现ifconfig出来ip老是变来变去决定固定服务ip,原来配置: [root@bogon bin]# cat /etc/sysconfig/network-scripts/ ...

  10. $Simpson$积分入门

    \(\rm{0x01}\) 前言 首先阐明一点,自适应辛普森算法(\(\rm{Adaptive ~Simpson's~ rule}\) )是一类近似算法(\(\rm{Approximation ~al ...