题目:

将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数)。

样例

给定 x = 123,返回 321

给定 x = -123,返回 -321

解题:

直接反转,越界处理好炒蛋

Java程序:

public class Solution {
/**
* @param n the integer to be reversed
* @return the reversed integer
*/
public int reverseInteger(int n) {
// Write your code here
int MAX = Integer.MAX_VALUE;
if(n>=0){
int res = 0;
int num = n;
while(n!=0){
if(res>MAX/10) return 0;
res =res *10 + n%10;
n = n/10;
}
return res;
}else{
int res = reverseInteger(-n);
return -res;
} }
}

总耗时: 16030 ms

Python程序:

还没好,一直Pending,Python不需要处理越界问题,

需要处理,上面说的是32位数,最大值是2的32次方,下面程序已经更改,可以AC

class Solution:
# @param {int} n the integer to be reversed
# @return {int} the reversed integer
def reverseInteger(self, n):
# Write your code here
MAX = 2147483647
flag = False
if n<0:
n = -n
flag = True
res = 0
while n!=0:
if res>MAX/10: return 0
res = res * 10 + n%10;
n = n/10
if flag:
return -res
else:
return res

总耗时: 650 ms

lintcode :reverse integer 颠倒整数的更多相关文章

  1. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  2. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  3. [leetcode]7. Reverse Integer反转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  4. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  5. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  6. 7. Reverse Integer[E]整数反转

    题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...

  7. [LeetCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...

  8. leetcode 7 reverse integer 反转整数

    描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...

  9. LeetCode 7. Reverse Integer 一个整数倒叙输出

    潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...

随机推荐

  1. Linux--变量与虚拟内存

    定义一个变量:存储类型  数据类型  变量名 存储类型(变量存储的位置):auto.register.static.extern 1.auto:对于局部变量,atuo可以缺省.位置:栈 2.exter ...

  2. C++二维数组动态内存分配

    对于二维数组和二维指针的内存的分配 这里首选说一下一维指针和一维数组的内存分配情况. 一维: 数组:形如int  a[5];这里定义了一个一维数组a,并且数组的元素个数是5,这里的a是这五个元素的整体 ...

  3. Windows 8.1 序列化与反序列化

    /// <summary> /// 对象序列化成 XML String /// </summary> public static void XmlSerialize<T& ...

  4. 利用Apply的参数数组化来提高代码的优雅性,及高效性

    利用Apply的参数数组化来提高代码的优雅性,及高效性 Function.apply()在提升程序性能方面的技巧 我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所 ...

  5. 浅析IList与List的区别

    List和IList是.net开发中经常遇到的两种类型,用法上经常会让初学者摸不到头脑,下面简要的分析一下这两种类型的区别 1.IList<T>是接口,继承自ICollection< ...

  6. Spark 大数据平台

    Apache Spark is an open source cluster computing system that aims to make data analytics fast - both ...

  7. JDBC 基本操作: CRUD

    Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可. Statement对象的executeUpdate方法,用于向 ...

  8. python之函数式编程

    python提供了支持函数式编程的简单机制: 1. map函数 2. filter函数 3. reduce函数. 典型的M/R计算模型. 但还是有点简单...

  9. ThinkPHP/Common/extend.php

    <?php // +---------------------------------------------------------------------- // | ThinkPHP [ ...

  10. Jquery post 传递数组给asp.net mvc方法

    以批量删除数据为例  做批量删除会需要传递要删除的数据ID数组 function RemoveLog(){ var postModel=[]; //遍历复选框获取要删除的数据ID 存放到数组中  $( ...