improve 127 by converting recursion to loop