【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 ...
随机推荐
- 第十八章 Python批量管理主机(paramiko、fabric与pexpect)
这个人的文章不错:http://lizhenliang.blog.51cto.com/all/7876557 转载:http://lizhenliang.blog.51cto.com/7876557/ ...
- Codis的源码编译生成tar包
一.Go环境的安装 1.下载地址 https://golang.org/dl/2.解压 tar -zxvf go1.7.1.linux-amd64.tar.gz -C /usr/local 3.修改配 ...
- Linux学习之十八-sudo分权管理
sudo分权管理 1.为什么需要sudo? 当我的主机是多人共管的环境时,如果大家都使用 su 来切换成为 root 的身份,那么就得每个人知道 root 的密码,这样密码太多人知道可能会流出去,很不 ...
- 转:GRADLE构建最佳实践
转自: http://www.figotan.org/2016/04/01/gradle-on-android-best-practise/#section-2 随着谷歌对Eclipse的无情抛弃和对 ...
- MySQL的备份与恢复具体解释
MySQL数据备份 在mySQL里面,有逻辑备份和物理备份.逻辑备份最大长处是对于各种存储引擎,都能够使用相同的方法来备份. 而物理备份则不同.不同的存储引擎有着不同的备份方法. 逻辑备份与恢复 备份 ...
- mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]
副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了“主节点挂掉了,整个集群内会自动切换”的问题.我们来看看mongoDB副本集的架构图: 由图可以看到客户端连接到整个副本集 ...
- 常用快递API及快递在线下单API分享
1.常用快递API 支持顺丰.EMS.申通.圆通.韵达.汇通.中通.天天.德邦.全峰等主流快递公司. 文档地址:https://www.juhe.cn/docs/api/id/43 1.1常用快递查询 ...
- Linux装mysqli.so
php 5.2.3+mysqli 安装与常见错误 总结 php 5.2.3+mysqli 安装与常见错误 总结 记得原来在编译php的已经已经加上参数--with-mysql=/usr/local ...
- Win8 恢复传统启动菜单 for 多系统
如果你安装了Win7和Win8,启动的时候得先启动到Win8然后再启动到Win7,很怀念原来的启动选择器,这里给你方法了~ 当前系统是Win8的输入 bcdedit /set {current} bo ...
- VueJS样式绑定v-bind:class
class 与 style 是 HTML 元素的属性,用于设置元素的样式,我们可以用 v-bind 来设置样式属性. Vue.js v-bind 在处理 class 和 style 时, 专门增强了它 ...