ZigZag conversion runs well in gcc v3.4.4 but failed when submit


  • 0
    X

    My code is:

    class Solution {
    public:
        string convert(string s, int nRows) {
            vector<string> row_str(nRows);
            int row   = 0;
            int trend = 1;
            
            if (s.size() <= nRows) {
                return s;
            }  
            
            for (int i=0; i<s.size(); i++) {
                row_str[row] += s[i];
                switch (trend) {
                case 0:
                    row--;
                    if (row <= 0) {
                        trend = 1;
                    }
                    break;
                case 1:
                    row++;
                    if (row >= nRows-1) {
                        trend = 0;
                    }
                    break;
                default:
                    break;
                }
            }
            
            string result;
            for (int i=0; i<nRows; i++) {
                result += row_str[i];
            }
    
            return result;        
        }
    };
    

    which runs well in gcc v3.4.4. But the Leetcode system submit feedback gives error:

    Submission Result: Runtime Error

    Last executed input: "AB", 2

    So, where's the problem?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.