Reverse Integer

想用余10直接算,没想到

-123%107, 原因 -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. c++ 函数中的部分代码执行一次

    编程时有时需要将一段代码中的某一块只执行一次: #include<iostream> using namespace std; int fun1(int a) { static bool ...

  2. 搭建Cordova + Ionic + WebStorm环境开发Web App应用

    1. 下载并且安装Node.js(https://nodejs.org/en/) 2. 打开终端,安装cordova (如果安装失败或者卡住不动则重新安装)    sudo npm install - ...

  3. ML平台_Paddle参考

    PaddlePaddle源自于 2013 年百度深度学习实验室创建的 “Paddle”.当时的深度学习框架大多只支持单 GPU 运算,对于百度这样需要对大规模数据进行处理的机构,这显然远远不够,极大拖 ...

  4. excel技巧--一键求和

    类似于上图的表格,我们要得到右侧和下侧栏的汇总结果,通常可以用sum公式加下拉方式,但是还有一种方法更快速,那就是使用 ALT + “+=”组合键就能一下子得到所有汇总结果.(+=键,就是一个键,该键 ...

  5. [1] 注解(Annotation)-- 深入理解Java:注解(Annotation)基本概念

    转载 http://www.cnblogs.com/peida/archive/2013/04/23/3036035.html 深入理解Java:注解(Annotation)基本概念 什么是注解(An ...

  6. linux下串口调试工具

    apt install cutecom 或者 serialtool 后者github上搜索

  7. 在 vmware player中安装 ubuntu 17.10

    目录 在 vmware 中安装 ubuntu 17.10 分区参考 vmware安装助手 第一步:更新 安装vmware tools 调整显示 第二步.输入法 五笔输入法 搜狗拼音输入法 ibus不能 ...

  8. 黄聪:AngularJS中的$resource使用与Restful资源交互(转)

    原文:http://blog.csdn.net/he90227/article/details/50525836 1.AngularJS中的 $resource 这个服务可以创建一个资源对象,我们可以 ...

  9. PHP echo()、print()、print_r()、var_dump()、var_export()的区别

    PHP中echo.print.print_r.var_dump.var_export的用法与区别 这些均为输出变量的内容: echo();不是函数.是PHP语句.没有返回值:输出一个或者多个字符串或者 ...

  10. C/C++基础----拷贝控制

    拷贝控制操作,有5个特殊成员函数copy ctor,copy =opt,move ctor,move =opt,dtor 有哪些地方会用到 拷贝初始化 除了=定义变量时 参数传递和函数返回时 花括号列 ...