zoukankan      html  css  js  c++  java
  • 9. Palindrome Number

    9. Palindrome Number

    题目

    
    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.
    
    

    解析

    // 9. Palindrome Number
    class Solution_9 {
    public:
    	bool isPalindrome(int x) { //+-处理
    		if (x<0)
    		{
    			return false; //负数不当做回文
    		}
    		if (x%10==0&&x!=0)
    		{
    			return false;
    		}
    		if (x<10)
    		{
    			return true;
    		}
    		int invertnum = 0;
    		while (x>invertnum)  //Revert half of the number
    		{
    			invertnum = invertnum * 10 + x % 10;
    			x /= 10;
    		}
    
    		return x == invertnum||x==invertnum/10; //121 ||1221
    	}
    };
    
    

    题目来源

  • 相关阅读:
    vue父子组件传值的方式
    定时任务写法
    仅仅为笔记
    consul剔除某个服务
    mybatis批量查询
    一次eureka的事故
    feign的工作原理
    JVM优化
    threadlocal应用
    秋招总结
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/8298709.html
Copyright © 2011-2022 走看看