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. ...
随机推荐
- Angular5 路由传参的3种方法
一共3种方法. 1.问号后面带的参数,获取参数的方式:ActivatedRoute.queryParams[id] 例如:/product?id=1&name=iphone还可以是: [rou ...
- Oracle中备份用户对象的两种方法
方法1: 执行步骤: exp userid=用户名/密码@数据库名 file=c:\emp.dmp 使用当前用户导出 exp userid=sys/sys@数据库名 file=c:\emp.dmp o ...
- Java面试16|设计模式
1.单例模式: 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下几个要素: 私有的构造方法 指向自己实例的私有静态引用 以自己实例为返回值的静态的公有的方法 单例模式根 ...
- 最小费用最大流(luogu P3381 【模板】最小费用最大流)
题目链接 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S. ...
- docker volume创建、备份、nfs存储
docker存储volume #环境 centos7.4 , Docker version 17.12.0-ce docker volume创建.备份.nfs存储 #docker volume 数据存 ...
- MacOS和iOS开发中异步调用与多线程的区别
很多童鞋可能对Apple开发中的异步调用和多线程的区别不是太清楚,这里本猫将用一些简单的示例来展示一下它们到底直观上有神马不同. 首先异步调用可以在同一个线程中,也可以在多个不同的线程中.每个线程都有 ...
- ROS探索总结(十八)——重读tf
在之前的博客中,有讲解tf的相关内容,本篇博客重新整理了tf的介绍和学习内容,对tf的认识会更加系统. 1 tf简介 1.1 什么是tf tf是一个让用户随时间跟踪多个参考系的功能包,它使用一种树型数 ...
- Eclipse中配置javap命令
Run→External Tools→External Tools Configurations-进入如下图二所示的Program配置界面.也可以通过如下图一所示的工具栏按钮进入Program配置界面 ...
- SQLite Update 语句(http://www.w3cschool.cc/sqlite/sqlite-update.html)
SQLite Update 语句 SQLite 的 UPDATE 查询用于修改表中已有的记录.可以使用带有 WHERE 子句的 UPDATE 查询来更新选定行,否则所有的行都会被更新. 语法 带有 W ...
- Apache Beam—透视Google统一流式计算的野心
Google是最早实践大数据的公司,目前大数据繁荣的生态很大一部分都要归功于Google最早的几篇论文,这几篇论文早就了以Hadoop为开端的整个开源大数据生态,但是很可惜的是Google内部的这些系 ...