If you plan to move out before the end of a lease term, notify your landlord as soon as possible. The more notice you give, the better your chances are that your landlord will find a new tenant (and not end up using your whole security deposit or sue you in small claims court for rent for the remaining lease term).
Try to work out a written agreement with your landlord that cancels the lease and allows you to leave early. You may need to sweeten the deal, by offering to pay a little extra, in exchange for the landlord letting you out of the lease early. For example, if you want to leave three months’ early, you might offer to pay half a month’s extra rent and be extra accommodating when the landlord wants to show the unit to prospective tenants. Use the Sample Agreement Regarding Cancellation of Lease for this purpose, and edit according to your particular situation. Be sure to attach a copy of your current lease, and keep a copy of your agreement with your landlord.
For more advice on the subject, see Breaking a Lease and Leaving Early.