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

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

(2)去前缀0

eg: reverse(1534236469); 会丢精度,如果不校验 所以WA了一次

int reverse(int x) {
long sumLong = 0;
int sum = 0;
int num = 0;
while (x!= 0) { //支持正负数
num = x % 10; //末尾数字
sum = sum * 10;//进位
sum += num;
x = x / 10;
//校验精度
sumLong = sumLong * 10;
sumLong += num;
if (sumLong != sum) {
sum = 0;
break;
}
}
return sum;
}

LeetCode 7. Reverse Integer 一个整数倒叙输出的更多相关文章

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

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

  2. [LeetCode] 7. Reverse Integer 翻转整数

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

  3. leetcode 7 reverse integer 反转整数

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

  4. JS使用Enter事件将输入的字符倒叙输出

    在JavaScript中执行当用户按下Enter键位时将用户输入的字符倒叙输出! HTML代码: <body> <form id="form1" runat=&q ...

  5. Java知识积累2-StringReverse实现文字(单词)倒叙输出

    package String; import java.util.Stack;import java.util.StringTokenizer; public class StringReverse ...

  6. python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数。如果不存在这样一对整数,则输入一条消息进行说明。

    python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数.如果不存在这样一对整数,则输入一条消息 ...

  7. 输入一个整数n,输出契波那契数列的第n项

    package bianchengti; /* * 输入一个整数n,输出契波那契数列的第n项 * 斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89... */ p ...

  8. [LeetCode]7. Reverse Integer整数反转

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

  9. 【LeetCode】1432. 改变一个整数能得到的最大差值 Max Difference You Can Get From Changing an Integer

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode ...

随机推荐

  1. 杭电 1280 前m大的数

    http://acm.hdu.edu.cn/showproblem.php?pid=1280 前m大的数 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. 《C++ Primer Plus》学习笔记 2.1.3 C++预处理器和iostream文件

    程序清单2-1 myfirst.cpp // myfirst.cpp -- displays a message #include <iostream> // a PREPROCESSOR ...

  3. jquery如何为元素设置style?

    $("#userLevelCss").attr("style","width:78%;float: right;display: none;" ...

  4. 160329(二)、web.xml配置详解

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  5. Spring的AOP-----HelloWord

    这里就一个计算器开发为例1搭建环境-搭配好Spring的AOP开发环境导入以下这些包:2建立好核心处理模块的类ArithmeticCalculator: package com.jeremy.spri ...

  6. Tips-Windows 10【多桌面视窗】操作

    Windows 10[多桌面视窗] 当你点击任务栏上的“task view”按键时,会在屏幕中间显示你当前正在使用的桌面,你可以点击“添加桌面”来创建一个新的桌面,在这个新的桌面你可以打开其他的应用程 ...

  7. synchronized和volatile的使用方法以及区别

    先看看下面的例子: public class ThreadTest { public static void main(String[] args) { final Counter counter = ...

  8. java开源模板引擎

      Velocity  Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义 ...

  9. 如何做rom,体验做rom过程,附图文教程,感谢各位romer

    http://bbs.gfan.com/android-5408130-1-1.html 有人问我,我简单的写一下,来源XDA,运行环境ubuntu 10.4. ubuntu安装很简单,在window ...

  10. 移除 URL 中的 index.php

    w 将.htaaccess 放至该站点根目录. http://codeigniter.org.cn/user_guide/general/urls.html 如果你的 Apache 服务器启用了 mo ...