add a O(n) space solution, need to be improved