ZigZag Conversion Different answers in leetcode compilor and mine and other online ones


  • 1
    P

    #include <bits/stdc++.h>
    using namespace std;

    typedef long long ll;
    #define ull unsigned long long

    #define loop1(i,n) loop2(i,0,n)
    #define loop2(i,a,b) loop3(i,a,b,1)
    #define loop3(i,a,b,c) for(int i = int(a) ; i < int(b) ; i+=int(c))

    class Solution {
    public:
    string convert(string s, int numRows){
    string ans="";
    int l = s.length();
    for(int i=0;i<numRows;i++){
    if(i==0 || i==numRows-1){
    int k = i;
    while(k <= l){
    ans.push_back(s[k]);
    k = k + (2*numRows-2);
    }
    }
    else{
    ans.push_back(s[i]);
    int k = (numsRows+numRows-2);
    do{
    if(k-i <= l) ans.push_back(s[k-i]);
    if(k+i <= l) ans.push_back(s[k+i]);
    k = k + (numsRows+numRows-2);
    }while(k<=l);
    }
    }
    return ans;
    }
    };

    int main(){
    clock_t beg,end;
    beg=clock();
    string str = "PAYPALISHIRING";
    Solution solve;
    cout<<solve.convert(str,13);
    end=clock();
    fprintf(stderr, "%.3f sec\n", double(end-beg)/CLOCKS_PER_SEC);
    return 0;
    }


Log in to reply
 

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