Leecode刷题之旅-C语言/python-7.整数反转
/*
* @lc app=leetcode.cn id=7 lang=c
*
* [7] 整数反转
*
* https://leetcode-cn.com/problems/reverse-integer/description/
*
* algorithms
* Easy (31.36%)
* Total Accepted: 77.7K
* Total Submissions: 247.8K
* Testcase Example: '123'
*
* 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
*
* 示例 1:
*
* 输入: 123
* 输出: 321
*
*
* 示例 2:
*
* 输入: -123
* 输出: -321
*
*
* 示例 3:
*
* 输入: 120
* 输出: 21
*
*
* 注意:
*
* 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
*
*/
int reverse(int x) {
long i =;
long t = x;
while(t){
i = i*+(t%);
t= t/;
}
if (i < INT_MIN || i >INT_MAX) //判定是否在int可表达的有效范围内
{
return ;
}
return i;
}
这道题相对来说很好理解,用余数除10的方法就可以实现整数的翻转。
要注意,这里设置成long类型,然后在最后判断是否在int范围内。否则会超出范围。
-----------------------------------------------------------------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=7 lang=python3
#
# [7] 整数反转
#
# https://leetcode-cn.com/problems/reverse-integer/description/
#
# algorithms
# Easy (31.70%)
# Total Accepted: 86K
# Total Submissions: 271.4K
# Testcase Example: '123'
#
# 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
#
# 示例 1:
#
# 输入: 123
# 输出: 321
#
#
# 示例 2:
#
# 输入: -123
# 输出: -321
#
#
# 示例 3:
#
# 输入: 120
# 输出: 21
#
#
# 注意:
#
# 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31, 2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
#
#
class Solution:
def reverse(self, x: int) -> int:
plus_minus = ""
reverse_x = ""
if x<0:
plus_minus = "-"
x = -x
for i in str(x):
reverse_x = i + reverse_x
reverse_x = plus_minus +reverse_x
if int(reverse_x)>pow(2,31)-1 or int(reverse_x)<pow(-2,31):
return 0
return int(reverse_x)
python这里得益于高级脚本语言的便捷,可以先把整形转成字符串,按后一位+前一位 这样的方式就可以实现翻转。
然后再把字符串转换成int类型(在这之前要判断其范围)
Leecode刷题之旅-C语言/python-7.整数反转的更多相关文章
- Leecode刷题之旅-C语言/python-349两整数之和
/* * @lc app=leetcode.cn id=371 lang=c * * [371] 两整数之和 * * https://leetcode-cn.com/problems/sum-of-t ...
- Leecode刷题之旅-C语言/python-9.回文数
/* * @lc app=leetcode.cn id=9 lang=c * * [9] 回文数 * * https://leetcode-cn.com/problems/palindrome-num ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
- Leecode刷题之旅-C语言/python-383赎金信
/* * @lc app=leetcode.cn id=383 lang=c * * [383] 赎金信 * * https://leetcode-cn.com/problems/ransom-not ...
随机推荐
- sql server性能分析--执行sql次数和逻辑次数
目前在做一个项目优化时,想通过数据库层分析sql server系统性能,查了一下网上代码,修改了一下标题和DMVs代码,以下代码可以用来分析系统运行一段时间后,那些语句是系统忙的sql语句.做为参考. ...
- CentOS 7 使用 yum 安装 jdk 1.8
安装之前先检查一下系统有没有自带open-jdk 命令: rpm -qa |grep java rpm -qa |grep jdk rpm -qa |grep gcj 如果没有输入信息表示没有安装. ...
- oozie说明(本文参考多处,自己留看)
Oozie概述: Oozie是一个基于Hadoop工作流引擎,也可以称为调度器,它以xml的形式写调度流程,可以调度mr,pig,hive,shell,jar,spark等等.在实际工作中,遇到对数据 ...
- ul li一行两个显示
- HDU 3625 第一类斯特林数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3625 题意: n个房间,房间里面放着钥匙,允许破门而入k个,拿到房间里面的钥匙后可以打开对应的门,但是 ...
- 4519: [Cqoi2016]不同的最小割
4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 489 Solved: 301 [Submit][Stat ...
- HDU 2795 Billboard 【线段树维护区间最大值&&查询变形】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others) ...
- Latex 编辑器安装
MiKTex + TexStudio 1. 下载安装MiKTex,从http://www.miktex.org/下载basic-miktex-2.9.5872-x64.exe,一路默认安装... 或者 ...
- 调用URL 接口服务
1.Net调用URL 接口服务 using System; using System.Collections; using System.Configuration; using System.Dat ...
- 优雅的QSignleton (一) Singleton单例实现
接下来笔者来介绍如何用QSingleton实现一个简单的单例. 代码如下. Singleton.cs namespace QFramework.Example { using UnityEngine; ...