Блог пользователя Garvit_2013

Автор Garvit_2013, история, 2 года назад, По-английски

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

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

use __builtin_popcount() instead of _builtin_popcount()

»
2 года назад, # |
Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

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));