leetCode(62)-Reverse Integer

时间:2023-01-03 22:12:15

题目:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

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.

Update (2014-11-10):
Test cases had been added to test the overflow behavior.

思路:

  • 题意:反转整数
  • 注意两点:1.负数的情况2.溢出返回0(绝对值大于Integer.MAX_VALUE)

代码:

public class Solution {
    public int reverse(int x) {
        boolean flag = true;
        if(x < 0){
            flag = false;
            x = x*-1;
        }
        long res = 0;
        while(x > 0){
            res = res *10 + x%10;
            x = x / 10;
        }
        if(res > Integer.MAX_VALUE){
            return 0;
        }
        if(flag){
            return (int)res;
        }else{
            return (int)res * -1;
        }
    }
}

leetCode(62)-Reverse Integer的更多相关文章

  1. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  2. leetcode:Reverse Integer 及Palindrome Number

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

  3. LeetCode 7 Reverse Integer &amp&semi; int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  4. Leetcode 7&period; Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

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

  6. &lbrack;LeetCode&rsqb;&lbrack;Python&rsqb;Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【LeetCode】Reverse Integer&lpar;整数反转&rpar;

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7&period; Reverse Integer (倒转数字)

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

  9. &lbrack;LeetCode&rsqb; 7&period; Reverse Integer 翻转整数

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

随机推荐

  1. Spring的JDBC模板

    Spring对持久层技术支持 JDBC : org.springframework.jdbc.core.JdbcTemplate Hibernate3.0 : org.springframework. ...

  2. 第 31 章 项目实战-PC 端固定布局&lbrack;4&rsqb;

    学习要点: 1.热门旅游区 2.标题介绍区 3.旅游项目区 主讲教师:李炎恢 本章主要开始使用学习用 HTML5 和 CSS3 来构建 Web 页面,第一个项目采用 PC 端固定布局来实现. 一.热门 ...

  3. 第四章 电商云化,4&period;2 集团AliDocker化双11总结&lpar;作者: 林轩、白慕、潇谦&rpar;

    4.2 集团AliDocker化双11总结 前言 在基础设施方面,今年双11最大的变化是支撑双11的所有交易核心应用都跑在了Docker容器中.几十万Docker容器撑起了双11交易17.5万笔每秒的 ...

  4. phpcms

    phpcms 织梦 帝国cms

  5. Elasticsearch从0&period;90到1&period;2的不兼容变化-系统和设置

      本文为官方文档的翻译加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.1.   1.系统级别及设置方面 1.1 es启动时,默认是作为一个前台程序启动.如果你想让e ...

  6. oracle导出数据显示出现ora-00109或者LRM-00109出错修改办法

    出现这种问题是因为日期格式的问题,调整日期格式后就可以了 改成yyyy-mm-dd的格式就好了

  7. Unity UGUI图文混排源码&lpar;二&rpar;

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  8. 利用YOLOV3训练自己的数据

    写在前面:YOLOV3只有修改了源码才需要重新make,而且make之前要先make clean. 一.准备数据 在/darknet/VOCdevkit1下建立文件夹VOC2007. voc2007文 ...

  9. SIM800C 连接服务器

    AT+CIPSTART=TCP,域名,端口号 OK 只返回OK,这种情况,说明域名的服务器出错了,OK表示格式正确,但是实际上的TCP是没有连接上的. 测试库服务器出错的时候,就是这种情况 实际连上了 ...

  10. Token和SessionStorage&lpar;会话存储对象&rpar;

    sessionStorage数据只在当前标签页共享 存在本地   关闭浏览器后会清除数据(关闭标签页不会清楚) localStorage数据会存在浏览器中  浏览器关了数据也还在 只有清除缓存才会消失 ...