Good question.  Copying the code without thinking about it is unlikely to deepen your understanding.  That said, if you've attempted the problem then see how the book's answer is better, just describe this in your post.