MY Accepted JAVA Solution


  • -1
    H
    public class Solution {
        public String convert(String s, int nRows) {
            if(nRows == 1){
                return s;
            }
            else{
                char[] myarr = s.toCharArray();
                int len = myarr.length;
                String[] mystrarr = new String[nRows];
                for(int m = 0; m < nRows; m++){
                	mystrarr[m] = new String();
                }
                String result = new String();
                int periodlen = 2*(nRows-1);
                for(int i = 0; i < len; i++){
                    int diff = i%periodlen;
                    for(int j = 0; j < nRows; j++){
                        if(diff == j || diff +j == periodlen){
                            mystrarr[j] += myarr[i];
                        }
                    }
                }
                for(int n = 0; n < nRows; n++){
                    result += mystrarr[n];
                }
                return result;
            }
            
        }
    }

Log in to reply
 

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