Simple Solution for Zig Zag


  • 0
    W

    class Solution {
    public String convert(String s, int numRows) {
    if(numRows==1) return s;
    int jumps=numRows*2-2;
    string ans="";
    for(int j=0;j<numRows;j++){
    int jumps2=(numRows-j)*2-2;
    int i=j;
    if(j=0||j=numRows){
    while(i<s.size){
    ans+=s[i];
    i+=jumps;
    }
    }else{
    while(i<s.size){
    ans+=s[i];
    ans+=s[i+jumps2];
    i+=jumps;
    }
    }
    }
    return ans;
    }
    }


Log in to reply
 

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