java solution with simple if-else


  • 0
    B
    class Solution {
        public String convert(String s, int numRows) {
            if(numRows==1) return s;
            else {
                StringBuilder sb = new StringBuilder();
                int num=numRows;
                int maxnum=2*(num-1);
                for(int i=0; i<s.length() && numRows>0; i++){
                    for(int j=i,k=0; j<s.length();k++)
                    {
                        sb.append(s.charAt(j));
                        if(i==0||i==num-1) {
                            j+=maxnum;
                            continue;
                        }
                        if((k&1)!=0) j+=maxnum-2*(numRows-1);
                        else j+=2*(numRows-1);
                    }
                    numRows--;
                }
                return sb.toString();
            }       
        }
    }
    

Log in to reply
 

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