zoukankan      html  css  js  c++  java
  • leetcode — palindrome-number

    import org.lep.leetcode.parseint.IntegerParser;
    
    /**
     * Source : https://oj.leetcode.com/problems/palindrome-number/
     *
     * Created by lverpeng on 2017/7/5.
     *
     * 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.
     *
     */
    public class IntPalindrome {
    
        /**
         * 负数可以自己决定是否进行判断,这里不进行判断,归为不是
         * 依次取出整数的第一位和最后一位比较
         * 第一位:整除
         * 最后一位:对10求余
         *
         * @param num
         * @return
         */
        public boolean isPalindrome (int num) {
            if (num < 0) {
                return false;
            }
            int base = 1;
            while (num / (base) >= 10) {
                base *= 10;
            }
    
            while (num != 0) {
                int left = num / base;
                int right = num % 10;
                if (left != right) {
                    return false;
                }
                num = (num % base) / 10;
                base /= 100;
            }
            return true;
        }
    
    
        public static void main(String[] args) {
            IntPalindrome palindrome = new IntPalindrome();
            System.out.println(palindrome.isPalindrome(0));
            System.out.println(palindrome.isPalindrome(-101));
            System.out.println(palindrome.isPalindrome(1001));
            System.out.println(palindrome.isPalindrome(1234321));
            System.out.println(palindrome.isPalindrome(2147447412));
            System.out.println(palindrome.isPalindrome(5155));
            System.out.println(palindrome.isPalindrome(5552));
        }
    }
    
  • 相关阅读:
    C++设计模式——代理模式
    C++设计模式——享元模式
    C++设计模式——外观模式
    C++设计模式——装饰模式
    C++设计模式——组合模式
    C++设计模式——桥接模式
    C++设计模式——适配器模式
    C++设计模式——原型模式
    云服务器和虚拟主机的区别
    ES6的Module系统
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7271537.html
Copyright © 2011-2022 走看看