7.Reverse Integer(long long类型)

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

click to showspoilers.

Have you thoughtabout this?

Here are some good questions to ask before coding. Bonus points for you ifyou have already thought through this!

If the integer‘s last digit is 0, what should the output be? ie, casessuch as 10, 100.

Did you notice that the reversed integer might overflow? Assume the inputis a 32-bit integer, then the reverse of 1000000003 overflows. How should youhandle such cases?

For the purpose of this problem, assume that your function returns 0 whenthe reversed integer overflows.

Update (2014-11-10):

Test cases had been added to test the overflow behavior.

HideTags

Math

#pragma once
#include<iostream>
#include<queue>
using namespace std;

//long 固定为32位 int 在32位机器上是32位,long long 固定是64位
int reverse(int x) {
	long long val = abs(x);//绝对值
	long long result = 0;
	long long up = 2147483647;
	long long down = 2147483648;
	down = -down;//2147483648计算机认为是unsigned int 不能加负号,曲线救国
	int flag = 1;//符号
	if (x < 0)
		flag = -1;
	queue<int> q;
	int temp = 0;
	while (val > 0)
	{
		temp = val % 10;//取余
		q.push(temp);
		val /=10;
	}
	while (!q.empty())
	{
		result =result*10 + q.front();
		q.pop();
	}
	if (result > up || result < down)
		return 0;
	return flag*result;
}
void main()
{
	cout << reverse(1000000003) << endl;
	cout << reverse(12345) << endl;
	system("pause");
}
时间: 2024-10-14 00:51:16

7.Reverse Integer(long long类型)的相关文章

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(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类型数据的取值范

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

7. Reverse Integer 反转int

7. Reverse Integer 问题: 反转int,当有负号时需要保留负号. 解决思路: 1.先将int类型转换为string,按照之前写过的string类型做好反转,再转为int类型. 2.不做类型转换.先将负数转换为正数进行统一处理,然后int类型数每次%10得到的余即依次为个.十.百-位上的数字. 自己写的1:(java) 这里是根据思路1来做的,发现在string类型转为int类型时容易抛出异常,无法转换.因此只能采用其他方法. public class Solution { pu

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

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

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 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