JAVA solution--easy and clear ( interesting approach )


  • 46
    J
    public class Solution {
    public String convert(String s, int numRows) {
        if(numRows<=1)return s;
        StringBuilder[] sb=new StringBuilder[numRows];
        for(int i=0;i<sb.length;i++){
            sb[i]=new StringBuilder("");   //init every sb element **important step!!!!
        }
        int incre=1;
        int index=0;
        for(int i=0;i<s.length();i++){
            sb[index].append(s.charAt(i));
            if(index==0){incre=1;}
            if(index==numRows-1){incre=-1;}
            index+=incre;
        }
        String re="";
        for(int i=0;i<sb.length;i++){
            re+=sb[i];
        }
        return re.toString();
    }
    

    }


  • 0
    S

    Thanks JenniferZZZ,this answer is really understandable and easy.


  • 0
    J

    You are welcome:)


  • 0
    C

    Thanks your perfect answer


  • 0
    J

    You are welcome:)


  • 0
    A

    What an easy and uncomplicated solution you came up with!!!. Great approach.


  • 0
    L

    when I see you answer ,I want to say:"hey,man,you are perfect and I want to make friend with you",although I am a Chinease


  • 0
    J

    Haha, thanks! I am Chinese too!


  • 0
    J

    Cool approach.


  • 0
    R

    thanks for your answer. Such an easy answer!


  • 0
    I

    really nice solution , cheers :)


  • 0

    Thanks for your idea


  • 0
    D

    Good Approach.


  • 0
    A

    @JenniferZZZ Awesome approach thanks for the solution


  • 3
    H

    Great answer, thank you! Some thought:

    1. The "numRows <= 1" line is very important. This algorithm is not working when numRows = 1
    2. You can initialize sb[i] with "new StringBuilder()"
    3. You can append sb[1], sb[2],....sb[numRow -1] to sb[0], then return sb[0].toString()

  • 0
    G

    said in JAVA solution--easy and clear ( interesting approach ):

    int incre=1;

    very cool solution, concise and no nested loop, much easier in terms of bug-free. really love the idea using incre = +1 or -1 to change the direction.

    thanks


  • 0
    D
    Same idea.
    int len = s.length();
            if (len ==0 || numRows <=1) {return s;}
            String[] ans = new String[numRows];
            Arrays.fill(ans,"");
            int row = 0;
            int direction = 1;
            for (int i = 0; i< len; i++) {
                ans[row] += s.charAt(i);
                row += direction;
                if (row >= numRows) {
                row = numRows - 2;
                direction = -1;
            }
             if (row < 0) {
                row = 1;
                direction = 1;
            }
            }
            
            String res = "";
            for (int i =0; i < numRows; i++) {
                res += ans[i];
            }
            return res;

  • 0

    nice, I was trying to find a slick way to handle the go up, hit end, go down, repeat and I really like they way you use the "incre" switching between 1 and -1. I came up with similar but using an up/down flag.

        public string Convert(string s, int numRows) 
        {
            if (numRows < 2) return s;
            
            StringBuilder[] rows = new StringBuilder[numRows];
            for (int i = 0; i < numRows; i++) rows[i] = new StringBuilder();
            
            int r = 0;
            bool down = true;
            foreach (char c in s)
            {
                rows[r].Append(c);
                if (down)
                {
                    if (r == numRows - 1) { r--; down = false; }
                    else r++;
                }
                else
                {
                    if (r == 0) { r++; down = true; }
                    else r--;
                }
            }
            
            StringBuilder result = new StringBuilder();
            foreach (StringBuilder sb in rows) result.Append(sb.ToString());
            return result.ToString();
        }
    

  • 0
    W

    Unbelievableļ¼


  • 0

    ``
    I love this solution, really easy to understand!


Log in to reply
 

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