Share my java accepted solution, i think it is not intuitive,could anyone point out the improvement


  • 0
    S
    public class Solution {
        public String convert(String s, int nRows) {
            String str = "";
            
            if(s.length() < 3 || nRows == 1) {
                return s;
            }
            for(int i=0; i<nRows; i++) {
                for(int j=i; j<s.length(); j+=(2*nRows-2)) {
                    str += s.charAt(j);
                    if(i==0 || i == nRows-1) {
                        continue;
                    }
                    if((2*(nRows-i)+j-2) < s.length()) {
                        str += s.charAt(2*(nRows-i)+j-2);
                    }
                }
            }
            return str;
        }
    }

Log in to reply
 

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