龙空技术网

使用python实现已有c代码

篮球鉴赏家小华 523

前言:

现在我们对“python调用c语言”大约比较关注,朋友们都想要分析一些“python调用c语言”的相关文章。那么小编也在网摘上收集了一些关于“python调用c语言””的相关资讯,希望咱们能喜欢,同学们快快来了解一下吧!

已有用c语言实现代码:

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

先罗列一下碰到的几处,C语言与python同一语义的不同实现方法:

字符串长度:

C语言:  int strTotalLen = strlen(s);python: strTatalLen = len(s)

二维数组定义并初始化:

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))

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

逻辑关系与:

 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语言完整代码:

#include<stdio.h>#include<stdlib.h>#include<string.h>char* longestPalindrome_dp(char* s){    int strTotalLen = strlen(s);    //定义并初始化二维数组    //DP关键步骤2:缓冲并复用以往结果    bool dp[strTotalLen][strTotalLen];    for (int k = 0; k < strTotalLen; k++)    {        for (int q = 0; q < strTotalLen; q++)        {            dp[k][q] = 0;        }      }    //存储最终结果    char* retStr = (char*)malloc(sizeof(int) * strTotalLen);    memset(retStr, 0x00, sizeof(char) * strTotalLen);    //DP关键步骤3:按照顺序从小往大算    for (int strLen = 0; strLen < strTotalLen; strLen++)//回文子串的长度    {        for (int strStart = 0; strStart + strLen < strTotalLen; strStart++)        {//回文子串的开始位置strStart,这样可以通过strEnd =  strStart + strLen得到子串的结束位置            int strEnd = strStart + strLen;            if (strLen == 0)//边界条件1,长度为1的子串,显然是回文子串            {                dp[strStart][strEnd] = 1;            }            else if (strLen == 1)//边界条件2,长度为2的子串,只要它的两个字母相同,是回文子串            {                dp[strStart][strEnd] = (s[strStart] == s[strEnd]);            }            else//DP关键步骤1:状态转移方程,s[strStart + 1][strEnd - 1]是回文子串            //且s[strStart] == s[strEnd]时,s[strStart][strEnd]才是回文子串            {                dp[strStart][strEnd] = (s[strStart] == s[strEnd] && dp[strStart + 1][strEnd - 1]);            }           //如果有更长的回文子串则更新            if (dp[strStart][strEnd] && strLen + 1 > strlen(retStr))            {                for (int m = strStart,n = 0; n < strLen + 1; m++,n++)                {                    retStr[n] = s[m];                    printf("retStr[%d] = %c\n",n,retStr[n]);                }                retStr[strTotalLen + 1] = '\0';            }         }       }    return retStr;}int main(){    char s[] ="ovvolevel";    char* s1 = longestPalindrome_dp(s);    printf("%s\n", s1);       return 0;}

python 完整代码:

import numpy as npclass Solution:    def longestPalindromeDp(self, s: str) -> str:        strTatalLen = len(s)        # 定义并初始化二维数组        # DP关键步骤二:缓冲并复用以往结果        dp = np.zeros((strTatalLen, strTatalLen))        retStr = ""  # 存储最终结果        # DP关键步骤三:按照顺序从小往大算        for strLen in range(strTatalLen):  # 回文子串的长度            # 回文子串的开始位置strStart,这样可以通过strEnd =  strStart + strLen得到子串的结束位置            for strStart in range(strTatalLen):                strEnd = strStart + strLen                if strEnd >= strTatalLen:                    break                if strLen == 0:  # 边界条件1,长度为1的子串,显然是回文子串                    dp[strStart][strEnd] = 1                elif strLen == 1:  # 边界条件2,长度为2的子串,只要它的两个字母相同,是回文子串                    dp[strStart][strEnd] = (s[strStart] == s[strEnd])                else:  # DP关键步骤1:状态转移方程,s[strStart + 1][strEnd - 1]是回文子串                       # 且s[strStart] == s[strEnd]时,s[strStart][strEnd]才是回文子串                    dp[strStart][strEnd] = (s[strStart] == s[strEnd] and dp[strStart + 1][strEnd - 1])                if dp[strStart][strEnd] and strLen + 1 > len(retStr):                    retStr = s[strStart:strEnd + 1]        return retStrif __name__ == "__main__":    s = "ovvolevel"    test = Solution()    s1 = test.longestPalindromeDp(s)    print(s1, end=' ')

标签: #python调用c语言 #python3 int转str #python如何调用c编写的代码 #c语言计算int长度