【Leetcode-easy】Palindrome Number
思路:除和求余 取得首位和末尾 比较是否相等。
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的更多相关文章
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- C# 写 LeetCode easy #9 Palindrome Number
9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...
- 【Leetcode】【Easy】Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 判断一个整数是不是回文整数(例12321).不能使 ...
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Palindrome Partitioning
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning/ We solve this problem using D ...
- 【LEETCODE OJ】Single Number
Prolbem link: http://oj.leetcode.com/problems/single-number/ This prolbem can be solved by using XOR ...
- 【LeetCode 234】Palindrome Linked List
Given a singly linked list, determine if it is a palindrome. 思路: 用快慢指针找到链表中点,反转后半部分链表,然后与前半部分进行匹配,随后 ...
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...
- 【leetcode❤python】191. Number of 1 Bits
#-*- coding: UTF-8 -*- class Solution(object): def hammingWeight(self, n): if n<=0:retu ...
随机推荐
- 2016.6.20 eclipse中的jsp文件的字体大小在哪里修改
刚打开eclipse的时候,觉得jsp文件的字体太小了.于是去修改字体,但是colors and fonts里的字体选项太多了,不知道哪一个是. 试了几个后发现,是structured text ed ...
- android mvp高速开发框架介绍(继续dileber)
android mvp框架:dileber(https://github.com/dileber/dileber.git) 继续为大家介绍android mvp开源框架 dileber 官方交流qq群 ...
- 跳转前暂停几秒js如何实现
jquery如何实现跳转前暂停几秒 今天有个需求,类似答题的,需要显示结果后再跳转. 此处直接通过settimeout实现. 代码如下: url = 'www.baidu.com'; setTimeo ...
- jmeter之怎样减负-实现稳定超高并发測试(性能调优)
新浪围脖>@o蜗牛快跑o 在測试过程中,刚開始学习的人(也包含早期的我),使用工具不当,加入众多监控组件,很想看到实时报告.跑不了一会,jmeter就卡死甚至oom.仅仅得重新启动.之前的 ...
- Android fragment (二)
怎样使用fragment? 1.首先你要确定下你有多少个fragment要使用在一个activity里. 2.依据你的fragment的数量,创建继承自fragment的class.然后依据实际需求重 ...
- python thrift hbase安装连接
默认已装好 hbase,我的版本是hbase-0.98.24,并运行 python 2.7.x 步骤: sudo apt-get install automake bison flex g++ git ...
- SQLite 数据库安装与创建数据库
嵌入式关系数据库 Ubuntu $ sudo apt-get install sqlite3 sqlite3-dev CentOS, or Fedora $ yum install SQLite3 s ...
- LeetCode222——Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- html 锚点定位
在html中设置锚点定位我知道的有几种方法.在此和大家分享一下: 1.使用id定位: <a href="#1F" name="1F">锚点1< ...
- Android-彻底地理解Binder
转自:https://blog.csdn.net/huachao1001 https://blog.csdn.net/huachao1001/article/details/51504469 你是不是 ...