原题链接:https://leetcode.com/problems/palindrome-number/description/

1. 题目要求:判断一个int类型整数是否是回文,空间复杂度O(1)

2. 注意:负数不是回文!!因为前面有负号!注意整数溢出问题。

3. 思路:依然采用取余取整的方法

 package com.huiAlex;

 public class PalindromeNumber3 {
public static void main(String[] args) {
PalindromeNumber3 pn = new PalindromeNumber3();
System.out.println(pn.isPalindrome(-12321));
System.out.println(pn.isPalindrome(-2147483648));
System.out.println(pn.isPalindrome(2147447412));
} public boolean isPalindrome(int x) {
// 负数不是回文!!!
if (x < 0) {
return false;
}
int num = x;
System.out.println(num);
int result = 0;
while (num != 0) {
if (result > (Integer.MAX_VALUE - num % 10) / 10) {
return false;
}
result = result * 10 + num % 10;
num = num / 10;
}
if(x>0){ }else {
x = -x;
}
if (result == x) {
return true;
} else {
return false;
}
}
}

LeetCode:9. Palindromic Number(Medium)的更多相关文章

  1. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

  2. LeetCode:36. Valid Sudoku(Medium)

    1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...

  3. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  4. LeetCode:16. 3Sum Closest(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...

  5. LeetCode:49. Group Anagrams(Medium)

    1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...

  6. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  7. PAT 甲级 1019 General Palindromic Number(20)(测试点分析)

    1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...

  8. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  9. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

随机推荐

  1. Ubuntu 下安装Mongodb

    Mongodb是一款开源的数据库,这里不用我多说了,下面说一下Ubuntu下安装Mongodb可能遇到的问题和解决方案. 故事背景: 今天M$促销,1¥Windows Azure 4000¥-30天的 ...

  2. 谣言粉碎机 - 极短时间内发送两个Odata request,前一个会自动被cancel掉?

    背景 有时我们能在Chrome开发者工具的Network tab里观察到SAP UI5应用会发出某些状态为"取消"的OData请求.如下图第五个请求. 之前有一种似是而非的说法:极 ...

  3. python UI自动化实战记录一:测试需求与测试思路

    测试需求: 项目包含两个数据展示页面,数据均来自于四个数据源接口. 测试操作步骤: 选择5个大类型中的一个,每个大类型下有3个子类型,选择任一子类型,页面数据更新.需验证页面上的数据与数据源接口数据一 ...

  4. Android(java)学习笔记57:PC and Phone 通信程序

    1. 首先我写的程序代码如下: package com.himi.udpsend; import java.net.DatagramPacket; import java.net.DatagramSo ...

  5. Python 函数作为返回值

    函数作为返回值高阶函数除了可以接收函数作为参数外,还可以把函数作为结果值返回. def lazy_sum(*args): def sum(): ax=0 for n in args: ax = ax ...

  6. 【luogu P1993 小K的农场】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1993 1.差分约束: 对于a - b <= c 有一条 b-->a 权值为c 对于a - b & ...

  7. Android学习笔记_69_android 支付宝之网页支付和快捷支付

    参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...

  8. android SQLITE的基本使用总结(八)

    sharedPreferences只适合存储比较简单的数据和键值对,支持不同的数据类型 文件存储连键值对都没有,不会进行任何格式化处理,存储简单的二进制或者文本数据 sqlite则能处理一些数据量大, ...

  9. [Oracle]分区索引

    上一节学习了分区表,接着学习分区索引. (一)什么时候对索引进行分区 · 为了避免移动数据时重建整个索引,可对索引分区,在重建索引时,只需重建与数据分区相关的索引: · 在对分区表进行维护时,为了避免 ...

  10. django-单表操作

    #######单表操作######## 前面视图层,模板层.路由层都写了大概,项目肯定是会和数据库打交道,那就讲讲orm的单表查询吧,直接写过一点点,不太全面. 1.项目刚创建好,我们需要在setti ...