007. Reverse Integer

题目链接:https://leetcode.com/problems/reverse-integer/description/

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

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [?2^31,  2^31 ? 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

思路:

  • 对待处理的整数X,拷贝一份并命名为 copy,取拷贝值的绝对值进行下述数据处理;
  • 用长整型变量 ans 来存储翻转后的数值, ans变量赋初值为0; 当copy > 0 时对copy进行整除和取余操作:
    • int cur = copy % 10;  // 当前处理的数位的值
    • ans = ans * 10 + cur;
    • copy = copy / 10;
  • 对求得的ans值进行范围检验
  • X值为正数,则返回ans;X值为负数则返回 -ans 。 

编码如下

 1 class Solution {
 2 public:
 3     int reverse(int x) {
 4         // 若数值不在 [?2^31,  2^31 ? 1]范围内,则返回0
 5         //if (x < (1 << 31) || x > (~(1 << 31)))  return 0;
 6
 7         // 若x属于[-9, 9]这个范围内,则返回x
 8         if (-10 < x && x < 10)  return x;
 9
10         // 其他情况
11         long int ans = 0;
12         int copy = (x >= 0 ? x : -x);
13
14         while (copy > 0)
15         {
16             int cur = copy % 10;    // 当前处理的数位值
17             ans = ans * 10 + cur;
18             copy = copy / 10;
19         }
20
21         ans = (x >= 0 ? ans : -ans);
22         if (ans < (1 << 31) || ans > (~(1 << 31)))  return 0;
23
24         return static_cast<int>(ans);
25     }
26 };

 

原文地址:https://www.cnblogs.com/ming-1012/p/9907702.html

时间: 2024-11-10 00:08:54

007. Reverse Integer的相关文章

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

LeetCode-Algorithms #007 Reverse Integer, Database #182 Duplicate Emails

LeetCode-Algorithms #007 Reverse Integer 给定一个32位整数, 将其各位反转并返回, 如果结果超出取值范围就返回0 1 class Solution { 2 public int reverse(int x) { 3 //对原数取绝对值 4 int y = Math.abs(x); 5 //将原数转换为字符串 6 String s1 = Integer.toString(y); 7 //将字符串转换为字符数组 8 char[] arr = s1.toCha

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

Java for LeetCode 007 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决. 题目不难: JAVA实现如下: public class Solution { static public int reverse(int x) { if(x=

LeetCode 007 Reverse Integer - Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的

Reverse Integer - 反转一个int,溢出时返回0

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: 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 you have alread

水题 Reverse Integer

public class Solution { int reverse(int x) { long result = 0; while(x!=0){ result = result*10 + x%10 ; x/=10 ; } if(result>Integer.MAX_VALUE || result<Integer.MIN_VALUE ) return 0 ; else return (int)result ; } } https://leetcode.com/problems/reverse

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) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方