The expected one is the same to the output,but fails!


  • 1
    H

    Here is my code,the output is the same to the expected .i don't why it failed.Anyone can help?

    class Solution {
    public:
        string convert(string s,int numRows){
        if(s.length()<3) return s;    
    	int rows = s.length(),lines=numRows;
    	char data[1000][1000] = {0};
    	int count = 0;
    	string ans;
    	int i=0,j=0,z=0;
    	while(count < s.length()){
    		for (j = 0; j < lines; ++j)
    		{
    			data[j][i] = s[count++];
    		}
    		i++;
    		for (z = j-2; z > 0; z--,i++)
    		{
    			data[z][i] = s[count++];
    		}
    	}
    	for (int i = 0; i < lines; ++i)
    	{
    		for (int j = 0; j < rows; ++j)
    		{
    			if(data[i][j]!=0) ans.push_back(data[i][j]);
    		}
    	}
    	ans.push_back('\0');
    	return ans;
        }
    };

  • 0
    P
    This post is deleted!

  • 0
    P

    I got the same error, but for Java.

    For the input "A", my output is "A" and the expected output is also "A" but I get an error.


  • 0
    S

    Even i faced the same issue. I got around that with a slight modification in my code.


  • 0
    H

    i deleted the last line code ans.push_back('\0'),but it's wrong too.
    even i printed every int vlaue(some chara can't printed),the output is the same to the expected.


  • 0
    T

    hello! i got the same error, who can tell me why?
    class Solution {
    public:
    string convert(string s, int numRows) {
    if (1 == numRows || s.size() == 1)
    return s;

    	int index=0;
    	int N;//the group counts ,each group has (numRows+1) members.
    	int strLen = s.size();
    	string retStr(strLen + 1, '\0');
    	int mid;//middle rows
    	if (numRows%2)//odd rows
    		mid = numRows / 2;
    	else
    		mid = numRows / 2 -1;
    
    	N = strLen / (numRows+1);// N may be zero when the size of the string <= numRows 
    	if (!N)
    		return s;
    
    	if (strLen % N)
    		N += 1;
    
    	for (int j = 0; j < numRows; ++j)//j is the offset of the member in the group
    
    	{
    		for (int i = 0; i < N; ++i)//i is the offset of the group
    		{
    			int m = i * (numRows+1) + j;
    			if (m < strLen)
    			{
    				retStr[index++] = s[m];
    				if (j == mid)
    				{
    					int mm = i * (numRows + 1) + numRows;
    					if (mm < strLen)
    						retStr[index++] = s[mm];
    				}
    			}
    		}
    	}
    
    	return retStr;
    }
    

    };


  • 0
    Y

    I also get the same problem,I get the right answer in Eclipse,but in their environment,my answer is wrong.Here is my code:

    public class Solution {
    public String convert(String s, int numRows) {
    if(s==null || s.length()==0) return "";
    if(s.length()<=numRows || numRows==1) return s;
    char[] mChar=s.toCharArray();
    String str="";
    for (int i = 0; i < numRows/2; i++) {
    for (int j = i; j < mChar.length; j+=(numRows+1)) {
    str+=mChar[j];
    }

    	}
    	 if(numRows%2==0){
    	 for (int j = numRows; j < mChar.length; j+=(numRows+1)) {
    			str+=mChar[j];
    		}
    	 for (int i = numRows/2; i < numRows; i++) {
    		 for (int j = i; j < mChar.length; j+=(numRows+1)) {
    				str+=mChar[j];
    			}	
    		 
    	}
    	 }else{//单数
    		 for (int j = numRows/2; j < mChar.length; j+=((numRows+1)/2)) {
    				str+=mChar[j];
    			}
    		 for (int i = numRows/2+1; i < numRows; i++) {
    			 for (int j = i; j < mChar.length; j+=(numRows+1)) {
    					str+=mChar[j];
    				}	
    			 
    		}
    	 }
    	
    	return str;
    }
    

    }


Log in to reply
 

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