Easy javasubmission with chinese comments


  • 0
    J
    public class Solution {
        public String convert(String s, int numRows) {
            if(s.length()==0||s==null||numRows<=0) return "";
            if(numRows==1) return s;
            StringBuilder sb=new StringBuilder();
            int m=numRows*2-2;//垂直列的间隔始终是m
            for(int i=0;i<numRows;i++){//按行添加,便于输出
                for(int j=i;j<s.length();j+=m){
                    sb.append(s.charAt(j));//将该行处于垂直列的元素全部添加
                    if(i!=0&&i!=numRows-1){//处理斜行,第一行和最后一行没有斜行元素
                        int k=j+m-2*i;
                        if(k<s.length()){
                            sb.append(s.charAt(k)); 
                        }
                    }
                }
            }
            return sb.toString();
            
        }
    }

Log in to reply
 

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