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 ...
随机推荐
- 内存不足导致mysql关闭,CentOS6.5增加swap分区
某日发现mysql自动关闭了,查找错误日志发现以下错误 2017-07-14 13:07:30 5494 [Note] InnoDB: Initializing buffer pool, size = ...
- IE浏览器清除缓存及历史浏览数据
IE浏览器清除缓存方法如下: 打开IE浏览器,依次点击"工具-Internet选项-常规-删除",如下图所示, 有的时候发现你明明已经执行了删除,但是实际上还是有缓存数据,一般是因 ...
- h5唤醒App
一.应用场景 用户在访问我们的网页时,判断出这个用户手机上是否安装了我们的App,如果安装了则直接从网页上打开APP,否则就引导用户前往下载,从而形成一个推广上的闭环.这里只针对从网页端打开本地APP ...
- Summer training round2 #6 (Training #22)
A:二分答案 如果中位数比目前的大就right=mid-1 else left=mid+1 C!:几何 G:优先队列贪心 #include <bits/stdc++.h> using na ...
- 用Ant实现Java项目的自动构建和部署(转载以作收藏)
原文地址:http://tech.it168.com/j/2007-11-09/200711091344781.shtml 本文请勿转载! Ant是一个Apache基金会下的跨平台的构件 ...
- 第三次java测验1
设计思想: 输入一个字符串,然后将字符串倒置,比较字符串第i位上的字符与倒数第i位上的字符是否相同,如果都相同则字符串是回文:否则字符串不是回文. 源代码: package java3; import ...
- python+Appium自动化:logging配置代码分离
配置文件信息log.conf: [loggers]keys=root,simpleExample [logger_root]level=DEBUGhandlers=consoleHandler,fil ...
- Java & Mysql 餐饮管理系统 过程心得记录
------------------------------------------Have a Good Day~---------------------------------- 准备国赛和AC ...
- base64 转 Image
/// <summary> /// base64 转 Image /// </summary> /// <param name="base64"> ...
- windows2012下一端口多网站 Apache配置
援引自https://www.cnblogs.com/huangtailang/p/6026828.html 1.在httpd.conf文件里启用虚拟主机功能,即去掉下面配置项前面的# #LoadMo ...