题目:

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

样例

给定 x = 123,返回 321

给定 x = -123,返回 -321

解题:

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

Java程序:

  1. public class Solution {
  2. /**
  3. * @param n the integer to be reversed
  4. * @return the reversed integer
  5. */
  6. public int reverseInteger(int n) {
  7. // Write your code here
  8. int MAX = Integer.MAX_VALUE;
  9. if(n>=0){
  10. int res = 0;
  11. int num = n;
  12. while(n!=0){
  13. if(res>MAX/10) return 0;
  14. res =res *10 + n%10;
  15. n = n/10;
  16. }
  17. return res;
  18. }else{
  19. int res = reverseInteger(-n);
  20. return -res;
  21. }
  22.  
  23. }
  24. }

总耗时: 16030 ms

Python程序:

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

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

  1. class Solution:
  2. # @param {int} n the integer to be reversed
  3. # @return {int} the reversed integer
  4. def reverseInteger(self, n):
  5. # Write your code here
  6. MAX = 2147483647
  7. flag = False
  8. if n<0:
  9. n = -n
  10. flag = True
  11. res = 0
  12. while n!=0:
  13. if res>MAX/10: return 0
  14. res = res * 10 + n%10;
  15. n = n/10
  16. if flag:
  17. return -res
  18. else:
  19. 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. [DevExpress]ChartControl之时间轴示例

    关键代码: using System; using System.Data; using System.Windows.Forms; using DevExpress.XtraCharts; name ...

  2. JavaScript 中的 replace 方法

    定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replaceme ...

  3. AJAX局部更新演出排期

    <script language="javascript" type="text/javascript"> function createXMLHt ...

  4. Oracle 表的连接方式(2)-----HASH JOIN的基本机制3

    HASH JOIN的模式 hash join有三种工作模式,分别是optimal模式,onepass模式和multipass模式,分别在v$sysstat里面有对应的统计信息: SQL> sel ...

  5. linux下查看进程内存使用情况

    1. top命令--动态查看一个进程的内存使用top -d 1 -p pid [,pid ...]  //设置为delay 1s,默认是delay 3s  如果想根据内存使用量进行排序,可以shift ...

  6. Png图片的透明部分穿透测试

           private void Window_MouseMove(object sender, MouseEventArgs e){ NavBtnList.Clear(); Point mou ...

  7. Linux内核学习方法

    Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天就是逃课,上网,玩游戏,睡觉.毕业的时候,人家跟我说Makefile我完全不知,但是一说Make Love我就来劲了 ...

  8. http概述

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...

  9. 如何设置让SFTP的用户限制在某个目录下

    通常SFTP的任何用户登录之后能看到整个系统的文件目录,这样很不安全. 通过chroot我们可以将某个用户登录SFTP后只能在某个限定的目录下操作,这样可以更安全.我们来看看怎么设置. 1.创建一个用 ...

  10. auto和decltype

    auto 1.编译器通过分析表达式的类型来确定变量的类型,所以auto定义的变量必须有初始值. auto i=; //ok,i为整型 auto j; //error,定义时必须初始化. j=;     ...