题目

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. 20155236范晨歌_EXP3免杀原理与实践

    20155236范晨歌_免杀原理与实践 免杀 概述 免杀,也就是反病毒(AntiVirus)与反间谍(AntiSpyware)的对立面,英文为Anti-AntiVirus(简写Virus AV),逐字 ...

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

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

  3. BZOJ1787_紧急集合_KEY

    题目传送门 LCA,对于每一个(x,y,z),两两求LCA得最优解或求出LCA不同于其他两组的那个为最优解. code: /************************************** ...

  4. c++ 自定义类型,函数指针类型

    用typedef定义函数指针类型 -函数指针和函数指针数组 46课里边有如下代码 int add(int a,int b,int d) { return a+b+d; } int mul(int a, ...

  5. 【转载】MSXML应用总结 开发篇(上)

    原文:http://blog.sina.com.cn/s/blog_48f93b530100ejv9.html 本篇是接前文“MSXML应用总结 概念篇”写的,主要总结一下MSXML DOM接口的应用 ...

  6. 23-[jQuery]-效果:隐藏,淡出,盒子高度,动画

    1.隐藏,显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. OpenStack入门篇(二十一)之VXLAN原理

    1.Vxlan的概念 VXLAN 全称 Virtual eXtensible Local Area Network.(虚拟扩展本地局域网)VXLAN 提供与 VLAN 相同的以太网二层服务,但是拥有更 ...

  8. Openstack入门篇(十六)之Cinder服务的部署与测试

    1.理解块存储服务 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,SCSI,SAN,iSCSI 等)挂接裸硬盘,然后分区.格式化.创建文件系统:或者直接使用裸硬盘存储数据(数据库) 通 ...

  9. Codeforces 937 D. Sleepy Game(DFS 判断环)

    题目链接: Sleepy Game 题意: Petya and Vasya 在玩移动旗子的游戏, 谁不能移动就输了. Vasya在订移动计划的时候睡着了, 然后Petya 就想趁着Vasya睡着的时候 ...

  10. [Mark]Windows Server 2008 R2 防火墙之SQL Server 2008 R2

    今天新装了一个DBServer (Windows Server 2008 R2),但是在客户端跑之前的应用时却发现出错了,Debugg后发现的错误如下:   由InnerException可知时DB ...