首頁>技術>
//經驗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語言程式碼:

Python3程式碼:

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系列:

二叉樹系列:

9
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 程式開發中命名的正確姿勢