leetCode练题——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.
# -*- 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
leetCode练题——7. Reverse Integer的更多相关文章
- leetcode算法题笔记|Reverse Integer
/** * @param {number} x * @return {number} */ var reverse = function(x) { var s; if(x<0){ s=-x; } ...
- 乘风破浪:LeetCode真题_013_Roman to Integer
乘风破浪:LeetCode真题_013_Roman to Integer 一.前言 上一节我们讨论了如何把阿拉伯数字转换成罗马数字,现在我们需要思考一下如何把罗马数字转换成阿拉伯数字,其实我们仔细观擦 ...
- 乘风破浪:LeetCode真题_008_String to Integer (atoi)
乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...
- 【算法】LeetCode算法题-Roman To Integer
这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- Leetcode算法题 7. Reverse Integer2
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- C语言二维数组指针与指针数组
http://c.biancheng.net/view/2022.html http://c.biancheng.net/view/2020.html
- 月薪20k的web前端开发程序员,他们都会的这6招
web前端工程师是近几年的新兴职业,也是目前火爆而且高薪的职业.不同的公司也有不同的叫法,比如:网页界面开发,网站设计等,要学好web前端开发,需要掌握什么方法与技巧? 一.div和table 这个是 ...
- 5.Mybatis--解决属性名和字段名不一致的问题(起别名+resultMap)
我们看一下数据库中的字段 新建一个项目(我们拷贝之前的)来测试实体类字段跟数据库不一致 看看看,实体类中的属性是不是跟数据库表中的名不一样了 好,我们查询一下: 出现问题了:空值 为什么为空? 因为 ...
- idea tomcat启动无效
今天换了台电脑,用idea 部署tomcat启动死活启动不了,报 Application Server was not connected before run configuration stop, ...
- BZOJ1015[JSOI2008]星球大战starwar题解报告
题目链接 考虑正序去除点去掉其所有连边十分复杂,可以倒序离线处理,每次新建一个点,连接其连边,用并查集统计联通块的个数. 附代码 #include<iostream> #include&l ...
- Linux - Shell - find - 进阶: 时间与大小
概述 继续 find 这次的内容, 参数稍微要 复杂那么一丢丢 背景 刚学会了 基础 的参数 现在来了解一些 时间 和 空间 的参数 一说到操控时间空间, 感觉立马起来了... 准备 OS cento ...
- 发送数据给sap和接收
1.确保已经连通sap 2.发送数据:这是以表的形式发送,而且是批量发送给sap 3.接收sap返回信息:这个比较特别,碰到时试一试 package com.yiyezhiqiu.lyh.utils; ...
- 安装rocky版本:openstack-nova-compute.service 计算节点服务无法启动
问题描述:进行openstack的rocky版本的安装时,计算节点安装openstack-nova-compute找不到包. 解决办法:本次实验我安装的rocky版本的openstack 先安装cen ...
- 一起了解 .Net Foundation 项目 No.3
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. AutoMapper Au ...
- js实现页面下拉后展示导航,以及点击导航自动滑动到相关页面
//监控,下拉750px后展示导航 $(window).scroll(function(){ var $this = $(this); var targetTop = $(this).scrollTo ...