US stocks end with gains, led by tech shares