I don't konw what it mean.it say "wrong answer, input["A",2],output"A",expected "A""


  • 1
    H
    class Solution {
    public:
    	string convert(string s, int nRows) {
    		if (s.empty() || nRows <= 0)
    		{
    			return "";
    		}
    		if (nRows == 1)
    		{
    			return s;
    		}
    		string zipS = "";
    		int len = s.length();
    		int loc = 0;
    		for (int k = 0; k < nRows; k++)
    		{
    			if (k == 0 || k == nRows - 1)
    			{
    				zipS += s[k];
    				loc = k;
    				while(loc+2*nRows-2 < len)
    				{
    					loc += 2*nRows-2;
    					zipS += s[loc];
    					
    				}
    			}
    			else
    			{
    				int j = 1;
    				zipS += s[k];
    				loc = k;
    				while(((j%2==1) && (loc+2*(nRows-k-1)) < len) ||(((j%2) == 0) && (loc+2*k) < len))
    				{
    					if (j%2 == 1)
    					{
    						zipS += s[loc+2*(nRows-k-1)];
    						loc += 2*(nRows-k-1);
    					}
    					else
    					{
    						zipS += s[loc+2*k];
    						loc += 2*k;
    					}
    					j++;
    				}
    			}
    		}
    	return zipS;
    	}
    };

  • 0
    S

    Checkout https://oj.leetcode.com/faq/ and pay attention to Why is my output wrong answer even though it looks identical to expected?


Log in to reply
 

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