This is one of the most frustrating things that can happen when you have invested valuable time writing up your solution. You can minimize the frustration by solving the problems for your pleasure first, then for the community here.
However, to answer your question, it is absolutely ethical to keep typing and post what you have so long as it is your own work. It is also ethical to look for opportunities to post material that the other answer(s) have left out.
Nevertheless, it is never ethical to copy material from the other answer and pass it off as yours. Make sure that whatever you pass off as your own work is actually your own work. One way to ensure this is to not look at the other answers as you are posting.
If you are really driven to be the first to answer, you can post the minimum amount of material needed to answer the question, and then fill in further details with additional edits. This is a common tactic here, but keep in mind that (a) it must be quite frustrating for the OP and (b) someone else could easily post a better answer in the time you take to post edits.
There are times that I look at my answer and see that, after everything I have done, it is exactly the same as an earlier-posted answer and I have nothing more to add. If I did not invest that much time in the answer, I may choose to delete my answer.