7. 反转整数(Reverse Integer) C++
知识点:
- 内置变量 INT_MAX INT_MIN
- 运算结果是否溢出的判断
- 判断pop>7即pop>INT_MAX%10
- 判断pop<-8即pop<INT_MIN%10
class Solution {
public:
int reverse(int x) {
int pop,ans=;
while(x)
{
pop = x%;
x /= ;
if(ans > INT_MAX/ || (ans == INT_MAX/ && pop >))
return ;
if(ans < INT_MIN/ || (ans == INT_MIN/ && pop <-))
return ;
//cout << "pop: " << pop << " ans: " << ans << '\n';
ans = ans* + pop;
}
return ans;
}
};
7. 反转整数(Reverse Integer) C++的更多相关文章
- [Swift]LeetCode7. 反转整数 | Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- Leetcode 7 反转整数Reverse Integer
给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: ...
- LeetCode 7. 反转整数(Reverse Integer)
题目描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- leetCode(62)-Reverse Integer
题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 clic ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
随机推荐
- 五、IO编程
input/output:输入.输出 Stream(流):Input Stream就是数据从外面(磁盘.网络)流进内存,Output Stream就是数据从内存流到外面去.(流:相当于管道) 由于CP ...
- 转 这种方法可以免去自己计算大文件md5 的麻烦
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using ...
- Unity Shaderlab: Object Outlines 转
转 https://willweissman.wordpress.com/tutorials/shaders/unity-shaderlab-object-outlines/ Unity Shader ...
- centos7 彻底卸载PHP7
[root@xxx php-memcached]# rpm -qa | grep php php70w-common--.w7.x86_64 php70w-devel--.w7.x86_64 php7 ...
- Import Projects from git
1:工具栏window -->show view -->Other
- Python pymysql 增删改查封装
关于pymysql 的增删改查,简单做个封装,方便后面使用直接拿来调用即可. 其中 增删改 的处理其实是一致的,本可以使用统一的方法,但是为了明显区分,这里分开来写了. 直接看代码就即可,如下: # ...
- 优雅地记录Python程序日志2:模块组件化日志记录器
本文摘自:https://zhuanlan.zhihu.com/p/32043593 本篇将会涉及: logging的各个模块化组件 构建一个组件化的日志器 logging的模块组件化 在上一篇文章中 ...
- 使用两个栈来完成一个队列,需要是实现的功能有add,poll.peek
2017-06-23 19:15:16 队列时先进先出型,而栈是先进后出型,这就需要建立一个联系.我想到的一个简单的表示方式是: 这样就需要两个栈,栈1是用来实现add操作,即直接push进去就行:栈 ...
- 关于ascii码的一些内容
1.通过C#程序输出tab(制表符)内容. 1.1常用方式我们可以是 //测试输出\t到文件 File.WriteAllText("test.txt", "a\tb\tc ...
- C# http监听之Nancy.net
通过winform或者是控制台应用程序监听http请求,之前使用的是微软的HttpListener,参考https://www.cnblogs.com/duanjt/p/5566336.html 然后 ...