反转整数
2017-09-08 10:05
253 查看
/*
问题描述:将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回0
来源:LintCode
作者:syt
日期:2017-9-8
思路:测试数据中存在越界数,所以转化为字符串处理即可
*/
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
/*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
// write your code here
int result = 0;
string nums = to_string(n);
string max = to_string(INT_MAX);
int i = 0;
int j = nums.length() - 1;
while (i < j)
{
char tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
i++;
j--;
}
cout << nums << endl;
cout << max << endl;
if (nums.length() == max.length() && nums > max)
result = 0;
else
result = atoi(nums.c_str());
if (n < 0)
result = -result;
return result;
}
问题描述:将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回0
来源:LintCode
作者:syt
日期:2017-9-8
思路:测试数据中存在越界数,所以转化为字符串处理即可
*/
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
/*
* @param n: the integer to be reversed
* @return: the reversed integer
*/
int reverseInteger(int n) {
// write your code here
int result = 0;
string nums = to_string(n);
string max = to_string(INT_MAX);
int i = 0;
int j = nums.length() - 1;
while (i < j)
{
char tmp = nums[j];
nums[j] = nums[i];
nums[i] = tmp;
i++;
j--;
}
cout << nums << endl;
cout << max << endl;
if (nums.length() == max.length() && nums > max)
result = 0;
else
result = atoi(nums.c_str());
if (n < 0)
result = -result;
return result;
}
相关文章推荐
- [置顶] [LeetCode] Reverse Integer 整数反转
- LintCode python 小白-简单题-413-反转整数
- 字符串反转成整数
- C语言实现整数反转-简练算法
- 整数反转
- 整数反转的程序
- 反转一个整数
- 按位反转字符串与整数
- LeetCode 7. Reverse Integer 整数反转
- 整数反转
- 对一个整数按位反转
- 整数反转算法(php实现)
- 整数二进制位反转
- [Leetcode #7]Reverse Integer整数反转、逆向打印整数
- LeetCode7——Reverse Integer(将一个整数反转,注意溢出的处理)
- 7. Reverse Integer(整数反转,注意超范围记为0)
- 输入一个整数,将各位数字反转后输出
- 反转在整数字节的顺序
- leetcode_7. Reverse Integer 反转整数的各个数字
- lintcode,反转整数