LeetCode(7)Reverse Integer
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
分析:
#define INT_MAX 2147483647 /* maximum (signed) int value */
溢出判断:
bool overflow(int x)
{
if (x / 1000000000 == 0) // x的绝对值小于1000000000, 不越界
{
return false;
} else if (x == INT_MIN) // INT_MIN反转后越界,也没法按下述方法取绝对值(需要特判),直接返回true
{
return true;
}
x = abs(x);
// x = d463847412 -> 2147483647. (参数x,本身没有越界,所以d肯定是1或2)
// or -d463847412 -> -2147483648.
for (int cmp = 463847412; cmp != 0; cmp/=10, x/=10)
{
if ( x%10 > cmp%10 )
{
return true;
} else if (x%10 < cmp%10)
{
return false;
}
} return false;
}
AC代码:
class Solution
{
public:
int reverse(int x)
{
if( overflow(x) == true)
{
return 0;
} int result = 0; while (x!=0)
{
result = 10*result + x%10;
x /= 10;
} return result;
}
private:
bool overflow(int x)
{
if (x / 1000000000 == 0) // x的绝对值小于1000000000, 不越界
{
return false;
} else if (x == INT_MIN) // INT_MIN反转后越界,也没法按下述方法取绝对值(需要特判),直接返回true
{
return true;
}
x = abs(x);
// x = d463847412 -> 2147483647. (参数x,本身没有越界,所以d肯定是1或2)
// or -d463847412 -> -2147483648.
for (int cmp = 463847412; cmp != 0; cmp/=10, x/=10)
{
if ( x%10 > cmp%10 )
{
return true;
} else if (x%10 < cmp%10)
{
return false;
}
} return false;
}
};
LeetCode(7)Reverse Integer的更多相关文章
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode(47)-Reverse Bits
题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(190) Reverse Bits
题目 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...
- LeetCode(151) Reverse Words in a String
题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...
- LeetCode(206) Reverse Linked List
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...
- LeetCode(92) Reverse Linked List II
题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1- ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- Codeforces Round #546 (Div. 2) B. Nastya Is Playing Computer Games
链接:https://codeforces.com/contest/1136/problem/B 题意: 有n个井盖,每个井盖上有一个小石头. 给出n和k,k表示刚开始在第k个井盖上方. 有三种操作, ...
- jQuery position() 源码解读
position的代码比较简单... position: function() { if ( !this[ 0 ] ) { return; } var offsetParent, offset, el ...
- 渣渣菜鸡为什么要看 ElasticSearch 源码?
前提 人工智能.大数据快速发展的今天,对于 TB 甚至 PB 级大数据的快速检索已然成为刚需,大型企业早已淹没在系统生成的浩瀚数据流当中.大数据技术业已集中在如何存储和处理这些海量的数据上.Elast ...
- [android][avd位置修改][需自己手动]
1.找到avd目录(C:\的那个)并打开,移动.avd文件到需要的位置. 2.修改ini文件path,完成.
- Sass基本特性
Sass扩展/继承@extend 代码的继承,声明方式:.class;调用方式:@extend 如: .btn { border: 1px solid #ccc; padding: 6px 10px; ...
- 安卓ListView基础应用
listview简单描述 主页面: package com.example.listview; import com.lidroid.xutils.ViewUtils; import com.lidr ...
- iOS 画圆图片的几种方法
方法一: self.cycleImv= [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)]; [self.view addS ...
- moment算本月开始日期和结束日期
moment算本月开始日期和结束日期 1.引入moment.js var vStartDate=new moment().add('month',addMonth).format("YYYY ...
- Windwos下安装和使用MongoDB
1)下载安装包下载路径:https://www.mongodb.com/download-center#community包名称:mongodb-win32-x86_64-3.4.10-signed. ...
- cookie安全
www.baidu.com host 文件 定义 a.baidu.com 为127.0.01 本地编写php程序 读取浏览器发送给 a.baidu.com的cookie 会把 .baidu.com域下 ...