leetCode练题——7. Reverse Integer
2024-09-05 10:02:40
1、题目:
7. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
2、我的解法:
# -*- coding: utf-8 -*-
# @Time : 2020/1/26 12:33
# @Author : SmartCat0929
# @Email : 1027699719@qq.com
# @Link : https://github.com/SmartCat0929
# @Site :
# @File : 7. Reverse Integer.py
class Solution:
def reverse(self, x: int) -> int:
if x >= -2147483648 and x <= 2147483647:
if x >= 0:
r = ""
else:
r = "-"
w = abs(x)
y = str(w)
n = len(y)
d = []
for i in (y):
d.append(i)
for j in range(n):
c = d.pop()
r = r + c
v = int(r)
if v >= -2147483648 and v <= 2147483647:
return v
else:
return 0
else:
return 0
最新文章
- 阅读笔记Multi-task Learning for Stock Selection [NIPS1996]
- discuz!安装遇到问题的解决方案
- 有关tp里搜索框的实现方法
- 【项目】UICollectionViewFlowlayout再一次自定义
- 不可不知的C#基础 4. 延迟加载 -- 提高性能
- JavaScript之字符串
- noj [1479] How many (01背包||DP||DFS)
- Python自动化运维之23、Dom
- javascript之闭包深入理解(二)
- 10 个迅速提升你 Git 水平的提示(转)
- pm2部署多个nodejs项目配置教程
- [POI2014]KUR-Couriers
- vue中的数据监听以及数据交互
- CentOS环境下使用GIT基于Nginx的私服搭建全过程
- vscode插件解析-BookMark
- Office DCOM 组件交互权限设置
- Windows下Caffe实现图像分类
- 浅谈js抽象工厂模式
- Java学习笔记之——常用转义符号
- sql server2008 R2打开报错:无法识别的配置节 system.serviceModel解决办法分享