If it isn't already represented in part or in full by another answer, and if you weren't pushed most of the way to your solution by an existing answer, then I'd say it's okay to answer your own question.
This is particularly true if you did research outside of the site after posting your question, and came up with a solution superior to the ones already offered.
If an existing answer on your question gave you most of the push toward your solution, I'd rather reward that answer and either edit into the answer the missing part(s) that made your final solution better, or ask the answerer to do so (though this may be difficult if it is additional code or otherwise not convenient to post in a comment).
If, on the other hand, you found your solution in another answer on the same site, I'd be just as tempted to close the question as a duplicate of the other question that contained your solution, if it makes sense (it might not always). Moderators and other answerers are not always aware that your question has a duplicate out there, but IMHO the fewer questions with the exact same answer, the better.