LeetCode7.翻转整数-C语言与python的异同点

首先,给出c语言和python同一语义的不同表达方式,然后再使用c语言和python分别解LeetCode题:LeetCode7.翻转整数。

C语言和Python中的整除“/”和取余“%”

c语言:7 / 4 = 1Python:7 // 4 = 1 //Python中 7 / 4 = 1.75 表示浮点数除法;双斜杠 //表示整数除法

再看一下符 对整除是否有影响:

c语言:                   python: 7 / 4 = 1                 7 // 4 = 1   -7 / 4 = -1	          -7 // 4 = -27 / -4 = -1                7 // -4 = -2-7 / -4 = 1                 -7 // -4 = 1

可以看出,除数和被除数符 不同时,计算结果是不同的。

再看一下,取余C语言和python是否有区别:

c语言:                         python: 7 % 4 = 3                     7 % 4 = 3  -7 % 4 = -3	               -7 % 4 = 17 % -4 = -3                   7 % -4 = -1-7 % -4 = -3                 -7 % -4 = -3

可以看出,除数和被除数符 不同时,计算结果是不同的。

结论:

当除数与被除数符 一致时,整除和 取余运算在c语言和python中所得结果是一样的;

当除数与被除数符 不一致时,注意调整python中符 。

整除时,除数和被除数符 不一致时,调整完除数的符 时,要根据结果看是否调整最后结果的符 ;

取余时,除数和被除数符 不一致时,由于结果与除数符 相同,只需要调整除数符 即可。

C语言和Python中的“与或非”

C语言:  ||   与&&if (ans > IntMax / 10 || (ans == IntMax / 10 && pop > IntMax % 10)) return 0;if (ans < IntMin / 10 || (ans == IntMin / 10 && pop < IntMin % 10)) return 0;Python: 或or  与andif ans > IntMax // 10 or (ans == IntMax // 10 and pop > IntMax % 10):     return 0 if ans < -(IntMin // -10) or (ans == -(IntMin // -10) and pop < IntMax % -10):     return 0

C语言和Python中的32位最大整数

C语言:#define INTMAX  ((unsigned)(-1)>>1)#define INTMIN  (~INTMAX)Python://Python没有宏定义 IntMax = 2 ** 31 - 1//指数^用**表示 IntMin = -2 ** 31

LeetCode7.整数翻转

给你一个 32 位的有符 整数 x ,返回 x 中每位上的数字反转后的结果。如果反转后整数超过 32 位的有符 整数的范围 [?2^31, 2^31 ? 1] ,就返回 0。假设环境不允许存储 64 位整数(有符 或无符 )。

示例 1:

输入:x = 123

输出:321

示例 2:

输入:x = -123

输出:-321

示例 3:

输入:x = 120

输出:21

示例 4:

输入:x = 0

输出:0

思路分析:

我们可以一次构建反转整数的一位数字。在这样做的时候,我们可以预先检查向原整数附加另一位数字是否会导致溢出。反转整数的方法可以与反转字符串进行类比。我们想重复“弹出” x 的最后一位数字,并将它“推入”到 ans 的后面。最后,ans 将与 x 相反。要在没有辅助堆栈 / 数组的帮助下 “弹出” 和 “推入” 数字,我们可以使用数学方法。

弹出:int pop = x % 10;x /= 10; 推入:ans = ans * 10 + pop;

但是,这种方法很危险,因为当ans = ans * 10 + pop; 时会导致溢出。但是我们可以事先检查这个语句是否会导致溢出很容易。在代码ans = ans * 10 + pop;前边加入检查:

if ans > IntMax // 10 or (ans == IntMax // 10 and pop > IntMax % 10):     return 0if ans < -(IntMin // -10) or (ans == -(IntMin // -10) and pop < IntMax % -10):      return 0

下边给出C语言和python 的完整代码:

c语言:

#include<stdio.h>#include<stdlib.h>#define INTMAX  ((unsigned)(-1)>>1)#define INTMIN  (~INTMAX)int reverse(int x){    int ans = 0;    int IntMax = INTMAX;    int IntMin = INTMIN;    while (x != 0)    {       int pop = x % 10;       x /= 10;       if (ans > IntMax / 10 || (ans == IntMax / 10 && pop > IntMax % 10)) return 0;       if (ans < IntMin / 10 || (ans == IntMin / 10 && pop < IntMin % 10)) return 0;       ans = ans * 10 + pop;    }    return ans;}int main(){    int x = -2147483647;    int ans = reverse(x);    printf("%dn", ans);    return 0;}

Python:

class Solution:    def reverse(self, x: int) -> int:        ans = 0        IntMax = 2 ** 31 - 1        IntMin = -2 ** 31        while x != 0:            if x > 0:                pop = x % 10                x //= 10            else:                pop = x % -10                x = -(x // -10)            if ans > IntMax // 10 or (ans == IntMax // 10 and pop > IntMax % 10):                return 0            if ans < -(IntMin // -10) or (ans == -(IntMin // -10) and pop < IntMax % -10):                return 0            ans = ans * 10 + pop        return ansif __name__ == "__main__":    x = 123    test = Solution()    ans = test.reverse(x)    print(ans)

leetcode2. 两数相加-c语言-python3

LeetCode4. 寻找两个正序数组的中位数

LeetCode5.0-最长回文子串-中心扩展法-C语言

LeetCode5.1-马拉车算法求解最长回文子串

LeetCode5.2-动态规划求解最长回文子串

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月12日
下一篇 2021年1月12日

相关推荐