题目大意:

判断输入的数字是不是回文数。所谓回文数就是正反读都一样的数字,比如说11,121,1221这样子的数字。负数不会是回文数。

解题思路:

思路一:如果这个数是负数,则返回false,否则用一个变量记录这个数字你转后的数字,判断它和逆转前是否相等。

思路二:如果是负数或者这个数个位为0,则返回false(0除外,如果是0也返回true)。否则用一个数字记录它逆转的一半,与其另一半比较,查看是否相等。(考虑数字位数的奇偶性)

解决代码:

我只写了思路二的代码,因为相对思路二的效率比思路一高出一倍。

bool isPalindrome(int x){
    ||x!=&&x%==) return false;    //负数或者个位数为0
        ;    //记录逆转的一半
        while(x>sum){
            sum=sum*+x%;
            x/=;
        }
        ;    //包含数字位数是奇数和偶数的两种情况
}

从0开始的LeetCode生活—9. Palindrome Number(回文数)的更多相关文章

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

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

  2. LeetCode Problem 9:Palindrome Number回文数

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

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

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

  4. leetcode 9 Palindrome Number 回文数

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

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

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

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

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

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

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

  8. Palindrome Number 回文数

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

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

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

随机推荐

  1. Python 爬取 中关村CPU名字和主频

    0.准备工作   1.相关教程         Python 爬虫系列教程:http://cuiqingcai.com/1052.html         Python Web课程:http://ww ...

  2. 从输入URL到页面加载的过程?如何由一道题完善自己的前端知识体系!

    前言 见解有限,如有描述不当之处,请帮忙指出,如有错误,会及时修正. 为什么要梳理这篇文章? 最近恰好被问到这方面的问题,尝试整理后发现,这道题的覆盖面可以非常广,很适合作为一道承载知识体系的题目. ...

  3. jquery的动画学习--jquery权威指南

        前面的fadeIn和fadeOut还有fadeTo以及sildeToggle还有sildeUp\sildeDown还有toggle还有show.hide等都经常用,就不再手写了,需要注意的是f ...

  4. tp3.2 URL_MODEL为2 配置

    1. tp项目index.php同级目录  add . htaccess文件 ,rewirte重写 内容为: <IfModule mod_rewrite.c> Options +Follo ...

  5. 在C#的控制台应用中使用Dapper链接MySQL并执行一些增删改查

    一.首先先创建一个C#的控制台应用 二.然后添加上必要的命名空间 using System;using System.Collections.Generic;using MySql.Data.MySq ...

  6. 【Luogu1345】奶牛的电信(网络流)

    [Luogu1345]奶牛的电信(网络流) 题面 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成 ...

  7. 含隐变量模型求解——EM算法

    1 EM算法的引入1.1 EM算法1.2 EM算法的导出2 EM算法的收敛性3EM算法在高斯混合模型的应用3.1 高斯混合模型Gaussian misture model3.2 GMM中参数估计的EM ...

  8. MySQ备份常见问题

    1.备份的时候出现2002报错,找不到/tmp/mysql.sock,这个文件 [root@centos199 backup]# mysqldump -uroot -ppassword cz-offi ...

  9. MySQL系列-第一章节:MySQL介绍与安装

    1.数据库介绍 1.1.什么是数据库`<Database>` 简单说存放数据的仓库,这个仓库按照一定的数据结构<数据结构是指数据的组织形式或数据之间的联系>来组织.存储的,我们 ...

  10. 设置mysql密码 Access denied 问题

    原文:http://www.upwqy.com/details/31.html 在Mac上安装完mysql以后 在终端执行 /usr/local/mysql/bin/mysql 可以直接进入.但是在设 ...