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 文中代码为了不动官网提供的初始 ...
随机推荐
- AcWing 487. 金明的预算方案
#include <cstring> #include <iostream> #include <algorithm> #include <vector> ...
- CSS技巧!鼠标经过图片抖动效果
把代码加到style.css(模板的主css里面): /**图片抖动**/ img:hover{-webkit-animation: tada 1s .2s ease both;-moz-animat ...
- Java-重载和重写区别剖析
重载(Overload)和重写(Override)是任何一门面向对象的语言都会具有的两个特性,自然,Java语言中也具有此两种特性.但是,对于Java新手,或者没有面向对象语言经验的开发者而言,这会是 ...
- C++ 深拷贝实例-改变原生数组
深拷贝 main.cpp #include <stdio.h> #include "IntArray.h" int main() { IntArray a(); ; i ...
- jsp中的javascript的$(document).ready( function() { $("#loginForm").validate()
转自:https://bbs.csdn.net/topics/392459787?list=71147533 下面是jsp页面中的JavaScript代码 $(document).ready( fun ...
- mysql 原有的主键情况下设置自增字段
mysql 的自增字段只能是主键,如果原表已经有主键,需要设置自增字段应该怎么做呢? 1.alter table bu_staff drop primary key; 先删除表的主键 id为原表 ...
- Python爬取mc皮肤【爬虫项目】
首先,找到一个皮肤网站,其中一个著名的皮肤网站就是 https://littleskin.cn .进入网站,我们就会见到一堆皮肤,这就是今天我们要爬的皮肤.给各位分享一下代码. PS:另外很多人在学习 ...
- dijkstra堆优化板子
咕咕咕. #include<queue> #include<cstdio> #include<cstring> #include<algorithm> ...
- Spark 中 GroupByKey 相对于 combineByKey, reduceByKey, foldByKey 的优缺点
避免使用GroupByKey 我们看一下两种计算word counts 的方法,一个使用reduceByKey,另一个使用 groupByKey: val words = Array("on ...
- Pacemaker+ISCSI实现Apache高可用-环境准备
Pacemaker是红帽7上的集群管理器,用于替代6上RHCS 配置ISCSI 服务端 yum -y install targetcli systemctl enable target.service ...