zoukankan      html  css  js  c++  java
  • [LeetCode]-009-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 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.

    题目:判断一个整数是否是回文的

    1、负数可以是回文的吗 负数不是回文整数
    2、如果你想把整形转换成字符串来处理,注意空间限制
    3、你也可以反转整数,但是可能出现溢出的情况

    Test  cases:

    -2147483648
    =>false
    -1
    =>false
    212
    =>true
    3
    =>true

     1 public class Solution{
     2     public boolean isPalindrome(int x) {
     3         if(x<0)
     4             return false;
     5         if(x>=0 && x<=9)
     6             return true;
     7         int len = (x + "").length();
     8         int[] arr = new int[len];
     9         int index = 0;
    10         while(x>0){
    11             arr[index++] = x%10;
    12             x = x/10;
    13         }
    14         System.out.println(Arrays.toString(arr));
    15         int middle = (len+1)/2;
    16         boolean flag = true;
    17         for(index=0; index<middle; index++){
    18             if(arr[index] != arr[len-1-index]){
    19                 flag = false;
    20             }
    21         }
    22         return flag;
    23     }
    24     
    25     public static void main(String[] args){
    26         int param = 5486;
    27         if(args.length!=0)
    28             param = Integer.valueOf(args[0]);
    29         Solution solution = new Solution();
    30         boolean res = solution.isPalindrome(param);
    31         System.out.println(res);
    32     }
    33 }
  • 相关阅读:
    机器学习作业12--朴素贝叶斯-垃圾邮件分类
    机器学习作业11--分类与监督学习,朴素贝叶斯分类算法
    机器学习作业9--主成分分析
    机器学习作业8--特征选择
    机器学习作业7--逻辑回归实践
    机器学习作业6--逻辑回归
    实验五 单元测试
    实验二 结对编程 第二阶段
    实验二 结对编程第一阶段
    实验一 GIT代码版本管理
  • 原文地址:https://www.cnblogs.com/lianliang/p/5396311.html
Copyright © 2011-2022 走看看