From http://oj.leetcode.com/discuss/135/answer-not-getting-accepted-because-exceeding-the-time-limit