原题链接: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. @Modules( ... ) 多个包路径问题

    如何支持多个包路径,modules不在同一个报名下 @Modules(scanPackage = true, packages = "cn.wizzer.modules, com.xxx.m ...

  2. vue.js--基础事件定义,获取数据,执行方法传值

    <template> <div id="app"> <h1>{{ msg }}</h1> <br> <button ...

  3. Android(java)学习笔记15:匿名内部类实现多线程

    1. 使用匿名内部类实现多线程 二话不说,首先利用代码体现出来,给大家直观的感觉: package cn.itcast_11; /* 4 * 匿名内部类的格式: 5 * new 类名或者接口名() { ...

  4. Android(java)学习笔记14:Java线程池

    1. 线程池: 1)程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互.而使用线程池可以很好的提高性能,尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池. 2)线程池里 ...

  5. linux shell——zsh的安装与使用

    Shell是在程序员与服务器间建立一个桥梁,它对外提供一系列命令,让我们得以控制服务器.常用的Bash就是Shell的一种,也是Linux下默认Shell程序.这里介绍一种更强大的.更人性化的Shel ...

  6. P1272

    P1272 重建道路 题目描述 一场可怕的地震后,人们用N个牲口棚(1≤N≤150,编号1..N)重建了农夫John的牧场.由于人们没有时间建设多余的道路,所以现在从一个牲口棚到另一个牲口棚的道路是惟 ...

  7. 八数码(IDA*算法)

    八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...

  8. 【题解】洛谷P3399 丝绸之路

    我丑,话说在前头 题目: https://www.luogu.org/problemnew/show/P3399 一道挺简单的DP 思路不难想 但是蒟蒻总是写错初始化啥的 思路 定义f[i][j]为第 ...

  9. python-time、datetimme模块

    time模块 1.time.time():返回当前时间的时间戳. 打印时间戳: >>> import time >>> time.time() 1530329387 ...

  10. 模块socket使用

    什么是socket:socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.我们无需再去深入理解tcp/udp协议,按照socket的规定去使用就行了. 首先一个c/s架构:分为两 ...