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;
}

最新文章

  1. iOS开发小技巧--iOS8之后的cell自动计算高度
  2. HTTP 错误 503.2 - Service Unavailable 正在超过 serverRuntime@appConcurrentRequestLimit 设置的值。
  3. jquery1.9+获取append后的动态元素
  4. LeetCode——Add Two Numbers
  5. 【剑指offer 面试题27】二叉搜索树与双向链表
  6. Busybox支持中文的解决办法
  7. SGU 172.eXam(二分图染色)
  8. jQuery技巧大放送【转】
  9. linux crontab详解
  10. 让 kibana 后台启动的方案
  11. golang-在gin中cookie跨域设置(配合ajax)
  12. Linux下Zookeeper安装使用
  13. 排序算法--插入排序(Insertion Sort)_C#程序实现
  14. Python-1 试玩OpenCV
  15. VMware安装centos虚拟机 通过NAT与主机互通并能上网
  16. Python 爬虫入门(三)—— 寻找合适的爬取策略
  17. 解决mysql不能远程登入的问题
  18. jQuery+SpringMVC中的复选框选择与传值
  19. P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…
  20. cadence help文件库调出指令 :cdnshelp

热门文章

  1. PL/SQL developer(绿色版)安装及配置
  2. 设置同样字体大小,chrome浏览器有时字体偏大的解决办法(转)
  3. MathTyp使用过程的几个问题
  4. jQuery Datatable 转载
  5. golang包管理
  6. 非常不错的前端框架Kendo-ui
  7. WPF 自定义命令 以及 命令的启用与禁用
  8. 第四章 Spring.Net 如何管理您的类___让对象了解自己的容器
  9. 【iOS与EV3混合机器人编程系列之三】编写EV3 Port Viewer 应用监測EV3port数据
  10. Python 入门(三)整数和浮点数+布尔类型