【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列
(一)题目
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
(二)解题
这题看上去很简单,动笔一挥之下,写出如下代码:
class Solution {
public:
int reverse(int x) {
bool flag = false;
if(x<0) {
x=-k;
flag = true;
}
long result=0;
while(x!=0)
{
result*=10;
result+=x%10;
x=x/10;
}
return flag==true ?-result:result;
}
};
满心以为一步就能Accepted。没想到直接wrong answer!!
1534236469反过来9646324351超过了int的最大值,于是修改代码:
class Solution {
public:
int reverse(int x) {
long k = x;
bool flag = false;
if(k<0) {
k=-k;
flag = true;
}
long result=0;
while(k!=0)
{
result*=10;
result+=k%10;
k=k/10;
}
if(result>2147483648||result<-2147483647) return 0;//判断越界没有
else return flag==true ?(int)-result:(int)result;//结果判断正负输出
}
};
这下能accepted了!果然越是看起来简单的题目越需要细节处理!
【一天一道LeetCode】#7. Reverse Integer的更多相关文章
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 【一天一道LeetCode】#12 Integer to Roman
一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- VMware 下的CentOS6.7 虚拟机与Windows7通信
在有网络的情况下,VMware 虚拟机使用桥接模式(Bridged) 和NAT方式,会自动通信,但是在没有网络的情况下怎么办呢?对,是的,使用host-only模式,如何设置呢? 注:将Windows ...
- 剑指Offer——完美+今日头条笔试题+知识点总结
剑指Offer--完美+今日头条笔试题+知识点总结 情景回顾 时间:2016.9.28 16:00-18:00 19:00-21:00 地点:山东省网络环境智能计算技术重点实验室 事件:完美世界笔试 ...
- [EXTJS5学习笔记]第二十六节 在eclipse/myeclipse中使用sencha extjs的插件
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40507383 插件下载: http://download.csdn.net/detai ...
- Struts 1 之文件上传
Struts 1 对Apache的commons-fileupload进行了再封装,把上传文件封装成FormFile对象 定义UploadForm: private FormFilefile; //上 ...
- 对LCS算法及其变种的初步研究
LCS的全称为Longest Common Subsequence,用于查找两个字符串中的最大公共子序列,这里需要注意区分子序列与子串,所谓子序列,指的是从前到后,可以跳跃元素筛选,而字串则必须连续筛 ...
- Oracle使用游标删除所有用户数据表中的所有记录脚本
应用场景:因为数据库中的数据涉及机密信息,希望一次性能删除掉所有数据,只保留数据表结构,供新项目开发程序用 测试结果:经查询已删除所有数据 存在问题:数据表如果存在外键的话下面脚本可能执行不成功,请自 ...
- UNIX网络编程——非阻塞connect:时间获取客户程序
#include "unp.h" int connect_nonb(int sockfd, const SA *saptr, socklen_t salen, int nsec) ...
- C语言--指针函数和函数指针
指针函数和函数指针 指针函数其实是一个简称,是指带指针的函数,它本质上是一个函数,只是返回的是某种类型的指针.其定义的格式为: 类型标识符 *函数名(参数表) 函数指针,从本质上说是一个指针,只是它 ...
- SendMessageUpwards定义简单按钮(Unity3D开发之十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=582 SendMessageUp ...
- UNIX环境高级编程——信号说明列表
$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGK ...