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 ...
随机推荐
- Maven下载项目依赖jar包和使用方法
一.Maven3.5.0安装与配置+Eclipse应用 参考:Maven3.5.0安装与配置+Eclipse应用 二.http://mvnrepository.com/ 此处以http://mvnre ...
- 关于java的一些小知识(课程作业01)
1,在java源代码中加空格注释不会影响程序的编译速度. 2,一个java类文件中真的只能有一个公有类吗? 如果只在第一个public类里面定义实体,或者两个都不定义并没有报错且程序可以运行.每个编译 ...
- mysql++ 3.2.3 源码安装
https://tangentsoft.com/mysqlpp/home 下载 wget https://tangentsoft.com/mysqlpp/releases/mysql++-3.2.3. ...
- STL基础--算法(不修改数据的算法)
不修改数据的算法 count, min and max, compare, linear search, attribute // 算法中Lambda函数很常用: num = count_if(vec ...
- neo4j---删除关系和节点
本文转载自:https://blog.csdn.net/chenjf0221/article/details/70238695 删除节点和节点关系 MATCH (a:key)-[r:KEY_WORD] ...
- 【转】探索 ConcurrentHashMap 高并发性的实现机制
原文链接:https://www.ibm.com/developerworks/cn/java/java-lo-concurrenthashmap/ <探索 ConcurrentHashMap ...
- Hadoop错误集:Could not find the main class: org.apache.hadoop.*
在搭建Hadoop机群的时候,之前遇见了很多次找不到类的错误,因为对Hadoop了解不深,所以就在网上漫无目的的找解决方案,所以这里总结下我用的方法. 解决办法一: 我之前遇到了找不到 org.apa ...
- CodeLite C/C+ IDE更新放出
CodeLite IDE Revision 1145 for C/C++已经发布,这是一个强大的开源,跨平台的C/C++整合开发环境.目前已经在Windows XP SP3,Ubuntu 7.10 G ...
- 基于Kafka消息驱动最终一致事务(二)
实现用例分析 上篇基于Kafka消息驱动最终一致事务(一)介绍BASE的理论,接着我们引入一个实例看如何实现BASE,我们会用图7显示的算法实现BASE.
- 《Effective Java》笔记-服务提供者框架
静态工厂方法返回的对象所属的类,在编写包含该静态工厂方法的类时可以不必存在.这种灵活的静态工厂方法构成了服务提供者框架(Service Provider Framework)的基础,例如JDBC AP ...