题目描述

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

Example 1:

Input: 121

Output: true

Example 2:

Input: -121

Output: false

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

Example 3:

Input: 10

Output: false

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?

#include<math.h>
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
if(x>=0&&x<=9)
return true;
int weishu=0;
int temp=x;
while(temp) //先看一下数字总共有几位
{
temp=temp/10;
weishu++;
}
int jishu=0; //看一下是奇数还是偶数
jishu=weishu%2;
int mowei=1;
int shouwei=weishu+1-mowei; //shouwei代表着要和末位对比的位置,而不是真正的首位
if(jishu)
{
while(x&&shouwei!=1){
int a=pow(10,shouwei-1); //这个动作是为了取shouwei,先把后面的位数去了
if((x/a%10)!=(x%10)) //(x/a%10)是shouwei,(x%10)是末位
return false;
x=x/10;
shouwei-=2; //对比完一对,shouwei的位置就减2,因为前面的指针要往后移一位,最后一位抛弃,相当于移了2位
} }else{ //偶数个位数也一样,区别就是shouwei剩一个还是0个 while(x&&shouwei!=0){
int a=pow(10,shouwei-1);
if((x/a%10)!=(x%10))
return false;
x=x/10;
shouwei-=2;
}
}
return true;
}
};

总结

按要求不转成string来做的。没看题解,我感觉我想的还挺巧妙的,就是提交情况来看,似乎在通过中算比较慢的,但比较省内存。我估计是int转string的方法时间复杂度低。

然后看了题解,发现把数转成回文,然后看是否相等就可以了。瞬间又觉得自己有点蠢。

leetcode9 Palindrome Number(按进阶要求)的更多相关文章

  1. LeetCode9 Palindrome Number

    题意: Determine whether an integer is a palindrome. Do this without extra space.  (Easy) 分析: 自己考虑的方法是利 ...

  2. 65. Reverse Integer && Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, re ...

  3. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  4. 9. Palindrome Number

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

  5. No.009 Palindrome Number

    9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...

  6. 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List

    9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...

  7. leetcode 第九题 Palindrome Number(java)

    Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...

  8. HDU 5062 Beautiful Palindrome Number(数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...

  9. Reverse Integer - Palindrome Number - 简单模拟

    第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...

随机推荐

  1. Supalle-Admin-Layout,一个PC端和手机端都合适用的后台页面模板

    Supalle-Admin-Layout主要使用有Vue.Element-UI.layui-icon,Ajax实现采用Fetch(是有这个打算,不过目前是jQuery.). 源码地址:https:// ...

  2. 浅谈CMDB

    CMDB和运维自动化 一.运维 运维,指的是对已经搭建好的网络,软件,硬件进行维护.运维领域也是细分的,有硬件运维和软件运维 硬件运维主要包括对基础设施的运维,比如机房的设备,主机的硬盘,内存这些物理 ...

  3. 如何在 Centos7 中使用阿里云的yum源

    如何在 Centos7 中使用阿里云的yum源 1. 备份原来的yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Ba ...

  4. 第二章 javaScript操作BOM

    什么是BOM      BOM(Browser Object Model)即浏览器对象模型.      BOM提供了独立于内容 而与浏览器窗口进行交互的对象:      由于BOM主要用于管理窗口与窗 ...

  5. Core CLR Host 源码简单分析

    在定制 CLR Host的时候,可以通过调用如下代码,来获取当前需要被宿主的程序调用入口: hr = Host->CreateDelegate( domainId, L"Main,Ve ...

  6. linux集群实施与部署-----Nginx

    ( 1 ) 配置基本环境 //安装虚拟工具 #cd /media/VMware\ Tools/ #cp VMwareTools--.tar.gz/tmp/ #cd /tmp/ #tar-xvzf VM ...

  7. HTML5 第二章 列表和表格和媒体元素

    列表: (1)什么是列表? 列表就是信息资源的一种展示形式. (2)无序列表: 语法: <ul> <li>第1项</li> <li>第2项</li ...

  8. 设计模式:与SpringMVC底层息息相关的适配器模式

    目录 前言 适配器模式 1.定义 2.UML类图 3.实战例子 4.总结 SpringMVC底层的适配器模式 参考 前言 适配器模式是最为普遍的设计模式之一,它不仅广泛应用于代码开发,在日常生活里也很 ...

  9. A solution to the never shortened to-do list

    I once told my younger sister my learning system, and the basic five doctrines of my methodology. Bu ...

  10. Elasticsearch索引增量统计及定时邮件实现

    0.需求 随着ELKStack在应用系统中的数据规模的急剧增长,每天千万级别数据量(存储大小:10000000*10k/1024/1024=95.37GB,假设单条数据10kB,实际远大于10KB)的 ...