Is this an OJ issue?


  • 0
    L

    I got a "Wrong Answer" like this:

    Input:		"A", 2
    Output:		"A"
    Expected:	"A"
    

    OJ issue? or my code issue? Thanks for help.

    Here is my code:

    class Solution {
    public:
    	string convert(string s, int nRows) {
    		if (s.empty() || nRows <= 1) return s;
    
    		int sLen = s.size();
    		vector<vector<char>> aux;
    		for (int i = 0; i < nRows; i++) aux.push_back(vector<char>());
    
    		int rowMod = nRows - 1;
    		for (int i = 0; i < sLen; i++) {
    			if (i % (2 * rowMod) == 0) {
    				int j = i, idx = 0;
    				while (idx < nRows) aux[idx++].push_back(s[j++]);
    				i = j - 1;
    			} else {
    				int idx = i % rowMod;
    				aux[rowMod - idx].push_back(s[i]);
    			}
    		}
    
    		vector<char> rsltVec;
    		for (int i = 0; i < nRows; i++) 
                rsltVec.insert(rsltVec.end(), aux[i].begin(), aux[i].end());
    
    		string ret(rsltVec.begin(), rsltVec.end());
    		return ret;
    	}
    };
    
    int main(int argc, char **argv) {
    	if (argc != 3) {
    		cerr << "Usage:\n\t" << argv[0] << " <string> <rowNum>" << endl;
    		return 1;
    	}
    
    	string str(argv[1]);
    	int nRows = atoi(argv[2]);
    
    	Solution s;
    	cout << s.convert(str, nRows) << endl;
    
    	return 0;
    }

  • 0

    Please read the FAQ, especially the question: "Why is my output wrong answer even though it looks identical to expected?"


  • 0
    L

    I got it:

    while (idx < nRows) aux[idx++].push_back(s[j++]);

    should be:

    while (j < sLen && idx < nRows) aux[idx++].push_back(s[j++]);

    Thanks @1337c0d3r!


Log in to reply
 

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