[Explained] Straightforward Java solution [ACCEPTED at 520ms]


  • 3
    A

    Basically the idea is to create an array of Strings equal to the 'numRows'. As we iterate through the given input string, we keep on appending each char from the input string to the strings stored in the array. The nested if statements help us switch the strings at each step in the for loop iteration.

    public String convert(String s, int numRows) {
    		String[] rows = new String[numRows]; // Strings are like 'rows'
    		for (int i = 0; i < rows.length; i++) {
    			rows[i] = ""; // initialize each to empty string
    		}
    
    		int row = 0; // this is going to keep track of which 'row' we are on
    		boolean dir = true; // positive indicates we are going down the rows and
    							// viceversa
    
    		for (int i = 0; i < s.length(); i++) {
    			rows[row] += s.charAt(i);
    
    			// Change the row based on which direction we are going
    			if (numRows > 1) { // no need to change row if only one row
    				if (row == 0) { // if we are at row no.0, next char should be
    								// saved in row no.1
    					row++;
    					dir = true; // true since we are going down
    				} else if (row == numRows - 1) { // similarly if we are at the
    													// lowest "row"
    					row--;
    					dir = false;
    				} else if (dir) {
    					// else we are midway in the "row" stack. So if we were
    					// going down the stacks, we increase the row
    					row++;
    				} else {
    					row--;
    				}
    			}
    		}
    		String result = "";
    		for (int i = 0; i < rows.length; i++) {
    			result += rows[i];
    		}
    		return result;
    	}

  • 0
    B

    This problem is in fact not clear. The definition of Zigzag is not given.


  • 0
    A

    The question has an example of how zigzag works. Take a look at it.


Log in to reply
 

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