How can I sort numbers lexicographically?

前端 未结 14 919
半阙折子戏
半阙折子戏 2020-12-09 04:56

Here is the scenario.

I am given an array \'A\' of integers. The size of the array is not fixed. The function that I am supposed to write may be called once with an

14条回答
  •  伪装坚强ぢ
    2020-12-09 05:43

    I'd just turn them into strings, and then sort then sort using strcmp, which does lex comparisons.

    Alternatively you can write a "lexcmp" function that compares two numbers using % 10 and /10 but that's basically the same thing as calling atoi many times, so not a good idea.

提交回复
热议问题