This is a slow but I think is easily understanding version


  • 0
    N
    public String convert(String s, int numRows) {
       if(numRows == 1){
    		return s;
    	}
    	List<List<Character>> container = new ArrayList<List<Character>>(numRows);
    	for(int i = 0;i<numRows;i++){
    		container.add(new ArrayList<Character>());
    	}
    	int index = 0;
    	int listIndex = 0;
    	boolean goDown = true;
    	while(index<s.length()){
    		if(goDown){
    			List<Character> list = container.get(listIndex++);
    			list.add(s.charAt(index++));
    			if(listIndex==numRows){
    				goDown = false;
    				listIndex = listIndex -2;
    			}
    		}else{
    			List<Character> list = container.get(listIndex--);
    			list.add(s.charAt(index++));
    			if(listIndex==-1){
    				listIndex = listIndex+2;
    				goDown = true;
    			}
    		}
    	}
    	StringBuilder sb = new StringBuilder();
    	for(List<Character> list:container){
    		for(char c:list){
    			sb.append(c);
    		}
    	}
    	return sb.toString();
    }

Log in to reply
 

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