classSolution { public: /** * @param nums: the original integer array * @return: the integer array after sorting */ staticstringDec2Bin(int tar){ string res; while (tar != 1 && tar != 0) { if(tar%2 == 1) res.insert(0,1,'1'); else res.insert(0,1,'0');
tar /= 2; } if (tar == 1) res.insert(0,1,'1');
return res; } //decimal to binary
staticintBin2Dec(string tar){ int res=0; int n = tar.length();
for (int i = n-1; i >=0; i--) if (tar[i] == '1') res += pow(2,n-i-1);
return res; }// binary to decimal
staticboolcmp(constint & a, constint & b){ string binA = Dec2Bin(a), binB = Dec2Bin(b); int na = 0, nb = 0; for (int i = 0; i < binA.length(); i++) if (binA[i] == '1') na++;
for (int i = 0; i < binB.length(); i++) if (binB[i] == '1') nb++;
return na > nb ? false : na < nb ? true : a < b;
} vector<int> AscendingBinarySorting(vector<int> &nums) { // Write your code here sort(nums.begin(), nums.end(), cmp); return nums; } };
-------------end of file
thanks for reading-------------