本文是在学习中的总结,欢迎转载但请注明出处: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的更多相关文章

  1. LeetCode 7. Reverse Integer

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

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [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 ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. 使用ffmpeg转码时遇到aac报错

    今天尝试用ffmpeg转一个视频的格式,结果报出这个错误: The encoder 'aac' is experimental but experimental codecs are not enab ...

  2. Python中strip()、lstrip()、rstrip()用法详解

    Python中有三个去除头尾字符.空白符的函数,它们依次为: strip: 用来去除头尾字符.空白符(包括\n.\r.\t.' ',即:换行.回车.制表符.空格)lstrip:用来去除开头字符.空白符 ...

  3. comtypes加word 2013批量将pdf转换为doc

    office 2013很强大. import os import sys import re import comtypes.client wdFormatPDF = 17 def covx_to_p ...

  4. chrome官方完整安装包

    But did you know Google allows you to download the full standalone installer of Chrome from its offi ...

  5. 让你的代码量减少3倍!使用kotlin开发Android(一)

    让你的代码量减少3倍!使用kotlin开发Android(一) 创建Kotlin工程 本文同步自博主的私人博客:wing的地方酒馆 写在前面 使用kotlin开发android已经两周多了.得到的好处 ...

  6. Errors occurred during the build. Errors running builder 'JavaScript Validator' on project '项目名'.

    把JavaScript Validator去掉.去掉的方法是:选择一个项目--右键Properties--Builders(排第二)--点一下右侧会有四项--取消第一项"JavaScript ...

  7. SQL实例整理

    本文适合将w3school的SQL教程(http://www.w3school.com.cn/sql/sql_create_table.asp)都基本看过一遍的猿友阅读. 说说博主的情况吧.毕业找工作 ...

  8. win2008r2 AD用户账户的批量导入方法

    win2008r2 AD用户账户的批量导入方法 http://www.jb51.net/article/38423.htm

  9. property干嘛的

    >>> import datetime >>> class c(): @property def noww(self): return datetime.datet ...

  10. 【伯乐在线】100个高质量Java开发者博客

    本文由 ImportNew - 夏千林 翻译自 programcreek.欢迎加入翻译小组.转载请见文末要求. ImportNew注:原文中还没有100个.作者希望大家一起来推荐高质量的Java开发博 ...