题目

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. 使用git管理代码

    上传工程 1.登录github后,点击右上角带有+号的图标,输入仓库名创建仓库(Repository). 2.在项目文件夹下执行以下命令: touch README.md git init 如果工程中 ...

  2. uliweb的模版

    uliweb模版的文件名是与函数名相同的 以test为例: ***@Android:~/ablog# vim apps/blog/templates/test.html 编辑test.html的内容 ...

  3. 20155212 2016-2017-2 《Java程序设计》第9周学习总结

    20155212 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 Chapter16 数据库本身是个独立运行的应用程序. 应用程序如何呼叫这组链接库? 不同的 ...

  4. 在CMD 中,如何切换python2.x 版本 和 python3.x版本?

    1.cmd中输入python,启动python2.x版本,如何切换呢? 2.找到python2.x安装路径,修改python.exe名称为:python27.exe 3. 打开cmd,输入python ...

  5. Codeforces 914 C 数位DP+暴力打表+思维

    题意 给出一个二进制数\(n\),每次操作可以将一个整数\(x\)简化为\(x\)的二进制表示中\(1\)的个数,如果一个数简化为\(1\)所需的最小次数为\(k\),将这个数叫做特殊的数, 问从\( ...

  6. [Vue warn]:vue-Failed to resolve directive: clipboard

    前言 需求:移动端需要一个按钮,复制到剪切板,分享给好友(没有调用微信内置的分享接口) 插件 vue-clipboard2 环境:vue,node 安装:npm install --save vue- ...

  7. flask 实现异步非阻塞----gevent

    我们都知道,flask不支持异步非阻塞的请求,我们可以创建一个新项目去测试一下,推荐大家使用pycharm去开发我们的flask  使用特别的方便. rom flask import Flask im ...

  8. WebGL2系列之实例数组(Instanced Arrays)

    实例化数组 实例化是一种只调用一次渲染函数却能绘制出很多物体的技术,它节省渲染一个物体时从CPU到GPU的通信时间.实例数组是这样的一个对象,使用它,可以把原来的的uniform变量转换成attrib ...

  9. 剑指offer试题(PHP篇三)

    21.栈的压入.弹出序列 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4 ...

  10. 【坚持】Selenium+Python学习记录 DAY8

    2018/05/ 28 [来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html) 继续敲类相关的代码 #No.1 class peo ...