Q7:Reverse Integer

7. Reverse Integer

官方的链接:7. Reverse Integer

Description :

Given a 32-bit signed integer, reverse digits of an integer.

Example1:



Input: 123

Output: 321



Example2:



Input: -123

Output: -321



Example3:



Input: 120

Output: 21



Note:

Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

问题描述

反转32位的数字

思路

上一次的模*10 + 这一次的模。中间判断是否有溢出

注意:last_mod * 10 + this_mod,而x /= 10

[github-here]

 1 public class Q7_ReverseInteger {
 2     public int reverse(int x) {
 3         int revResult = 0;
 4         while (0 != x){
 5             int newResult = revResult * 10 + x % 10;
 6             //judge whether it overflows
 7             if (newResult / 10 != revResult) {
 8                 return 0;
 9             }
10             revResult = newResult;
11             x /= 10;
12         }
13         return revResult;
14     }
15 }

原文地址:https://www.cnblogs.com/wpbxin/p/8654620.html

时间: 2024-10-07 15:47:32

Q7:Reverse Integer的相关文章

LeetCode第[7]题(Java):Reverse Integer 标签:数学

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

LeetCode之“数学”:Reverse Integer && Reverse Bits

1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

No.007:Reverse Integer

题目: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 官方难度: Easy 翻译: 倒转整数. 例子: 输入123,返回321:输入-123,返回-321. 思路: 1.既然是倒转整数,拿倒转字符串的思路虽然能做,但应该不是原题想要的结果,而且效率应该也会差一点. 2.优先获得输入整数的最高位数,使用取余的方式乘以10的对应次方,循环累加. 解题中可能遇到的困难: 1

LeetCode:Reverse Integer

昨天晚上临睡前A掉的.这道题依然很水. 题目描述:给定一个int数,将各位翻转以后输出. tip:这道题本身很简单,重要的是如何把情况考虑全面.比如:1.int数翻转以后溢出的问题.2.数的结尾有0,翻转以后如何输出的问题,比如100. 代码: public class Solution { final static int max = 2147483647; final static int testMax = 214748364; final static int min = -214748

lintcode 容易题:reverse integer 颠倒整数

题目: 颠倒整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 解题: 直接反转,越界处理好炒蛋 Java程序: public class Solution { /** * @param n the integer to be reversed * @return the reversed integer */ public int reverseInteger(int n

leetcode(7): Reverse Integer 源码实现 runtime: 8ms

题目 :Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 题目分析及部分代码解析: 1.需要考虑一位数,比如1,2,3等特殊情况,返回本身. 2.需要考虑0,返回0. 3.需要考虑如123000,45600等末尾有若干零的情况,正确结果应为321.654,不应该出现000321,00654等情况. 4.需要4字节int类型数据的取值范

LeetCode7~9 Reverse Integer/String to Integer (atoi)/Palindrome Number

一:Reverse Integer 题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 链接:https://leetcode.com/problems/reverse-integer/ 分析:这题通过不断取余将余数存放在一个vector中,然后乘以相应的10^i次方相加即可,这里主要考虑是否overflow,因此将result设为long long int

LeetCode:Palindrome Number,Reverse Integer

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext