【问题标题】:Cumulative approach for finding longest common prefix using C not working使用 C 查找最长公共前缀的累积方法不起作用
【发布时间】:2021-09-15 06:55:59
【问题描述】:

我正在尝试将我用 C++ 编写的代码用于解决一些基本问题,然后用其他语言重新编写,以便更熟悉这些其他语言的语法和库函数。

我目前正在解决 leetcode 中的一个问题,我知道我可以找到解决问题的方法,但想在此之前提出我自己的可行解决方案。

这个基本问题是在字符串数组中找到最长的前缀。就像我说的,我正在使用我的 C++ 工作解决方案并尝试翻译成另一种语言。在这种情况下,我试图将解决方案翻译成 C。

我正在使用基本的累加器方法。但是,与我的 C++ 版本不同,C 中的版本返回整个第一个工作,而不是前缀。例如,如果数组是 ["run", "rude", "running"],那么它应该返回 "ru",但它返回的是 "run"。

我认为这与我复制、附加或分配值的方式有关,但目前我看不出问题出在哪里。

非常感谢任何想法和帮助。

这是我的代码:

// Variables
char * longestCommonPrefix(char ** strs, int strsSize){
char *result = (char *) malloc(sizeof(char));
char current = ' ';

// Find smallest word 
int smallestWordSize = 201;
int size = sizeof(strs) / sizeof *strs;
for (int index = 0; index < size; ++index) {
        if (strlen(strs[index]) < smallestWordSize) {
            smallestWordSize = strlen(strs[index]);
        }
    }  

// For case where there is only one element
if(size == 1){
    result = strs[0];
}

// Create result string
else {
    for (int i = 0; i < smallestWordSize; ++i) {
     strncpy(current, strs[0][i], 1);
        for (int j = 1; j < size; ++j) {
            if (current != strs[j][i]) {
                return result;
            }
        }
        strncat(result, current, 1);
                  
        }
    }


return result;

}

是的,我意识到我没有使用参数 strsSize,因为在 C++ 解决方案中,它没有那个参数,我正在尝试尽可能多地从一种语言直接翻译到另一种语言。

【问题讨论】:

  • 无论使用哪种语言,您可以获得的最佳建议是学习有效地调试。在调试器中运行您的程序并在它运行时跟踪它以找出它开始出错的地方。 How to debug small small programs.
  • 建议:不要只学习 leetcode。在本地构建和调试它。实际上,一旦出现问题,您就没有调试工具。在“现实生活”中,调试步骤与编写代码本身一样重要(如果不是更重要的话)。
  • terrylt1, char ** strs ... int size = sizeof(strs) / sizeof *strs; 为 1。指针大小除以指针大小。你肯定不想要这个。
  • 您必须自己跟踪大小并将其作为额外参数传递给在数组上工作的函数。或者使用一个特殊的值来标记数组的结尾,比如字符串中的 nul 字符。
  • result = strs[0]; 会导致内存泄漏,并且当您尝试一次多次释放字符串时可能会导致 UD。

标签: arrays c append accumulate strncpy


【解决方案1】:

所以,经过努力,我终于想出了一个解决方案。在这个过程中,我确实发现了更简单的解决方案,但正如我所说,我希望这与我在 C++ 中所做的累加器方法相匹配。这是我想出来的,给那些可能感兴趣的人。

char * longestCommonPrefix(char ** strs, int strsSize){
char current = ' ';

int smallestWordSize = 201;
for (int index = 0; index < strsSize; ++index) {
        if (strlen(strs[index]) < smallestWordSize) {
            smallestWordSize = strlen(strs[index]);
        }
    }

    char *result = malloc(sizeof(char) * smallestWordSize +1);
    result[0] = '\0';

    if(strsSize == 0) {
      return result;
    }

     if(strsSize == 1){
           result = strs[0];
           return result;
      }

      int i = 0;
      bool match = true;

      for (i = 0; i < smallestWordSize && match; ++i) {
          current = strs[0][i];

          for (int j = 1; j < strsSize && match; ++j) {
                if (current != strs[j][i]) {
                     i--;
                     match = false;
                }
          }
      }

      if (i > 0){
           strncat(result, strs[0], i);
      }
      else {
           result = "";
           return result;;
      }

 return result;

}

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 2018-09-30
    • 2021-09-11
    • 2022-01-11
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2020-07-05
    • 2011-12-25
    相关资源
    最近更新 更多