【问题标题】:What is the normal way to debug nested for loop?调试嵌套 for 循环的正常方法是什么?
【发布时间】:2020-07-17 06:28:48
【问题描述】:

我正在为 Leetcode 问题 38 编写代码。数数并说。它没有通过案例,所以我添加了一些cout 进行调试。请告诉我是否有调试嵌套 for 循环的正常方法,我应该在哪里添加 cout 表达式。我不想知道如何修改代码以通过案例。
这是我的代码:

class Solution {
public:
    string countAndSay(int n) {
        string cur("1");
        while (--n) {
            string tmp = cur;
            string next;
            for (int i = 0; i < tmp.size();) {
                cout << "i:" << i << endl;
                int count = 1;
                for (int j = i + 1; j < tmp.size(); j++) {
                    if (tmp[j] != tmp[0]) {
                        break;
                    }
                    count++;
                }
                cout << "count:" << count << endl;
                next += std::to_string(count) + tmp[0];
                cout << "cur:" << cur << endl;
                i += count;
            }
            cur = next;
            cout << n << cur << endl;
        }
        
        return cur;
        
    }
};

【问题讨论】:

    标签: c++ debugging nested-loops cout


    【解决方案1】:

    您将不得不为此使用调试器,并逐步检查您的算法以查找错误。别人的算法很难调试。

    这会通过:

    #include <string>
    
    struct Solution {
        static const std::string countAndSay(int n) {
            if (not n) {
                return "";
            }
    
            std::string res = "1";
    
            while (--n) {
                std::string curr = "";
    
                for (int index = 0; index < res.size(); index++) {
                    int count = 1;
    
                    while ((index + 1 < res.size()) and (res[index] == res[index + 1])) {
                        count++;
                        index++;
                    }
    
                    curr += std::to_string(count) + res[index];
                }
    
                res = curr;
            }
    
            return res;
        }
    };
    

    Java 解决方案

    class Solution {
        public String countAndSay(int n) {
            if (n == 1)
                return "1";
            String prev = countAndSay(n - 1);
            StringBuilder str = new StringBuilder();
            int i = 0;
            while (i < prev.length()) {
                char curr = prev.charAt(i);
                int j = 0;
                while (i + j < prev.length() && prev.charAt(i + j) == curr)
                    j++;
                str.append(j);
                str.append(curr);
                i += j;
            }
            return str.toString();
        }
    }
    

    这是 LeetCode 使用正则表达式的解决方案之一:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    class Solution {
      public String countAndSay(int n) {
        String currSeq = "1";
    
        // Pattern to match the repetitive digits
        String regexPattern = "(.)\\1*";
        Pattern pattern = Pattern.compile(regexPattern);
    
        for (int i = 1; i < n; ++i) {
          Matcher m = pattern.matcher(currSeq);
          StringBuffer nextSeq = new StringBuffer();
    
          // each group contains identical and adjacent digits
          while (m.find()) {
            nextSeq.append(m.group().length() + String.valueOf(m.group().charAt(0)));
          }
          // prepare for the next iteration
          currSeq = nextSeq.toString();
        }
    
        return currSeq;
      }
    }
    

    这是另一个 LeetCode 的解决方案,也使用了滑动窗口:

    class Solution {
      public String countAndSay(int n) {
    
        LinkedList<Integer> prevSeq = new LinkedList<Integer>();
        prevSeq.add(1);
        // Using -1 as the delimiter
        prevSeq.add(-1);
    
        List<Integer> finalSeq = this.nextSequence(n, prevSeq);
        StringBuffer seqStr = new StringBuffer();
        for (Integer digit : finalSeq) {
          seqStr.append(String.valueOf(digit));
        }
        return seqStr.toString();
      }
    
      protected LinkedList<Integer> nextSequence(int n, LinkedList<Integer> prevSeq) {
        if (n <= 1) {
          // remove the delimiter before return
          prevSeq.pollLast();
          return prevSeq;
        }
    
        LinkedList<Integer> nextSeq = new LinkedList<Integer>();
        Integer prevDigit = null;
        Integer digitCnt = 0;
        for (Integer digit : prevSeq) {
          if (prevDigit == null) {
            prevDigit = digit;
            digitCnt += 1;
          } else if (digit == prevDigit) {
            // in the middle of the sub-sequence
            digitCnt += 1;
          } else {
            // end of a sub-sequence
            nextSeq.add(digitCnt);
            nextSeq.add(prevDigit);
            // reset for the next sub-sequence
            prevDigit = digit;
            digitCnt = 1;
          }
        }
    
        // add the delimiter for the next recursion
        nextSeq.add(-1);
        return this.nextSequence(n - 1, nextSeq);
      }
    }
    

    参考

    • 有关其他详细信息,您可以查看Discussion Board。有很多公认的解决方案,有各种languages 和解释、高效的算法,以及渐近的time/space 复杂性分析1, 2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多