leetcode:7. Reverse Integer

这题简单,也花了我好长时间,我自己写的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的更多相关文章
- LeetCode:7. Reverse Integer(Easy)
题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode练习题 7. Reverse Integer
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
随机推荐
- [原创]关于在CentOS 7.0 下 安装nfs ,遇见 Transaction check error问题的解决
今天小弟的同学在使用阿里云的服务器安装nfs的时候,出现了一下问题 Transaction check error: file /usr/lib/systemd/system/blk-availabi ...
- PHP 邮件发送类
mail.php <?php /** * 邮件发送类 * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件的邮件 * 需要的php扩展,sockets和Filei ...
- web框架之Django<一、初识>
一.什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. 对于所有的 ...
- nginx 搭建虚拟主机
一.排错三部曲 第一步在客户端上ping服务端ip ping 10.0.0.8 第二部在客户端上telnet服务器端IP.端口 telnet 10.0.0.8 第三部在客户端使用wget命令检测 ...
- find查找文件
linux下最强大的搜索命令为”find“. 它的格式为”find <指定目录> <指定条件> <指定动作>“: 比如使用find命令搜索在根目录下的所有inter ...
- running a background task over ssh
原文: Why does running a background task over ssh fail if a pseudo-tty is allocated? 问题: I've recently ...
- Linux- 自动备份MySQL数据库脚本
数据安全很重要,所以日常中需要对数据库进行备份.
- IE和FireFox关于CSS的兼容性
1. [代码][Java]代码 CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理技巧并整理了一下.对于web2 ...
- php设计模式课程---3、为什么会有抽象工厂方法
php设计模式课程---3.为什么会有抽象工厂方法 一.总结 一句话总结: 解决简单工厂方法增加新选择时无法满足面向对象编程中的开闭原则问题 1.什么是面向对象编程中的开闭原则? 应该对类的增加开放, ...
- ES doc_values介绍——本质是field value的列存储,做聚合分析用,ES默认开启,会占用存储空间(列存储压缩技巧,除公共除数或者同时减去最小数,字符串压缩的话,直接去重后用数字ID压缩)
doc_values Doc values are the on-disk data structure, built at document index time, which makes this ...