ZigZag Conversion by C++ vector


  • 0
    Z

    class Solution {
    public:
    string convert(string s, int numRows) {
    string q = "";
    vector<vector<char>>p(numRows);
    int n = 0;
    int count = 0;

    	if (s.size() == 0)
    		return q;
    	else if (numRows == 1)
    		return s;
        else if(numRows>s.size())
            return s;
    	else{
    		
    			for (n = 0;; n++){
    				
    					for (int i = 0; i < numRows; i++){
    						if (count < s.size()){
    						if (n % (numRows - 1) == 0){
    							p[i].push_back(s[count]);
    							
    							++count;
    						}
    						else if (n % (numRows - 1) == numRows - i - 1){
    							p[i].push_back(s[count]);
    							
    							++count;
    							break;
    						
    						}
    						else continue;
    					}
    		
    
    				}
    					if (count >= s.size())
    						break;
    		}
    	}
    	
    	for (int j = 0; j < numRows; j++)
    	for (int i = 0; i <p[j].size(); i++){
    		
    			q += p[j][i];
    	}
    	return q;
    }
    

    };>! >! Spoiler


Log in to reply
 

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