491. Palindrome Number【easy】
Check a positive number is a palindrome or not.
A palindrome number is that if you reverse the whole number you will get exactly the same number.
Notice
It's guaranteed the input number is a 32-bit integer, but after reversion, the number may exceed the 32-bit integer.
11
, 121
, 1
, 12321
are palindrome numbers.
23
, 32
, 1232
are not palindrome numbers.
解法一:
class Solution {
public:
/*
* @param num: a positive number
* @return: true if it's a palindrome or false
*/
bool isPalindrome(int num) {
//negative number
if(num < )
return false; int len = ;
while(num / len >= )
len *= ; while(num > ) { //get the head and tail number
int left = num / len;
int right = num % ; if(left != right)
return false;
else {
//remove the head and tail number
num = (num % len) / ;
len /= ;
}
} return true;
}
};
分别过滤出头尾2个数进行比较
解法二:
class Solution {
public:
/*
* @param num: a positive number
* @return: true if it's a palindrome or false
*/
bool isPalindrome(int num) {
if (num < )
return false; if (num < )
return true; int digits = ;
int t = num;
int d = ;
while(t != ) t /= , ++d; int left = pow(, d - );
int right = ;
while( left >= right)
{
if (num / left % != num / right % )
return false; left /= ;
right *= ;
}
return true;
}
};
依旧是过滤出头尾2个数进行比较,处理的方式和解法一稍有不同,参考@MagiSu 的代码
491. Palindrome Number【easy】的更多相关文章
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 82. Single Number【easy】
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1,2,2,1,3,4, ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 605. Can Place Flowers【easy】
605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...
- 485. Max Consecutive Ones【easy】
485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...
随机推荐
- ecshop ecmall shopex
ecshop 是一个B2C商城 适合企业及个人快速构建个性化网上商店.系统是基于PHP语言及MYSQL数据库构架开发的跨平台开源程序.(如沃购网) 山大路是dedecms ecmall(ECMall ...
- 一、Instrument之Core Animation工具
一.Instrument 三个方法: (1).按下Command + I打开Instrument; (2).Xcode->product->profile; (3).Xcode->O ...
- ylbtech-LanguageSamples-Indexers_2(索引器)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers_2(索引器) 1.A,示例(Sample) 返回顶部 Indexers ...
- iOS:进度条控件的详细使用
进度条控件:UIProcessView:UIView 功能:顾名思义,用来显示下载进度或者传输数据进度. 属性: @property(nonatomic) UIProgressViewStyl ...
- third-maximum-number
https://leetcode.com/problems/third-maximum-number/ // 开始我以为相同的也占一位,比如5,3,3,2,得出3,但是答案是需要2 public cl ...
- Android studio 和 Eclipse快捷键对比
操作 studio eclipse debug/run 计算变量值 alt+F8 ctrl+shift+I 跳到下一步 F8 F6 跳到下一个断点 shift+F8 F8 进入到代码 F7 F5 ...
- Python 面试中 8 个必考问题(转载)
Python 面试中 8 个必考问题 1.下面这段代码的输出结果是什么?请解释. def extendList(val, list=[]): list.append(val) return list ...
- 该登录名来自不受信任的域,不能与 Windows 身份验证一起使用。
使用sql2008 远程连接数据库的时候遇到了这个问题,我用的是ADO.NET 实体数据模型,有app.config和web.config 解决了好久,因开始以为是sql的权限问题.后来解决了只需 ...
- DotNetBar ComboBoxEx
DotNetBar ComboBoxEx using System; using System.ComponentModel; using System.Drawing; using System.R ...
- [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers
Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...