How can I sort numbers lexicographically?

前端 未结 14 935
半阙折子戏
半阙折子戏 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:38

    If you're going for space-wise efficiency, I'd try just doing the work in the comparison function of the sort

    int compare(int a, int b) {
       // convert a to string
       // convert b to string
       // return -1 if a < b, 0 if they are equal, 1 if a > b
    }
    

    If it's too slow (it's slower than preprocessing, for sure), keep track of the conversions somewhere so that the comparison function doesn't keep having to do them.

提交回复
热议问题