Determine whether an integer is a palindrome. Do this without extra space.

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.

Solution 1: (with extra space)

 class Solution
{
public:
bool isPalindrome(int x)
{
if(x == ) return true;
if(x < ) return false; vector<int> v;
bool flag = true; // 若x = 13314, 则v为4 1 3 3 1
while(x != )
{
v.push_back(x % );
x = x / ;
} auto left = v.begin(), right = prev(v.end());
for(; left < right; ++left, --right) // 注意: left < right
{
if(*left != *right)
{
flag = false;
break;
}
}
return flag;
}
};

Solution 2:

class Solution
{
public:
bool isPalindrome(int x)
{
if(x < ) return false;
else if(x == ) return true; bool flag = true; int temp = x, y = ;
while(x != )
{
y = y * + x % ;
x = x / ;
}
if(y != temp) flag = false;
return flag;
}
};

Palindrome Number ---- LeetCode 009的更多相关文章

  1. Palindrome Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...

  2. Palindrome Number leetcode java

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  3. 《LeetBook》leetcode题解(9):Palindrome Number[E]——回文数字

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 地址:https://github.com/hk029/leetcode 这 ...

  4. leetcode bug & 9. Palindrome Number

    leetcode bug & 9. Palindrome Number bug shit bug "use strict"; /** * * @author xgqfrms ...

  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. leetcode题解 9. Palindrome Number

    9. Palindrome Number 题目: Determine whether an integer is a palindrome. Do this without extra space. ...

  9. LeetCode--No.009 Palindrome Number

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

随机推荐

  1. 初学java之StringBuffer类的常用方法

    import java.text.*; public class Gxjun { public static void main(String atgs[]) { StringBuffer str= ...

  2. 初学java之(盒子分布)

    import javax.swing.*; import java.awt.*; class WinGrid extends JFrame { Box basebox , boxv1,boxv2; p ...

  3. 创建交货单/外向交货BAPI_OUTB_DELIVERY_CREATE_SLS/STO

    FUNCTION Z_SD_CREATE_DN. *"-------------------------------------------------------------------- ...

  4. Linux下smba服务端的搭建和客户端的使用

    解决了 windows下用root登录linuxsamba后有部分目录访问无权限的问题.应该是SELinux 设置问题. 对selinux进行修改,一般为终止这项服务,操作如下: 查看SELinux状 ...

  5. 编程思考 PetShop读后感

    标准,插拔式的设计思想建立一致的标准是通向“复用”的通道.分层,使其得到的充分的独立.一个东西如果独立了[不是孤立],这个事物就具有很强大的力量,这个和一个人的成长是相同的道理.所以呢,在写程序的过程 ...

  6. centos 运用ssh的rsa算法实现无密码登录

    ssh 公钥和私钥原理 1.客户端机子生成私钥和公钥,将公钥放到服务器证书中,然后就可以实现免密码登录.(服务器认证文件要有该登录用户的读执行权限) 2.a登录b: a机子:test01账号(b也要建 ...

  7. 计算excel列的名字

    #include <iostream> using namespace std; int main() {     unsigned int column;     cin>> ...

  8. 时钟 IoTimer

    /* 例程是在运行在DISPATCH_LEVEL的IRQL级别 例程中不能使用分页内存 另外在函数首部使用 #pragma LOCKEDCODE */ #include "Driver.h& ...

  9. 谷歌 不支持 activeX插件

    因为Chrome浏览器42以上版本已经陆续不再支持NPAPI插件,也就是说,目前的迅雷插件.FLASH插件.支付宝插件.阿里旺旺插件.百度贴吧.网银等网站都受到一定程度的影响,本文分享给大家如何让谷歌 ...

  10. 分布式一致性原理—CAP

    背景 随着分布式事务的出现,传统的单机事务模型(ACID)已经无法胜任,尤其是对于一个高访问量.高并发的互联网分布式系统来说. 如果我们要求严格一致性,很可能就需要牺牲掉系统的可用性,反之亦然.但两者 ...