Garvit_2013's blog

By Garvit_2013, history, 3 years ago, In English

Can anybody help me with the following problem ? What I am missing . Thanks

Sort Integers by The Number of 1 Bits

Code Link: https://leetcode.com/playground/YGNUSzLx

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +1 Vote: I do not like it

use __builtin_popcount() instead of _builtin_popcount()

»
3 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

The problem said that if two number have same number of 1 bits, sort it in ascending order. So your compareator is not enough This is a way to fix.

static bool cmp(int a, int b){
        return (__builtin_popcount(a) < __builtin_popcount(b) || (__builtin_popcount(a) == __builtin_popcount(b) && a < b));