【LeetCode从零单排】No.9 Palindrome Number

简介: 题目      这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意)。Palindrome就是判断一个整数是否对称。Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could

题目

      这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意)。Palindrome就是判断一个整数是否对称。

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.




代码

public class Solution {
    public boolean isPalindrome(int x) {
            if(x<0){
    	    	return false;
    	    }
    	    else{
    	    	  if(x==0){
    	    		  return true;
    	    	  }
    	    	  else{
    	    		  int temp=0;
    	    		  int temp_x=0;
    	    		  temp=x;
    	    		  while(temp/10!=0 || (temp<=9 && temp>0)){
    	    			  temp_x=temp_x*10;
    	    			  temp_x+=temp%10;
    	    			  temp=temp/10;
    	    		  }
    	    		//  System.out.print(""+temp_x);
    	    		  if(x==temp_x){
    	    			  return true;
    	    		  }
    	    		  else{
    	    			  return false;
    	    		  }
    	    		  
    	    	  }
    	    }
 }
    
}



/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/



目录
相关文章
|
5月前
|
算法
Leetcode 313. Super Ugly Number
题目翻译成中文是『超级丑数』,啥叫丑数?丑数就是素因子只有2,3,5的数,7 14 21不是丑数,因为他们都有7这个素数。 这里的超级丑数只是对丑数的一个扩展,超级丑数的素因子不再仅限于2 3 5,而是由题目给定一个素数数组。与朴素丑数算法相比,只是将素因子变了而已,解法还是和朴素丑数一致的。
63 1
|
5月前
|
存储
Leetcode Single Number II (面试题推荐)
给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。
21 0
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode contest 190 5417. 定长子串中元音的最大数目 Maximum Number of Vowels in a Substring of Given Length
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode 136. 只出现一次的数字 Single Number
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
LeetCode contest 177 5169. 日期之间隔几天 Number of Days Between Two Dates
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
|
算法
LeetCode 414. Third Maximum Number
给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。
66 0
LeetCode 414. Third Maximum Number