zoukankan      html  css  js  c++  java
  • 回文数

    回文数

    题目:https://leetcode-cn.com/problems/palindrome-number/description/

    package com.test;
    
    public class Lesson003 {
        public static void main(String[] args) {
            int i = 121;
            boolean isHuiwen = judgeHuiwen(i);
            System.out.println(isHuiwen);
        }
    
        private static boolean judgeHuiwen(int x) {
            // 负数不回文
            if (x < 0) {
                return false;
            }
            // 10以内回文,0也是回文
            if (x < 10) {
                return true;
            }
            // 末尾是0不回文
            if ((x ^ 0) == 0) {
                return false;
            }
            // 最多10位数
            int[] arr = new int[10];
            int index = 0;
            while (true) {
                int i1 = x % 10;
                arr[index] = i1;
                index++;
                x = x / 10;
                if (x < 10) {
                    break;
                }
            }
            arr[index] = x;
            for (int j = 0; j <= index; j++) {
                // 首尾不相等就返回false
                if (arr[j] - arr[index - j] != 0) {
                    return false;
                }
                // 到了中心点就返回true
                if (j >= index / 2) {
                    return true;
                }
            }
            return true;
        }
    }
  • 相关阅读:
    CSP游戏 4
    CSP 交通规划
    CSP 地铁修建
    CSP 通信网络
    CSP URL映射
    CSP 权限查询
    CSP Markdown
    CSP JSON 查询
    SQL里的子查询
    SQL里的操作符
  • 原文地址:https://www.cnblogs.com/stono/p/9457732.html
Copyright © 2011-2022 走看看