Share a Java solution which beats 98.17%


  • 0
    L

    It is easy to solve if you get a formula about step.

    public class Solution {
        public String convert(String s, int numRows) {
            if(s.length()<=1 || numRows==1)return s;
            StringBuffer res=new StringBuffer("");
            int index,step;
            for(int r=0;r<numRows;r++){
                index=r;
                step=2*r-1;
                while(index<s.length()){
                    res.append(s.charAt(index));
                    do{
                        step=2*(numRows-2)-step;
                    }while(step<=0);
                    index+=step+1;
                }
            }
            return res.toString();
        }
    }
    

Log in to reply
 

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