9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)

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

暴力的时候,注意奇数偶数。

 1 class Solution:
 2     def isPalindrome(self, x):
 3         """
 4         :type x: int
 5         :rtype: bool
 6         """
 7         x =str(x)
 8
 9         if(len(x)%2!=0):#数
10             for i in range(int(len(x)/2)+1):
11                 if(x[i]!=x[len(x)-i-1]):
12                     return False
13         else:
14             for i in range(int(len(x)/2)+1):
15                 if(x[i]!=x[len(x)-i-1]):
16                     return False
17         return True
18
19         

原文地址:https://www.cnblogs.com/zle1992/p/8452811.html

时间: 2024-08-04 08:17:05

9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)的相关文章

[LeetCode]Palindrome Number 判断二进制和十进制是否为回文

class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ num<<=1; t>>=1; len++; } len/=2; while(len--){ if((num&x==0)&&(x&1)!=0){ return 0; } x&=(~num); x>>=1; num>>=2; }

Java 判断是否为汉字 判断是否为乱码 判断字符串是否为双整型数字 整数 数字

/**  * 判断是否为汉字  *   * @param str  * @return  */ public static boolean isGBK(String str) {  char[] chars = str.toCharArray();  boolean isGBK = false;  for (int i = 0; i < chars.length; i++) {   byte[] bytes = ("" + chars[i]).getBytes();   if (

求一个整型数字中有没有相同的部分,例如12386123这个整型数字中相同的部分是123,相同的部分至少应该是2位数,如果有相同部分返回1,如果没有则返回0。方法是先将整型数字转换到数组中,再判断。函数为 int same(int num)其中num是输入的整型数字

import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Test { public static void main(String[] args) { /** * 2.求一个整型数字中有没有相同的部分,例如12386123这个整型数字中相同的部分是123, * 相同的部分至少应该是2位数,如果有相同部分返回1,如果没有则返回0. * 方法是先将整型数字转换到数组中,再判断.

判断输入的字符串是否是回文数

<?phpfunction yuanyincount($str){ $str_len=strlen($str); $a_count=0; $e_count=0; $i_count=0; $o_count=0; $u_count=0; $other_count=0; //五种原因字母的数组,没写输出 $a_arr=array(); $e_arr=array(); $i_arr=array(); $o_arr=array(); $u_arr=array(); $other_arr=array();

Leetcode 9. 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 space. You

(LeetCode)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 ex

9 Palindrome Number(判断是否为回文数Easy)

题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 1 class Solution { 2 public: 3 bool isPalindrome(int x) { 4 if(x<0)return false; 5 if(x==0)return true; 6 int start,end,num=0,temp=x; 7 while(temp){ 8 num++; 9 temp=temp/10; 10 } 11

如何判断一个单向链表是否为回文链表(Palindrome Linked List)

题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案就是数组.遍历链表,用数组把数据存下来,然后再进行一次遍历,同时用数组反向地与之比较,这样就可以判断是否回文.这个方法时间复杂度是O(n),达到了要求,然而空间复杂度显然不满足要求.所以,开数组这一类的方法显然不是最佳的. 2,既然要满足O(1)的空间复杂度,我就想到了用一个变量来存储这些数据,恰好

使用数组判断输入的五位数是否为回文

输入一个五位正整数,使用数组判断它是不是回文数(例如12321是回文) Scanner inScanner = new Scanner(System.in);  System.out.print("请输入一个五位整数:");  String num = inScanner.next();  if (num.matches("\\d+")) { // 是否输入回车   char[] nums = num.toCharArray();  //将输入的字符串转化为一个字符