solution only need constant memory space