The best way to prevent leg cramps is to know what causes them. First, don't overwork yourself and keep yourself well hydrated. Avoid extremes in environmental temperatures, especially when going from indoors to outdoors. Consume plenty of electrolytes, such as potassium and calcium. Finally, be sure to stretch properly before exercising. If the cramps continue, see a doctor.