`

【LeetCode】- Reverse Integer(将一个整数反转)

 
阅读更多

[问题:]

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

题解:给定一个整数,将这个整数的数字旋转位置。

[ 分析:]

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
注意点1:如果一个整数最后一位位0,比如:10100,那么反转后将变成101。
注意点2:如果输入的数为1000000003,那么反转后将会出现溢出,需要注意。

[ 解法:]

/*
 * 假设输入123:
 * 123 -> 3  
 * 12 -> 30 + 2  
 * 1 -> 320 + 1  
 */
public class Solution {
	public int reverse(int x) {
		int result = 0;
		while (x != 0) {
			result = result * 10 + x % 10; // 每一次都在原来结果的基础上变大10倍,再加上余数
			x = x / 10; // 对x不停除10
		}
		return result;
	}

	public static void main(String[] args) {
		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
		while (scanner.hasNext()) {
			int num = scanner.nextInt();
			System.out.println(new Solution().reverse(num));
		}
	}
}



分享到:
评论

相关推荐

    leetcode第321题-LeetCode-Reverse_Integer:LeetCode-Reverse_Integer

    给定一个有符号的 32 位整数 x,返回 x 其数字颠倒。 如果反转 x 导致值超出有符号的 32 位整数范围 [-231, 231 - 1],则返回 0。 假设环境不允许您存储 64 位整数(有符号或无符号)。 示例 1: 输入:x = 123 输出...

    字符串整数的余数leetcode-reverse-integer:倒整数

    给定一个 32 位有符号整数,反转整数的数字。 注意:假设我们正在处理的环境只能存储 32 位有符号整数范围内的整数:[−231, 231 − 1]。 出于此问题的目的,假设您的函数在反转整数溢出时返回 0。 示例 1: Input: ...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic ...Reverse Integer 整数反转 ...Integer ...在排序数组中查找元素的第一个和最后一个位

    判断链表是否为回文链表leetcode-Leetcode-solutions:Leetcode问题的思考和解决方案

    构造一个 Python 字典: d={} 将每一行作为记录存储到 dict 存储的正常元素应该是 i%(d*numOfRows-2 )=k,其中k=1到numOfRows当k>=numOfRows时,对应中间的元素,存储位置应该是(numOfRows-1)-(i-(numOfRows-1)) #...

    leetcode卡-leetcode:一些leetcode问题的解决方法,请注意license!

    Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中删除重复项 代码: Leetcode\RemoveDuplicates\RemoveDuplicates....

    javalruleetcode-Leetcode:力码解决方案

    java lru leetcode Leetcode-Java Use Java to solve Leetcode&JianZhiOffer problems. Leetcode Num ...(寻找两个有序数组的中位数) ...Reverse Integer (整数反转) Easy Math 8 String to Integer (ato

    leetcode题库-LeetCode:力码

    整数反转 Reverse Integer.cpp 9 回文数 Palindrome Number.cpp 12 整数转罗马数字 Integer to Roman.cpp 13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 ...

    leetcode跳跃-LeCode:乐科

    整数反转 8. String to Integer (atoi) 字符串转换整数 (atoi) 9. Palindrome Number 回文数 10. Regular Expression Matching 正则表达式匹配 11. Container With Most Water 盛最多水的容器 12. Integer to Roman ...

    lrucacheleetcode-myfirstrepo:书中的编码练习-Python速成课程,EricMatthes的第2版

    ->leetcode-reverse_integer.py :此代码返回 32 位整数的反转。 使用的技术- Python3。 来源- leetcode.com ->eq_world_map2.py : 此代码使用数据可视化库 - plotly 显示地震的 30 天数据。 数据源是eq_data_30_day...

    LeetCode去除数组重复元素-leetcode_[removed]刷题

    Integer(整数反转) 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 Palindrome Number(回文数) 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数...

    leetcode卡-LeetCodeSolution:LeetCode解题方案

    数字反转(Reverse) 回文数(PalindromeNumer) 罗马数字转整数(RomanToInteger) 两数之和(sumInArray) 有序数组的平方(squaresOfASortedArray) 卡牌分组(xOfAKindInADeckOfCards) 排序数组去除重复项...

    LeetCode判断字符串是否循环-leetcode:leetcode

    给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。 题解思路 两层遍历,先...

    leetcode二维数组搜索-leetcode:C中一些算法问题的解决

    ./7_reverse_integer.c : 反转整数 ./string2int.c : 字符串到整数 ./search_number_appeared_more_than_half.c : 找出数组中出现次数超过了多少 ./152_maximum_product_subarray.c : 最大积子./152_maximum_product...

    LeetCode2018

    400 题LeetCode solutions in Java 8 and Python3.NO.Title中文名SolutionNoteDifficultyTag0EasyMapping1MediumLinkedList2MediumMapping3Median of Two Sorted Arrays两个排序数组的中位数Java PythonNoteHard4...

    lrucacheleetcode-PythonStudy:Python学习

    这是一个学习python的项目! #code 这是python的一些测试功能特性 #datastruct 这是一些在python中研究数据结构的代码 #leetcode 这是一些解决##Reverse Integer 整数的逆向数字问题的代码。 示例 1:x = 123,返回 ...

Global site tag (gtag.js) - Google Analytics