这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch

public int reverse(int x) {
try {
int result = 0;
if (x == 0) {
return x;
} else if (x < 0) {
String num = Integer.toString(0 - x);
String newNum = new StringBuilder(num).reverse().toString();
result = 0 - Integer.parseInt(newNum);
} else {
String num = Integer.toString(x);
String newNum = new StringBuilder(num).reverse().toString();
result = Integer.parseInt(newNum);
} return result;
} catch (Exception e) {
return 0;
} }

这种方法要好一些:https://discuss.leetcode.com/topic/15134/very-short-7-lines-and-elegant-solution

public int reverse(int x) {
long rev= 0;
while( x != 0){
rev= rev*10 + x % 10;
x= x/10;
if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}
return (int) rev;
}

leetcode:7. Reverse Integer的更多相关文章

  1. LeetCode:7. Reverse Integer(Easy)

    题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...

  2. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  3. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  4. C# 写 LeetCode easy #7 Reverse Integer

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

  5. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  6. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  7. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  8. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  9. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

随机推荐

  1. nginx gzip 压缩设置

    mime.types 中包含所有文件的类型,不知道的可以去里面查询 gzip配置的常用参数 gzip on|off;  #是否开启gzip gzip_buffers 32 4K| 16 8K #缓冲( ...

  2. 算法(Algorithms)第4版 练习 1.5.24

    package com.qiusongde; import edu.princeton.cs.algs4.StdOut; public class Exercise1524 { public stat ...

  3. 使用jQuery为博客生成目录

    这段代码展示了如何为div#content中的内容生成目录,也无非是对h系列标记进行解析.当然,也早有一些人实现了.​1. [代码][HTML]代码     <html>    <h ...

  4. meta 标签代码解决IE兼容问题,IE6,IE7,IE8,IE9,IE10(包括360的兼容模式)

    最近做了一个项目,客户反映,在360下布局错位,远程调试了一下,发现客户使用的是360的兼容模式,然而我在自己的电脑上测试的时候是正常的(兼容模式也正常):简单研究了一下360的兼容模式,在360的兼 ...

  5. CheckStyle:unable to parse configuration stream - Element type "message" must be declared

    版本在1.3以上,包括1.3: <!DOCTYPE module PUBLIC          "-//Puppy Crawl//DTD Check Configuration 1. ...

  6. Finding Similar Items 文本相似度计算的算法——机器学习、词向量空间cosine、NLTK、diff、Levenshtein距离

    http://infolab.stanford.edu/~ullman/mmds/ch3.pdf 汇总于此 还有这本书 http://www-nlp.stanford.edu/IR-book/ 里面有 ...

  7. 全面解析Bootstrap手风琴效果

    触发手风琴可以通过自定义的data-toggle 属性来触发.其中data-toggle值设置为 collapse,data-target="#折叠区标识符". 第一步:设计一个面 ...

  8. GeoServer基础教程(四):空间数据互操作的接口规范WMS、WFS和WCS

    转载:https://ethanblog.com/tech/all-about-wms-wfs-and-wcs.html 前面几节介绍了GeoServer基础教程的一些基本操作,相信大家对GeoSer ...

  9. Python习题-一个函数实现读写功能

    def new_op_file(filename,content=None): f = open(filename,'a+') f.seek(0) if content: #非空即真,如果有内容就往下 ...

  10. 前端多媒体(7)—— 在浏览器中实现rtmp推流

    示例:https://young-cowboy.github.io/gallery/rtmp_client/index.html 在国内的直播场景中通常使用,rtmp协议作为推流协议.RTMP是Rea ...