This paper proposes an optimal galloping trajectory, which costs low energy and guarantees the stability of the quadruped robot. In the realization of fast galloping, the trajectory design is important. For a galloping trajectory, we propose an elliptic leg trajectory, which provides simplified locomotion to complex galloping motions of animals. However, the elliptic trajectory, as an imitation of animal galloping motion, does not guarantee stability and minimal energy consumption. We propose optimization based on energy and stability using a genetic algorithm, which provides a robust and globally optimized solution to this multi-body, highly nonlinear dynamic system. To evaluate and verify the effectiveness of the proposed trajectory, a series of computer simulations were carried out.