题目

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Note:

The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

翻译

很简单的整数倒置 需要注意的是符号和范围

利用Python则很简单

Hints

Related Topics: Math

代码

Java

class Solution {
public int reverse(int x) {
long result = 0;
while(x!=0){
result = result*10+x%10;
if(result>Integer.MAX_VALUE||result<Integer.MIN_VALUE)
return 0;
x = x/10;
}
return (int)result;
}
}

Python

class Solution(object):
def reverse(self, x):
sign = 1 if x>=0 else -1
x = x*sign
xs = str(x)
ys = xs[::-1] y = int(ys)*sign
if y>2**31-1 or y<-2**31:
return 0
return y

蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]的更多相关文章

  1. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  2. 蜗牛慢慢爬 LeetCode 1.Two Sum [Difficulty: Easy]

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  3. 蜗牛慢慢爬 LeetCode 20. Valid Parentheses [Difficulty: Easy]

    题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...

  4. 蜗牛慢慢爬 LeetCode 25. Reverse Nodes in k-Group [Difficulty: Hard]

    题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...

  5. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

    题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  7. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. 蜗牛慢慢爬 LeetCode 36.Valid Sudoku [Difficulty: Medium]

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  9. 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]

    题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...

随机推荐

  1. 《Java 程序设计》实验报告汇总

    <Java 程序设计>实验报告汇总 20145207<Java程序设计>实验一 (Java开发环境的熟悉)实验报告 20145207<Java程序设计>实验二 (J ...

  2. OSG环境变量设置

    osg中需要设置一些环境变量设置,如 OSG_FILE_PATH:此变量设置模型数据的目录 OSG_SCREEN: 此变量设置显示模型是在单屏幕还是多屏幕,1为单屏幕 OSG_WINDOW: 此变量设 ...

  3. UEditor富文本web编辑器

    UEditor是由百度web前端研发部开发所见即所得,前几天把公司原来的富文本编辑器换成百度的了,可以把秀米制作的样式 整个复制到文本编辑器中,原汁原味... 到官网看了文档,其实很简单,就简单的配置 ...

  4. 2555: SubString

    2555: SubString 链接 题意: 动态在末尾加入一个字符串,询问一个字符串出现了多少次. 分析: 如果没有动态加入,那么建出SAM后,求出parent树上,每个点|Right|,然后走一遍 ...

  5. Security Permissions Caching

    Security Permissions Caching Security permission caching is implemented in Security Adapters - class ...

  6. 如何为一个高负荷站点配置tomcat连接器(connector)【译文】(第一篇)

    引言 最近正好要用到这些内容,因此就找了一篇比较有分量的文章,思来想去,还是尝试写一下译文吧.其实LZ的英语是非常烂的(四级没过的LZ眼泪掉下来),因此这篇文章翻译的水平LZ自己也不敢恭维.各位猿友大 ...

  7. linux 利用cat写入一段文件

    linux 利用cat写入一段文件 cat >> /etc/rc.local <<EOFsysctl -w net.ipv4.icmp_echo_ignore_all=1sys ...

  8. SQL常见面试题

    1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name   kecheng   fenshu张三    语文       81张三     数学       75李四     语文   ...

  9. Scrapy爬取携程桂林问答

    guilin.sql: CREATE TABLE `guilin_ask` ( `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键', `question ...

  10. Win环境 Android Studio使用Git 教程 ( 一 )

    一. 安装 下载安装完成Git后 进入命令行 输入命令git --version,如果能显示版本则说明安装成功,如果没有显示版本,需要配置环境变量: 在path中添加git的安装位置 二 . 配置信息 ...