leetcode-easy-string-7 Reverse Integer
mycode
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
while True:
if a[0] == '':
if len(a) > 1: a = a[1:]
else: return 0
else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a
注意:用int(x)时,会自动把x前面的0去掉
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
#while True:
# if a[0] == '0':
# if len(a) > 1: a = a[1:]
# else: return 0
# else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a
参考
class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
return x if x < 2147483648 and x >= -2147483648 else 0
leetcode-easy-string-7 Reverse Integer的更多相关文章
- Leetcode 题目整理-2 Reverse Integer && String to Integer
今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...
- LeetCode 【2】 Reverse Integer --007
六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
- leetCode练题——7. Reverse Integer
1.题目: 7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...
- 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- leetcode第七题--Reverse Integer
Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- 【LeetCode算法-7】Reverse Integer
LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...
- LeetCode记录之7——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The ...
随机推荐
- 搭建私有CA并基于OpenSSL实现双向身份认证
0x00 前言 互联网上的Web应用由于用户数目广泛,都是采用单向身份认证的,只需要客户端验证服务端的身份.但如果是企业内部的应用对接,客户端数量有限,可能就会要求对客户端也做身份验证,这时就需要一个 ...
- 在java中使用solr7.2.0 新旧版本创建SolrClient对比
在Java中使用solr 版本7.2.0 solrj已经更新到了7.2.0,新版本solr获取SolrClient的方式也和之前旧版本有所不同 solr6.5开始不推荐直接使用HttpSolrClie ...
- SecureCRT 连接 Centos7.0 (NAT模式),且能连接公网。
1.打开物理主机运行-输入cmd,输入ipconfig,获取物理主机ip地址. ip:192.168.11.138 2.点击网络适配器,选择NAT模式. 3.点击Centos界面左上角-编辑-虚拟网络 ...
- 使用TensorFlow玩GTA5
小白学TensorFlow(一) tensorflow安装 在安装之前,您必须选择以下类型的TensorFlow之一来安装: TensorFlow仅支持CPU支持.如果您的系统没有NVIDIA®G ...
- 【转载】Stanford CoreNLP Typed Dependencies
总结自Stanford typed dependencies manual 原文链接:http://www.jianshu.com/p/5c461cf096c4 依存关系描述句子中词与词之间的各种语法 ...
- Highcharts基本名词解释
1.Highcharts基本组成: 2.名词解释 lang 语言文字对象 所有Highcharts文字相关的设置 chart 图表 图表区.图形区和通用图表配置选项 colors 颜色 图表数据列颜色 ...
- 程序中的一些限制(基于Linux系统C语言)
今天突然想起来几个问题,在程序运行起来时,存在一些限制: 1,数组的长度(成员的个数)存在限制!(数组定义的空间大小)2,一个进程里打开的文件数.3,一个文件的名字的长度.4,一个进程里创建线程的个数 ...
- cmd命令行显示中文乱码
cmd命令行显示中文乱码多数是由于字符编码不匹配导致. 1.查看cmd编码方式 方法一.打开cmd,输入chcp命令回车(显示默认编码:活动代码页:936指GBK) 方法二.打开cmd在标题栏单击鼠标 ...
- tp6中使用微信支付sdk
一.下载微信支付sdk 二.将lib文件夹下的文件复制到目录:extend->WxPay 将example文件夹下的WxPay.Config.php文件也复制到:extend->WxPay ...
- Java I/O 全面详解
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...