Reverse Integer

想用余10直接算,没想到

-123%10 是 7, 原因 -123-(-123//10*10)

r=a-n*[a/n]
以上,r是余数,a是被除数,n是除数。 唯一不同点,就是商向0或负无穷方向取整的选择,c从c99开始规定向0取整,python则规定向负无穷取整,选择而已。 所以套用上述公式为:
C 语言:(a%n的符号与a相同)
-9%7=-9 - 7*[-1]=-2;
9%-7=9 - -7*[-1]=2;
Python语言::(a%n的符号与n相同)
-9%7=-9 - 7*[-2]=5
9%-7=-9 - -7*[-2]=-5

by 负数求余,Python和C语言的不同

所以直接存符号吧。

第1次提交
import time

class Solution:
def __init__(self):
self.maxValue=2**31-1
self.minValue=-2**31
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
reverseInt=0 flag=1
if x<0:
flag=-1
x=abs(x) i=0
while x>=1:
reverseInt*=10
reverseInt+=x%10
x//=10
i+=1
#print(reverseInt,x) reverseInt*=flag if reverseInt>self.maxValue or reverseInt<self.minValue:
reverseInt=0
return reverseInt if __name__ == "__main__": data = [
{
"input":123,
"output":321,
},
{
"input":-123,
"output":-321,
},
{
"input":120,
"output":21,
} ];
for d in data: print(d['input']) # 计算运行时间
start = time.perf_counter()
result=Solution().reverse(d['input'])
end = time.perf_counter() print(result)
if result==d['output']:
print("--- ok ---",end="\t")
else:
print("--- error ---",end="\t") print(start-end)

一次AC,题太简单,没有什么感觉:)

LeetCode 7 Reverse Integer & int的更多相关文章

  1. LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2

    https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. leetcode:Reverse Integer 及Palindrome Number

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

  4. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

  5. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  6. [LeetCode][Python]Reverse Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

  8. LeetCode 7. Reverse Integer (倒转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  9. [LeetCode] 7. Reverse Integer 翻转整数

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

随机推荐

  1. cpu怎么实现运算的

      这么好玩的问题,没人回答说不过去.不懂电子元件的码农我要强答一发了.太学术的我不会,你们也听不懂.所以臭不要脸非常不严谨地科普一记.说错不许打我.另外关于这个问题,推荐<编码 (豆瓣)> ...

  2. ElasticSearch停止启动

    1.查找ES进程 ps -ef | grep elastic 2.杀掉ES进程 kill -9 2382(进程号) 3.重启ES sh elasticsearch -d 注意:elasticsearc ...

  3. 【java】private关键字

     private关键字(1)修饰成员:可以修饰成员变量和成员方法(2)特点:被private修饰的后的成员只能在本类中被访问 (3)private的应用:定义类的时候,当把成员变量给private修饰 ...

  4. 胖子哥的大数据之路(7)- 传统企业切入核心or外围

    一.引言 昨天和一个做互联网大数据(零售行业)的朋友交流,关于大数据传统企业实施的切入点产生了争执,主要围绕两个问题进行了深入的探讨: 问题1:对于一个传统企业而言什么是核心业务,什么是外围业务? 问 ...

  5. ALGO-119_蓝桥杯_算法训练_寂寞的数

    问题描述 道德经曰:一生二,二生三,三生万物. 对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和.例如,d()=++=, d()=++++=. 因此,给定了任意一个n作为起点,你可 ...

  6. window7环境MySql5.7 zip安装配置教程

    1.将zip压缩包解压到一个目录下,并改名为mysql5.7 我的是放在D:\web\mysql5.7下 2.修改my-default.ini文件 下面几项是必填的,否则无法启动 basedir = ...

  7. LeetCode——4. Median of Two Sorted Arrays

    一.题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays 二.题目大意: 给定两个排序过的数组,求出两个数组的中位数,要求时间复 ...

  8. JavaScript之图片操作6

    上一篇写的关于放大镜的,可能在实际开发中用的不是很多,接下来将的图片无缝滚动在实际工作中就是用的比较多的了. 如上图,通过定时器控制图片无缝滚动,当鼠标悬浮时停止滚动,鼠标离开,滚动继续. 主要原理是 ...

  9. spring笔记-@Primary注解

    1.问题 当一个接口有2个不同实现时,使用@Autowired注解时会报org.springframework.beans.factory.NoUniqueBeanDefinitionExceptio ...

  10. 服务链路追踪(Spring Cloud Sleuth)

    sleuth:英 [slu:θ] 美 [sluθ] n.足迹,警犬,侦探vi.做侦探 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的 ...