博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 7. Reverse Integer
阅读量:6720 次
发布时间:2019-06-25

本文共 3617 字,大约阅读时间需要 12 分钟。

https://leetcode.com/problems/reverse-integer/description/

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123Output:  321

Example 2:

Input: -123Output: -321

Example 3:

Input: 120Output: 21

Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

  • 简单题,类似字符串处理。主要是考虑integer overflow情况。比较tricky的方法是用long来存储result结果,然后判断是否超过INT_MAX/INT_MIN。
  • 我用的还是判断overflow。由于输入不可能是INT_MAX/INT_MIN的reverse,所以不需要考虑abs(INT_MIN) > abs(INT_MAX)的情况。
1 // 2 //  main.cpp 3 //  LeetCode 4 // 5 //  Created by Hao on 2017/3/16. 6 //  Copyright © 2017年 Hao. All rights reserved. 7 // 8  9 #include 
10 #include
11 #include
12 using namespace std;13 14 class Solution {15 public:16 int reverse(int x) {17 int result = 0;18 int sign = 1;19 20 if (x < 0) {21 sign = -1;22 x = abs(x);23 }24 25 while (x != 0) {26 if (result > (INT_MAX - x % 10) / 10) // it's ok coz input would not be reverse of INT_MIN27 return 0;28 29 result = result * 10 + x % 10;30 x /= 10;31 }32 33 return result * sign;34 }35 };36 37 int main(int argc, char* argv[])38 {39 Solution testSolution;40 string result;41 42 vector
iVec = {
123, -123, 120, 2147483647, -2147483648, 0};43 44 /*45 32146 -32147 2148 049 050 051 */52 for (auto i : iVec) {53 cout << testSolution.reverse(i) << endl;54 }55 56 return 0;57 }
View Code

  • 一种解法同上,字符串处理。注意Python3中整除用//,而不是/。
  • 第二种解法利用Python sliding s[::-1]做字符串反转。
  • Python3 字符串 | 菜鸟教程
    • http://www.runoob.com/python3/python3-string.html
  • python - Maximum and Minimum values for ints - Stack Overflow
    • https://stackoverflow.com/questions/7604966/maximum-and-minimum-values-for-ints
    • Python 3
      • In Python 3, this question doesn't apply. The plain int type is unbounded.
      • However, you might actually be looking for the machine's . That's still available in Python 3 as sys.maxsize.
    • Python 2
      • In Python 2, the maximum value for plain int values is available as sys.maxint:
      • >>> sys.maxint 9223372036854775807
    • You can calculate the minimum value with -sys.maxint - 1 as shown .
    • Python seamlessly switches from plain to long integers once you exceed this value. So most of the time, you won't need to know it. 
1 class Solution: 2     def reverse(self, x: int) -> int: 3         result = 0 4         sign = 1 5          6         if x < 0: 7             sign = -1 8             x = abs(x) 9             10         while x != 0:11             if result > ( 2**31 - ( x % 10 ) ) // 10: # be careful of floor division (integer division)12                 return 013             14             result = result * 10 + x % 1015             x //= 10 # be careful of floor division (integer division)16         17         return result * sign18     19     def reverse2(self, x: int) -> int:    20         # [begin:end:step] - leaving begin and end off and specifying a step of -1, it reverses a string.21         s = str( abs( x ) )22         x_reverse = int( s[ ::-1 ] ) if x > 0 else (- int( s[ ::-1 ] ) )23         24         if ( ( x_reverse >= - 2**31 ) and ( x_reverse <= 2**31 - 1 ) ):25             return x_reverse26         else:27             return 0
View Code

 

转载于:https://www.cnblogs.com/pegasus923/p/8418953.html

你可能感兴趣的文章
浅析Objective-C字面量
查看>>
[Lua] Lua学习笔记(二) Lua语法简述(一)
查看>>
struts2的迭代
查看>>
三台机器实现免秘钥分发
查看>>
基于mongodb+node express的增删查改(CRUD)操作
查看>>
一句代码搞定点击空白处收键盘
查看>>
PHP动态属性和stdclass
查看>>
IBM P570查看配置
查看>>
如何在现有Fabric网络上添加一个Org?
查看>>
负载均衡集群介绍、LVS介绍、LVS调度算法、LVS NAT模式搭建
查看>>
Nginx服务监控
查看>>
C++一些标准模板容器简要介绍(2)
查看>>
博客测试
查看>>
dovecot并发数造成foxmail、outlook等客户端工具接收邮件有时候报错
查看>>
进程管理工具的使用
查看>>
mybatis第三天 小结
查看>>
phoneGap插件开发-多参数和返回值
查看>>
检测Gps和网络定位权限
查看>>
maven中使用springboot返回jsp和json数据
查看>>
GRASP设计模式
查看>>