Looking to associate strings to ints in a cleaner/more efficient way

前端 未结 8 968
孤街浪徒
孤街浪徒 2021-01-07 07:06

How can I improve this?

The relationship is one to one and continuous on [-1,5] so i was thinking of using enum, but I\'m not sure how to compare a string value to

8条回答
  •  借酒劲吻你
    2021-01-07 07:33

    Honestly, I wouldn't worry about keeping something like this ultra efficient, but there is a change you could make. If the word you pass is the last word you check for then your program ends up performing all of the checks in your function. This shouldn't be a problem in this case, but generally you don't want to flood your program with if statements, especially if you have a lot of cases.

    Use a hashtable and just insert pairs. This way, all of your evaluateWord calls will return in amortized constant time. :)

    Good luck!

提交回复
热议问题