LeetCode 7 Reverse Integer & int
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
所以直接存符号吧。
第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的更多相关文章
- LeetCode 7 Reverse Integer int:2147483647-2147483648 难度:2
https://leetcode.com/problems/reverse-integer/ class Solution { public: int inf = ~0u >> 1; in ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- LeetCode 7. Reverse Integer (倒转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
随机推荐
- nusaop 关于webService
用PHP和NuSoap来建立SOAP服务器非常容易.基本上,你只要写出你想要暴露给你的Web services的函数,然后用NuSoap去注册它们就可以了.OK,另外还需要两步才能完成PHP SOAP ...
- java面试题001
1.指针和函数的关系 这里主要谈指针函数和函数指针,在c中指针函数是返回值为指针的函数:函数指针是指向函数的指针变量. 2.什么是事务? 为了完成对数据的操作,要求并发访问在多个构件之间共享的数据.这 ...
- FPGA中关于SPI的使用
FPGA中关于SPI的使用 信息来源 SPI Flash的编程 最新的SPI不止有4根信号线,可以增加到支持4bit的数据宽度 SPI Flash Basics 能够扩展成4bit数据的是MOSI信号
- a:hover应用精粹
原本想把题目叫做“纯CSS相册2”的,但在实现过程中试验了许多东西,干脆全部写出来分享了.大家知道,能兼容IE6的具有动态切换能力的CSS属性也只有hover伪类了,但hover伪类在IE仅对链接生效 ...
- STL基础--流
流 介绍 // cout: 全局ostream对象,(typedef basic_ostream<char> ostream) // <<: ostream& ostr ...
- STL基础--基本介绍
为什么要使用C++标准库 /* * 为什么使用C++标准库: * 1. 代码重用,不用重新造轮子 * 2. 效率(快速,且使用更少的资源). 现代C++编译器经常对C++标准库的代码有优化 * 3. ...
- vc++获取网页源码之使用类型库(TypeLib)生成包装类
1.在MFC项目名称上 右击->添加->选择Visual C++下的MFC->TypeLib中的MFC类->添加 可以从注册表表中共或是文件中根据相应的接口生成对应的包装类 效 ...
- pandoc 基本使用
pandoc –s 输入文件.后缀 –o 输出文件.后缀
- 服务容错保护断路器Hystrix之二:Hystrix工作流程解析
一.总运行流程 当你发出请求后,hystrix是这么运行的 红圈 :Hystrix 命令执行失败,执行回退逻辑.也就是大家经常在文章中看到的“服务降级”. 绿圈 :四种情况会触发失败回退逻辑( fal ...
- [UE4]widget事件:On Mouse Enter、On Move Leave、Set Color And Opactiy
只要是widget对象,都具有On Mouse Enter.On Move Leave事件