Efficient algorithm to find first available name

前端 未结 7 1419
野性不改
野性不改 2021-01-19 03:26

I have an array containing names of items. I want to give the user the option to create items without specifying their name, so my program will have to supply a unique defau

7条回答
  •  深忆病人
    2021-01-19 03:55

    Insert all of the existing names into a hash table. Repeat your loop, but make isAvailable check the hash table. Assuming a decent hash, it's O(nh) where h is the cost of evaluating the hash.

提交回复
热议问题