Leetcode_7_Reverse Integer
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41578077
Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:
(1)这道算法题属于OJ中比较简单的题目。题意是将给定的整数逆序,但是涉及到几种特殊情况需要考虑。
(2)为了便于处理,将整数转为字符串。我们只需要遍历一次该字符串即可。
(3)在遍历的过程中,需要对正负数进行判断,当字符串长度大于0时,先取第一个字符进行判断,看其是否等于‘-’,如果相等
就是负数,需要设置一个标志位。
(4)在继续遍历的过程中,只需判断字符是否为数字(显然都是),然后通过标志位判断正负后进行累加操作。
(5)需要注意的时,翻转后的的整数可能会超过int的最大值,为了防止越界,我们定义的sun的类型必须为long,这样当大于或
者小于int的最大值和最小值时就返回0;其余情况直接将long强转为int,并返回结果。
(6)注意上述几个方面后,OJ通过肯定没问题。
算法实现代码如下所示(希望对你有所帮助,谢谢):
public static int reverse(int x) { String s = String.valueOf(x); long sum = 0; boolean isnegitive = false; for (int i = s.length()-1; i >=0 ; i--) { if(s.charAt(0)=='-'){ isnegitive = true; } if( Character.isDigit(s.charAt(i))){ if(isnegitive){ sum = sum * 10 - Integer.parseInt(String.valueOf(s.charAt(i))); }else{ sum = sum * 10 + Integer.parseInt(String.valueOf(s.charAt(i))); } } } if(sum>Integer.MAX_VALUE || sum<Integer.MIN_VALUE){ return 0; } return (int)sum; }
Leetcode_7_Reverse Integer的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- ZOJ-2965
Accurately Say "CocaCola"! Time Limit: 2 Seconds Memory Limit: 65536 KB In a party he ...
- 【python标准库模块一】时间模块time学习
本文介绍python的标准库模块time的常见用法 时间模块time 导入时间模块 import time 得到时间戳,这是统计从1970年1月1日0点0分到现在经过了多少秒,一般用于加减法一起用,比 ...
- WSGI及gunicorn指北(一)
作为一个Python Web 开发工程师,pyg0每天都喜滋滋的写着基于各种web框架的业务代码. 突然有一天,技术老大过来跟pyg0说,嘿,我们要新上线一个服务,你来帮我部署一下吧.不用太复杂.用g ...
- 小知识点 取消button动作 和取巧按钮高亮
如果让按钮在点击时不变暗 进行下面设置: 如果使butten失去动画效果进行下面设置:(一般用于图片+文字,效果等于可以加图片班的label,当然label不能加图片)
- Python3 解释器
Linux/Unix的系统上,Python解释器通常被安装在 /usr/local/bin/python3.4 这样的有效路径(目录)里. 我们可以将路径 /usr/local/bin 添加到您的Li ...
- 操作系统内核Hack:(一)实验环境搭建
操作系统内核Hack:(一)实验环境搭建 三四年前,心血来潮,入手<Orange's:一个操作系统的实现>学习操作系统内核,还配套买了王爽的<汇编语言(第二版)>和<80 ...
- MongoDb 用 mapreduce 统计留存率
MongoDb 用 mapreduce 统计留存率(金庆的专栏)留存的定义采用的是新增账号第X日:某日新增的账号中,在新增日后第X日有登录行为记为留存 输出如下:(类同友盟的留存率显示)留存用户注册时 ...
- Java使用BigDecimal保留double、float运算精度、保留指定位数有效数字、四舍五入
工具类 package --; import java.math.BigDecimal; /** * Created by kongqw on 2015/12/10. */ public final ...
- AP模块NOTE修改API
--创建 AP_NOTES_PUB.Create_Note ( p_api_version IN NUMBER , p_init_msg_list IN VARCHAR2 := FND_API.G_F ...
- 适配器模式(adapter)
适配器模式的定义: 将一个类的接口转换成客户希望的另外一个接口,适配器模式使得原本由于接口不兼容而不能在一起的那些类可以一起工作. 主要分为三类:类的适配器模式.对象的适配器模式.接口的适配器模式. ...