//经验1.字符串系列://<1>长度//C语言: int strTotalLen = strlen(s);//python: strTatalLen = len(s)//<2>判断字符串中是否有空格//c语言:while (' ' == s[i])//判断有几个前导空格 //python: s = s.lstrip()//去掉前导空格//<3>判断字符串中是否存在数字//c语言:while (s[i] >= '0' && s[i] <= '9')//python: if s[i].isdigit(): //<4>字符串转换为对应的整数 //c语言: s[i] - '0'//python: int(s[i])
//经验2.二维数组//<1>定义并初始化为同一状态://C语言: bool dp[strTotalLen][strTotalLen]; for (int k = 0; k < strTotalLen; k++) { for (int q = 0; q < strTotalLen; q++) { dp[k][q] = 0; } }//python:import numpy as npdp = np.zeros((strTatalLen, strTatalLen))//<2>定义并初始化为不同状态://C语言: #define START 0 #define SIGNED 1 #define IN_NUMBER 2 #define END 3 #define MAX_STATE 4int map[MAX_STATE][MAX_STATE] = {//将表格转换为二维数组 {START, SIGNED, IN_NUMBER, END}, {END, END, IN_NUMBER, END}, {END, END, IN_NUMBER, END}, {END, END, END, END} };int state = 0;state = map[state][START];//python://无法宏定义不同状态,可用字符串表示,使用字典索引map = { 'START': ['START', 'SIGNED', 'IN_NUMBER', 'END'], 'SIGNED': ['END', 'END', 'IN_NUMBER', 'END'], 'IN_NUMBER': ['END', 'END', 'IN_NUMBER', 'END'], 'END': ['END', 'END', 'END', 'END'] }state = 'START'state = map[state][0]
//经验3.for循环://<1>如何终止for循环// C语言: for (int strLen = 0; strLen < strTotalLen; strLen++) { for (int strStart = 0; strStart + strLen < strTotalLen; strStart++) { int strEnd = strStart + strLen;// python: for strLen in range(strTatalLen): for strStart in range(strTatalLen): strEnd = strStart + strLen if strEnd >= strTatalLen: break //<2>for循环起点调整// C语言: for(i = 1; i < len(s); i++)// python: start = 1 for i in range(start, len(s)):
//经验4.逻辑关系与:// C语言: dp[strStart][strEnd] = (s[strStart] == s[strEnd] && dp[strStart + 1][strEnd - 1]);//Python: dp[strStart][strEnd] = (s[strStart] == s[strEnd] and dp[strStart + 1][strEnd - 1]) // 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
//经验5.C语言和Python中的整除“/”和取余“%”//c语言:7 / 4 = 1//Python: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中符 。整除时,除数和被除数符 不一致时,调整完除数的符 时,要根据结果看是否调整最后结果的符 ;取余时,除数和被除数符 不一致时,由于结果与除数符 相同,只需要调整除数符 即可。*/
//经验6.C语言和Python中的32位最大整数//C语言:#define INTMAX ((unsigned)(-1)>>1)#define INTMIN (~INTMAX)//Python://Python没有宏定义 IntMax = 2 ** 31 - 1//指数^用**表示 IntMin = -2 ** 31
//经验7.三目运算符//C语言: return (flag == 1? IntMax:IntMin);//Python:return IntMax if flag == 1 else IntMin
//经验8. if-else if -else//C语言: if(' ' == s[i]) { state = map[state][START]; } else if('+' == s[i] || '-' == s[i]) { state = map[state][SIGNED]; } else if(s[i] >= '0' && s[i] <= '9') { state = map[state][IN_NUMBER]; } else { state = map[state][END]; }//Python: if(c == ' '): state = map[state][0] elif (c == '+' or c == '-'): state = map[state][1] elif(c.isdigit()): state = map[state][2] else: state = map[state][3]
下边使用python3改写一下之前的两个C语言代码:
C语言代码1:字符串转换成一个 32 位有符 整数-atoi 函数Leet
Python3代码:
class Solution: def myAtoi(self, s: str) -> int: s = s.lstrip() ans = 0 IntMax = 2 ** 31 - 1 IntMin = -2 ** 31 flag = 1 start = 0 if s == "": return 0 if '+' == s[0] or '-' == s[0]: if '-' == s[0]: flag = -1 start = 1 for i in range(start, len(s)): if s[i].isdigit(): if ans < IntMax // 10 or (ans == IntMax // 10 and int(s[i]) < 8): ans = ans * 10 + int(s[i]) else: return IntMax if flag == 1 else IntMin else: break return flag * ansif __name__ == "__main__": s = "+1" test = Solution() ans = test.myAtoi(s) print(ans)
C语言代码2:确定有限状态机(DFA)-Leet
Python3代码:
class Solution: def myAtoi(self, s: str) -> int: IntMax = 2 ** 31 - 1 IntMin = -2 ** 31 map = { 'START': ['START', 'SIGNED', 'IN_NUMBER', 'END'], 'SIGNED': ['END', 'END', 'IN_NUMBER', 'END'], 'IN_NUMBER': ['END', 'END', 'IN_NUMBER', 'END'], 'END': ['END', 'END', 'END', 'END'] } flag = 1 state = 'START' ans = 0 for c in s: if(c == ' '): state = map[state][0] elif (c == '+' or c == '-'): state = map[state][1] elif(c.isdigit()): state = map[state][2] else: state = map[state][3] if(state == 'START'): continue if(state == 'SIGNED'): if(c == '-'): flag = -1 if(state == 'IN_NUMBER'): if ans < IntMax // 10 or (ans == IntMax // 10 and int(c) < 8): ans = ans * 10 + int(c) else: return IntMax if flag == 1 else IntMin if(state == 'END'): break return flag * ansif __name__ == "__main__": s = "-91" test = Solution() ans = test.myAtoi(s) print(ans)
LeetCode系列:
leetcode2. 两数相加-c语言-python3
LeetCode4. 寻找两个正序数组的中位数
LeetCode5.0-最长回文子串-中心扩展法-C语言
LeetCode5.1-马拉车算法求解最长回文子串
LeetCode5.2-动态规划求解最长回文子串
LeetCode7.翻转整数-C语言与python的异同点
字符串转换成一个 32 位有符 整数-atoi 函数Leet
确定有限状态机(DFA)-Leet
二叉树系列:
判断二叉树是否为平衡二叉树
平衡二叉树
构建平衡二叉树
如何优雅地画好二叉树
二叉树的层序遍历及应用
二叉树遍历的思维导图
平衡二叉树的结点删除操作
不平衡二叉树的旋转(LL、RR、LR、RL)
二叉查找树(BST:Binary Search Tree)
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!