AC solution with question


  • 0
    K

    This is the AC solution using a big array to store all elements from stack exchange

    awk '
    { 
        for (i=1; i<=NF; i++)  {
            a[NR,i] = $i
        }
    }
    NF>p { p = NF }
    END {    
        for(j=1; j<=p; j++) {
            str=a[1,j]
            for(i=2; i<=NR; i++){
                str=str" "a[i,j];
            }
            print str
        }
    }' file.txt
    

    Then I tried to improve it somehow by using "attaching" idea and got the following code which "should" be better I think:

     col=`head -1 "file.txt"|awk '{print NF'}`
     echo $col
        
        
     for((i=1;i<=$col;i++))
     do
         awk '{printf "%s %s",$'"$i"'," "}END{print ""}' file.txt
     done
    

    then I got Memory exceed result. Would someone please enlighten me on this?


  • 0
    J

    I'm also curious about this. I came up with a similar solution (just using cut and tr inside the loop instead of awk) and got memory exceeded as well. I'm not sure how this could use more peak memory than storing everything in awk until the end. Could they be summing the memory use of all the processes regardless of whether they are concurrently active?


  • 0
    Z

    same problems here.


Log in to reply
 

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