思路:除和求余 取得首位和末尾 比较是否相等。

     public boolean isPalindrome(int x){
if(x<0){
return false;
}
int div=1;
while(x/div>=10){
div*=10;
}
while(x!=0){
int left=x/div;
int right=x%10;
if(left!=right){
return false;
}
x=(x%div)/10;//去掉最高位和最低位
div/=100;
}
return true;
}

其他思路:http://m.blog.csdn.net/blog/yike1207/44307819博文总结了几种方法(整数反转、转化为字符串处理,取高低位比较)该作者认为上述方法都用到了额外的存储空间,不用额外的存储空间一种方法是通过递归,并修改引用参数值。

 bool isPalindrome(int x, int &y) {
// y为必须为引用和指针型可改变的变量
if (x < 0) return false;
if (x == 0) return true;
if (isPalindrome(x/10, y) && (x%10 == y%10)) {
// 每次执行到 x%10 == y%10 的时候 x 为前i位,y表示前(n+1-i)位
y /= 10;
return true;
} else {
return false;
}
}
bool isPalindrome(int x) {
return isPalindrome(x, x);
}

【Leetcode-easy】Palindrome Number的更多相关文章

  1. 【Leet Code】Palindrome Number

    Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...

  2. C# 写 LeetCode easy #9 Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  3. 【Leetcode】【Easy】Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...

  4. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  5. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  6. 【LeetCode OJ】Palindrome Partitioning

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...

  7. 【LEETCODE OJ】Single Number

    Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...

  8. 【LeetCode 234】Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后 ...

  9. 【LeetCode #179】Largest Number 解题报告

    原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...

  10. 【leetcode❤python】191. Number of 1 Bits

    #-*- coding: UTF-8 -*- class Solution(object):    def hammingWeight(self, n):        if n<=0:retu ...

随机推荐

  1. [field:description /]标签如何限制字数?|DedeCms

    [field:description /]标签如何限制字数? [field:description function='cn_substr(@me,80)'/]dede 里的所有标记都支持这样使用函数 ...

  2. ASP.NET Web API是如何根据请求选择Action的?[下篇] 【转】

    再<上篇>中我们简单介绍了用于实现Action选择机制的HttpActionSelector,接下来我们来讨论本章最为核心的内 容:ASP.NET Web API如何利用HttpActio ...

  3. masm学习

    . .model flat,stdcall option casemap:none ; case sensitive ;http://www.popbook.com/wbbs/topic.asp?l_ ...

  4. 关于 Shiro 的权限匹配器和过滤器

    项目源码:https://github.com/weimingge14/Shiro-project演示地址:http://liweiblog.duapp.com/Shiro-project/login ...

  5. 【音乐App】—— Vue-music 项目学习笔记:搜索页面开发

    前言:以下内容均为学习慕课网高级实战课程的实践爬坑笔记. 项目github地址:https://github.com/66Web/ljq_vue_music,欢迎Star. 搜索歌手歌曲 搜索历史保存 ...

  6. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...

  7. TCP/IP详解 卷一(第一章 概述)

    很多不同的厂家生产各种型号的计算机,它们运行完全不同的操作系统,但TCP/IP协议族允许它们相互进行通信. 1.分层 TCP/IP不是一个协议,而是一个协议族,通常它被认为是一个四层的协议系统,下面展 ...

  8. 【Excle数据透视表】如何快速选取所有标签并标注黄色底纹

    如下图:需要把所有标签标注为黄色底纹该如何操作呢? 步骤 单击数据透视表任意单元格→数据透视表工具→分析→选择→整个数据透视表→选择→标签→开始→字体组合中"填充颜色" 第一次选择 ...

  9. 动态载入Layout 与 论Activity、 Window、View的关系

    1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  10. Vue.js 很好,但会比 Angular 或 React 更好吗?

    文章转自:http://www.oschina.net/translate/vuejs-is-good-but-is-it-better-than-angular-or-rea Vue.js 是一个用 ...