LeetCode——Palindrome Number
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.
推断一个整数是否是回文数。不要是使用而外的空间。
首先想到的是例如以下的方法,即若是个位数。则返回true,否则就前后相应的位置推断是否相等。
public static boolean isPalindrome(int x) {
if(x / 10 == 0 && x >= 0)
return true;
String str = x + "";
char ch[] = str.toCharArray();
for(int i=0;i<ch.length / 2;i++){
if(ch[i] != ch[ch.length - i - 1])
return false;
}
return true;
}
也能够将原来的数逆一下,再与原来的数进行比較。
public static boolean isPalindrome(int x) {
int reverse = 0,temp = Math.abs(x);//考虑了负数,转成正数处理 ?
while( x != 0 )
{
reverse = reverse * 10;
reverse = reverse + x%10;
x = x/10;
}
return reverse == temp;
}LeetCode——Palindrome Number的更多相关文章
- [LeetCode] Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- [Leetcode]Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
- leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- leetcode Palindrome Number python
class Solution(object): def isPalindrome(self, x): """ :type x: int :rtype: bool &quo ...
- leetcode:Palindrome Number【Python版】
一次AC 题目要求中有空间限制,因此没有采用字符串由量变向中间逐个对比的方法,而是采用计算翻转之后的数字与x是否相等的方法: class Solution: # @return a boolean d ...
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- [leetcode] Palindrome Number(不使用额外空间)
本来推断回文串是一件非常easy的事情,仅仅须要反转字符串后在与原字符串相比較就可以.这道题目明白说明不能使用额外的空间.那么使用将其分解连接成字符串的方法便不是可行的.仅仅好採用数学的方式: 每次取 ...
- Palindrome Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Palindrome Number - LeetCode 注意点 负数肯定是要return false的 数字的位数要分奇数和偶数两种情况 解法 解法一: ...
随机推荐
- BootStrap中按钮点击后被禁用按钮的最佳实现方法
//禁用button $('button').addClass('disabled'); // Disables visually $('button').prop('disabled', true) ...
- 织梦DedeCMS使用SQL批量替换文章标题内容
在使用织梦DedeCMS的过程中,出于伪原创或者其他的原因,我们需要对文档的内容.标题.描述等等进行同义词或者其他的替换.这个就是一个简单的织梦SQL语句操作的问题,No牛网在织梦DedeCMS常用S ...
- 监控之_nrpe
监控机上安装nagios插件和nrpe(nrpe添加为xinetd服务) 1.添加nagios用户 /usr/sbin/useradd nagios passwd nagios 2.安装nagio ...
- POST提交表单,本地Windows测试无乱码,而将项目部署到服务器端产生乱码原因之一
项目在Windows机上eclipse发布至tomcat测试没有问题,表单提交到数据库的中文都能正常显示,而将项目部署到服务器上时,出现中文乱码 确认POST提交都使用了request.setChar ...
- remove()
remove() 用于从列表中删除指定的元素 In [35]: l = ['a', 'b', 'c'] In [36]: l.remove('b') In [37]: l Out[37]: ['a', ...
- 如何快速入门单片机C语言
一.为什么要学单片机技术? 传统的电子产品升级改造成智能化的电子产品需要用到单片机技术.也就是说传统的电子产品如电视机.电子表.计算器.数码相机.手机.MP3.遥控器.洗衣机等产品智能化.微型化,需要 ...
- discuz 文档说明
Discuz 文档说明 基于7.0的标准程序,部分与插件无关的文件不作说明 文件颜色说明: 红色:程序核心文件,修改这类文件时千万要注意安全! 橙色:做插件几乎不会用到的文件,大概了解功能就可以了, ...
- 佛祖保佑永无bug
世界最难懂C语言代码竞赛: // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // O\ = /O // ____/`---'\_ ...
- 当JS出现的Cannot read property 'XXX' of null错误
由于在加载JS的时候,页面还未加载完成,就出现了这样的错误.解决方法很简单,将这段 js 放到页面的最下面,等到所以页面加载完成时,再加载这段JS.
- c++11——模板的细节改进
c++11改进了编译器的解析规则,尽可能的将多个右尖括号(>)解析为模板参数结束符,方便编写模板相关的代码. 1. 模板的右尖括号 之前的c++标准中,模板套模板中右尖括号不能连在一块,否则会和 ...