Precondition: You've backed up your iPhone with iCloud before you lost the contacts.
What you need: iCloud
There are two ways for you to recover contacts from your iCloud backup: Merge it to your iPhone or restore it to your iPhone.
Merge iCloud contacts on your iPhone: Go to Settings < iCloud. Log in to your Apple ID and turn off the item of Contacts. Then turn it on and choose Merge on the pop-up message. Then all contacts in the iCloud backup is back to your iPhone.
Restore iCloud backup to your iPhone: In this way, you need to set your iPhone as a new device by going to Settings < General < Reset < Erase All Content and Settings, and then choose Erase iPhone on the pop-up message. When it restarts, you can choose to Restore from iCloud Backup.
Apparently, the first way is highly recommended. If there are really special reasons that you can't do it by the merging way, the second way still can be an option.