leetcode09-Palindrome Number之Java版本

我的leetcode之旅,该篇章主要完成使用Java实现算法。这是第9篇Palindrome Number

全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.题目简介:

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

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.

2.我的思路:

1.将x的从个位开始取出乘以10+上后面的数,并判断最后是否等于X就行

3.我的AC代码

public class Solution {
    public boolean isPalindrome(int x) {
       if (x < 0 || (x > 0 && x%10 == 0)) {
        return false;
    }
    int num = 0;
    while (x > num) {
        num = num * 10 + x % 10;
        x /= 10;
    }
    return x == num || num/10 == x;
    }
}

好的本章介绍到这里 来自伊豚wpeace(blog.wpeace.cn)

时间: 2025-02-01 15:47:41

leetcode09-Palindrome Number之Java版本的相关文章

Palindrome Number leetcode java

题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

Palindrome Number之Java实现

一.题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.Example 1: Input: 121 Output: trueExample 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right

leetcode palindrome number(easy) /java

题: 把x翻转后,判断是否与x相等.需考虑溢出.但是如果此数翻转后溢出,那么说明其不是回文数. 推理入下,如果翻转后的数溢出且是回文数,那么原数也溢出.矛盾. public class Solution { public boolean isPalindrome(int x) { if(x<0) return false; int y=0,z=x; while(x!=0) { y=y*10+x%10; x=x/10; } return y==z; } } 以及复数不是回文数.

LeetCode【9】. Palindrome Number --java的实现

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 spa

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

centos 7 java版本切换

有一个项目需要java 1.7配合,原服务器上已安装java 1.8,需要切换版本到java 1.7. 查看centos7支持的java版本 yum search java|grep java-1 java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos java-1.6.0-openjdk-devel.x86_64 : OpenJDK Develo

权重轮询调度算法 java版本

权重轮询调度算法(Weighted Round-Robin Scheduling)--java版本 由于每台服务器的配置.安装的业务应用等不同,其处理能力会不一样.所以,我们根据服务器的不同处理能力,给每个服务器分配不同的权值,使其能够接受相应权值数的服务请求. 2个java源文件,如下所示: public interface IProduceStrategy { public int getPartitionIdForTopic(); } public class WeightFactorPr

hdu 5062 Beautiful Palindrome Number(Bestcodeer Round #13)

Beautiful Palindrome Number                                                                 Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 116    Accepted Submission(s): 82 Problem Description

关于消息推送的补充,主要介绍服务端的实现,包含object c 版本 c 版本 java 版本 php 版本 (转)

要实现消息推送功能,我们可以采用第三方(腾讯:信鸽:百度:云推送:极光推送:友盟):当然,因为各种原因,我们不能使用第三方的推送服务,那我们就需要自己编写服务端.在网上寻觅了很久,找到一篇很不错的讲解消息推送的文章,包含(object c 版本 c 版本 java 版本 php 版本)的后端实现,分享之. 原文地址:http://tanqisen.github.io/blog/2013/02/27/ios-push-apns/ 一步一步实现iOS应用PUSH功能 FEB 27TH, 2013 1