LeetCode:9. Palindromic Number(Medium)
原题链接: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)的更多相关文章
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
- LeetCode:36. Valid Sudoku(Medium)
1. 原题链接 https://leetcode.com/problems/valid-sudoku/description/ 2. 题目要求 给定一个 9✖️9 的数独,判断该数独是否合法 数独用字 ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- LeetCode:16. 3Sum Closest(Medium)
1. 原题链接 https://leetcode.com/problems/3sum-closest/description/ 2. 题目要求 数组S = nums[n]包含n个整数,找出S中三个整数 ...
- LeetCode:49. Group Anagrams(Medium)
1. 原题链接 https://leetcode.com/problems/group-anagrams/description/ 2. 题目要求 给定一个字符串数组,将数组中包含相同字母的元素放在同 ...
- LeetCode:22. Generate Parentheses(Medium)
1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...
- PAT 甲级 1019 General Palindromic Number(20)(测试点分析)
1019 General Palindromic Number(20 分) A number that will be the same when it is written forwards or ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
随机推荐
- 关于Java中截取字符串
获取系统时间:to_char(sysdate,'yyyy-mm-dd')截取CREATETIME常量的前10位字符串:CREATETIME.substring(0,10)截取DESCRIPT常量的前2 ...
- linux系统(CentOS)下安装PhantomJS
1.查看linux系统位数,来判断下载适配的PhantomJS: 输入命令:# lsb_release -a 2.下载PhantomJS: 从官网http://phantomjs.org/downlo ...
- 【转载】#440 - A Class Can Implement More than One Interface
It's possible for a class to implement more than one interface. For example, a Cow class might imple ...
- ArcGIS10.1之crossdomain文件
大家都知道在10.1之前的版本在开发的时候需要使用跨域部署文件crossdomain.xml文件,在10.1中该文件不需要单独拷贝到IIS根目录或者是java版本的weboutput目录,在serve ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 【莫队算法模版】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意概括: 有 N 只袜子(分别编号为1~N),有 M 次查询 (L, R)里面随机 ...
- 设置IE浏览器的默认下载路径
实现效果: 知识运用: Default Download Directory键 实现代码: private void button2_Click(object sender, EventArgs e) ...
- c++字符串初始化
#include<string> string s1 = "abcdefg"; string s2("abcdefg");
- TemplateSyntaxError at /article/list-article-titles/admin/
如图红圈所示,发现一个注释掉的{% if userinfo %}标签竟然可以影响后面的标签快,不能注释,需要完全删除才不会报错. 继续这类django在html模板中直接注释掉发生错误以及解决方案: ...
- 【JeeSite】登录和主题切换
最高管理员账号,用户名:thinkgem 密码:admin 1. 密码加密:登录用户密码进行SHA1散列加密,此加密方法是不可逆的.保证密文泄露后的安全问题. 在spring-shiro配置文件 ...
- 12java基础继承
26.定义类Human,具有若干属性和功能:定义其子类Man.Woman: 在主类Test中分别创建子类.父类和上转型对象,并测试其特性. package com.hry.test; public ...