leetcode-T7 Reverse digits of an integer
2015-10-20 08:35
344 查看
题目要求:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解:
解题思路:注意两点,1、当x最后一个数是0怎么办,例如0,10,100; 2、当结果溢出,即超过int所能表示的最大范围,如1000000003,翻转后会超过INT_MAX。int型数据类型最大占4个字节,在limits.h头文件里有定义#define INT_MAX 2147483647即2^31, #define INT_MIN INT_MAX-1 。
突然想起一个比较好的例子,关于溢出问题:
unsigned short i = 0;
unsigned short a=65530,b=65530;
i=a+b;
cout<<i<<endl;
这里应该输出65524,无符号短整型最大65535,因为数据溢出后是循环增长,即65535-> 0 ->..-> 65524.
signed short i = 0;
signed short a=65530,b=65530;
i=a+b;
cout<<i<<endl
而如果换成有符号短整型,溢出后就应该是这样循环:65535->(-65536) ->(-65535) -> (-12).
下面是stackoverflow上关于int最大最小的一个问题:
Q:-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the
This will print
my testing. However, if we cast -2147483648 to integer, the result will be different:
This will print
I'm confused. Can anyone give an explanation on this?
A:
not a "number". C++ language does not support negative literal values.
actually an expression: a positive literal value
unary
in front of it. Value
apparently too large for the positive side of
on your platform. If type
the domain of larger type and the result would be negative, as one would expect.
However, apparently in your case the range of
and in general there's no integer type with greater range than
your platform. This formally means that positive constant
all available signed integer types, which in turn means that the behavior of your program is undefined. (It is a bit strange that the language specification opts for undefined behavior in such cases, instead of requiring a diagnostic message, but that's the
way it is.)
In practice, taking into account that the behavior is undefined,
get interpreted as some implementation-dependent negative value which happens to turn positive after having unary
to it. Alternatively, some implementations might decide to attempt using unsigned types to represent the value (for example, in C89/90 compilers were required to use
As a side note, this is the reason why constants like
typically defined as
instead of the seemingly more straightforward
The latter would not work as intended.
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解:
#define INT_MAX 2147483647 #define INT_MIN (-INT_MAX -1) class Solution { public: int reverse(int x) { int y = 0; int n; while (x != 0 ) { n = x % 10; if( y > INT_MAX/10 || y < INT_MIN/10) { return 0; } y = y*10 + n; x = x/10; } return y; } };
解题思路:注意两点,1、当x最后一个数是0怎么办,例如0,10,100; 2、当结果溢出,即超过int所能表示的最大范围,如1000000003,翻转后会超过INT_MAX。int型数据类型最大占4个字节,在limits.h头文件里有定义#define INT_MAX 2147483647即2^31, #define INT_MIN INT_MAX-1 。
突然想起一个比较好的例子,关于溢出问题:
unsigned short i = 0;
unsigned short a=65530,b=65530;
i=a+b;
cout<<i<<endl;
这里应该输出65524,无符号短整型最大65535,因为数据溢出后是循环增长,即65535-> 0 ->..-> 65524.
signed short i = 0;
signed short a=65530,b=65530;
i=a+b;
cout<<i<<endl
而如果换成有符号短整型,溢出后就应该是这样循环:65535->(-65536) ->(-65535) -> (-12).
下面是stackoverflow上关于int最大最小的一个问题:
Q:-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the
if(...)sentence:
if (-2147483648 > 0) std::cout << "true"; else std::cout << "false";
This will print
truein
my testing. However, if we cast -2147483648 to integer, the result will be different:
if (int(-2147483648) > 0) std::cout << "true"; else std::cout << "false";
This will print
false.
I'm confused. Can anyone give an explanation on this?
A:
-2147483648is
not a "number". C++ language does not support negative literal values.
-2147483648is
actually an expression: a positive literal value
2147483648with
unary
-operator
in front of it. Value
2147483648is
apparently too large for the positive side of
intrange
on your platform. If type
long inthad greater range on your platform, the compiler would have to automatically assume that
2147483648has
long inttype. (In C++11 the compiler would also have to consider
long long inttype.) This would make the compiler to evaluate
-2147483648in
the domain of larger type and the result would be negative, as one would expect.
However, apparently in your case the range of
long intis the same as range of
int,
and in general there's no integer type with greater range than
inton
your platform. This formally means that positive constant
2147483648overflows
all available signed integer types, which in turn means that the behavior of your program is undefined. (It is a bit strange that the language specification opts for undefined behavior in such cases, instead of requiring a diagnostic message, but that's the
way it is.)
In practice, taking into account that the behavior is undefined,
2147483648might
get interpreted as some implementation-dependent negative value which happens to turn positive after having unary
-applied
to it. Alternatively, some implementations might decide to attempt using unsigned types to represent the value (for example, in C89/90 compilers were required to use
unsigned long int, but not in C99 or C++). Implementations are allowed to do anything, since the behavior is undefined anyway.
As a side note, this is the reason why constants like
INT_MINare
typically defined as
#define INT_MIN (-2147483647 - 1)
instead of the seemingly more straightforward
#define INT_MIN -2147483648
The latter would not work as intended.
相关文章推荐
- sqlite3:基本操作
- FERET 人脸识别技术测试的简单介绍
- Qt: QT中PRO文件写法的详细介绍
- 选择排序
- 黑马程序员----多线程1(继承Thread类、实现Runnable接口)
- 开源 java CMS - FreeCMS2.4 热词管理
- Android尺子项目
- android应用默认安装路径配置
- Qt:把QString转换为 double类型
- CentOS (RHEL) 系统管理中的查看内存插槽数、最大容量和频率
- oracle 备份操作流程
- 见过的最全的iOS面试题
- Linux下目录详解
- 见过的最全的iOS面试题
- github优秀开源项目大全-iOS
- 用户请愿地址曝光 请求微软恢复Win10手动更新权限
- Linux下cat命令
- 养护脾胃要做到“三定”
- JSP page指令
- Qt:QT中PRO文件写法的详细介绍