crazy bug in IDE


  • 0
    C

    Here is my code for this problem, which gives correct output on console, but shows trimmed result in returned output

    class Solution {
    public:
        string convert(string s, int numRows) {
            if(numRows <= 1) return s;
            
            string ans = "";
            
            numRows--;
            
            for(int i = 0; i <= numRows; i++){
                for(int j = 0; j < s.length();){                
                    ans += s[i+j];
                    
                    if(i != 0 && i != numRows && 2*numRows - i + j < s.length()){
                        ans += s[2*numRows - i + j]; 
                    }
                    
                    j += (2*numRows);
                }
            }
            
            cout << ans << endl;
            return ans;
        }
    };
    

    Test case :

    "ABCDE"
    3

    0_1513904843830_Screenshot from 2017-12-21 20-06-48.png


  • 0
    C

    Okay so lesson learnt : you should always check your string for spaces, it is not visible in stdout.

    The following code change in the start of inner loop got me an AC :

     if(i+j < s.length()) ans += s[i+j];
    

Log in to reply
 

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