12 ms awk based solution


  • 2
    B

    This is all it takes. I tried to do the same thing with sed, but I couldn't remember how to do it with sed.

    awk 'NR == 10' < file.txt
    

    Given that the awk binary is slightly larger than the sed binary on my box I wonder if I'll get better runtime using sed, since some of the runtime will be affected by loading that binary before executing it. But given that those binaries are so small (on my OS X box), I wonder if it will make a difference.

    panzani leetcode$ which sed
    /usr/bin/sed
    panzani leetcode$ du -h /usr/bin/sed
     20K	/usr/bin/sed
    panzani leetcode$ which awk 
    /usr/bin/awk
    panzani leetcode$ du -h /usr/bin/awk 
     52K	/usr/bin/awk

  • 0
    N

    I have the same answer :)


  • 0
    S

    The sed solution:
    sed -n 10p <file.txt


Log in to reply
 

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