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. css的两种盒子模型

    css的两种盒子模型:W3C标准盒子模型.IE盒子模型 两者的相同之处:都包含margin.border.padding.content 两者的不同之处:W3C标准盒子模型的content部分不包含其 ...

  2. uva---(11549)CALCULATOR CONUNDRUM

    Problem C CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She ...

  3. IO流--复制picture ,mp3

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import ...

  4. NPOI相关

    总结一下工作中遇到的NPOI以及在ASP.NET MVC中的使用 http://www.cnblogs.com/fenglingyi/p/4750323.html

  5. jquery用Ajax中的回调函数时注意事项

    前端代码 <script language="javascript" type="text/javascript" src="<?php ...

  6. MySql的导入与导出

    1.导入 load data infile '/tmp/yhb/skin_info.txt' into table t_skin fields terminated by '\t' (skin_id, ...

  7. lmdb简介——结合MVCC的B+树嵌入式数据库

    lmdb简介 lmdb是openLDAP项目开发的嵌入式(作为一个库嵌入到宿主程序)存储引擎.其主要特性有: 基于文件映射IO(mmap) 基于B+树的key-value接口 基于MVCC(Multi ...

  8. TelephonyManager对黑名单的管理

    import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util. ...

  9. uva 1210

    #include<iostream> #include<cstring> using namespace std; + ; bool notprime[MAXN];//值为fa ...

  10. 解析Json的谷歌官方方法Gson和阿里巴巴的fastJson方法。

    //测试单个json文本 public void testGsonTwo(){ String jsonStr = "{\"id\":100,\"name\&qu ...