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,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- eclipse打开jsp的方式怎么设置成默认
https://jingyan.baidu.com/article/4ae03de34137be3eff9e6b93.html
- jQuery prop() 与 removeProp()源码解读
prop() prop: function( elem, name, value ) { var ret, hooks, notxml, nType = elem.nodeType; // don't ...
- Ubuntu 解决E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
今天想在ubuntu上配置nodejs的环境.结果用apt install安装软件时出现报错. $ sudo apt-get update 会得到下面错误提示: E: 无法获得锁 /var/lib/a ...
- DBAplus社群线上分享----Sharding-Sphere之Proxy初探
功能 Cobar Mycat Heisenberg Shark TDDL Sharding-JDBC 是否开源 开源 开源 开源 开源 部分开源 开源 架构模型 Proxy架构 Proxy架构 Pro ...
- handler 方法进不去,服务器上出现应用程序错误。此应用程序的当前自定义错误设置禁止远程查看
HTTP/1.1 500 Internal Server ErrorCache-Control: privateContent-Type: text/html; charset=utf-8Server ...
- C语言abs函数
C语言编程入门教程 - abs 函数是用来求整数的绝对值的. //函数名:abs //功 能:求整数的绝对值 //用 法:int abs(int i); //程序例: #include<stdi ...
- android textview添加滚动条
给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...
- Git 学习教程【转+总结】
之前是在用SVN,现在因为小伙伴比较喜欢Git,所以也开始学习Git,很感谢 时光穿梭机 - 廖雪峰 的无私奉献.本文用来记录我在学习Git过程中的收获和笔记,廖雪峰大神的Git教程参考这里. 1.G ...
- GNU make(2)
GNU make(2) 参考: GNU Make学习总结(二) 变量 变量由一个前导符号$加上字符或者是括号字符组成, 名称区分大小写. 命名: 习惯上用全部大写字符表示常量, 小写字符表示变量, 单 ...
- (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql
http://blog.csdn.net/yerenyuan_pku/article/details/71893689 前面对MyBatis框架的学习中,我们对Mapper.xml映射文件多少有些了解 ...