力扣 1356.根據數字二進制下 1 的數目排序 力扣(點擊查看題目)
題目描述
給你一個整數數組 arr 。請你將數組中的元素按照其二進制表示中數字 1 的數目升序排序。
如果存在多個數字二進制中 1 的數目相同,則必須將它們按照數值大小升序排列。
請你返回排序後的數組。
示例 1:
<code>輸入:arr = [0,1,2,3,4,5,6,7,8] 輸出:[0,1,2,4,8,3,5,6,7] 解釋:[0] 是唯一一個有 0 個 1 的數。 [1,2,4,8] 都有 1 個 1 。 [3,5,6] 有 2 個 1 。 [7] 有 3 個 1 。 按照 1 的個數排序得到的結果數組為 [0,1,2,4,8,3,5,6,7]/<code>
示例 2:
<code>輸入:arr = [1024,512,256,128,64,32,16,8,4,2,1] 輸出:[1,2,4,8,16,32,64,128,256,512,1024] 解釋:數組中所有整數二進制下都只有 1 個 1 ,所以你需要按照數值大小將它們排序。/<code>
示例 3:
<code>輸入:arr = [10000,10000] 輸出:[10000,10000]/<code>
示例 4:
<code>輸入:arr = [2,3,5,7,11,13,17,19] 輸出:[2,3,5,17,7,11,13,19]/<code>
示例 5:
<code>輸入:arr = [10,100,1000,10000] 輸出:[10,100,10000,1000]/<code>
提示:
- 1 <= arr.length <= 500
- 0 <= arr[i] <= 10^4
解決方案
前言
題目本身很簡單,只要調用系統自帶的排序函數,然後自己改寫一下排序規則即可,所以這裡主要講講如何計算數字二進制下 1 的個數。
方法一:暴力
對每個十進制的數轉二進制的時候統計一下 1 的個數即可。
C++
<code>class Solution { public: int get(int x){ int res = 0; while (x) { res += (x % 2); x /= 2; } return res; } vector sortByBits(vector& arr) { vector bit(10001, 0); for (auto x: arr) { bit[x] = get(x); } sort(arr.begin(),arr.end(),[&](int x,int y){ if (bit[x] < bit[y]) { return true; } if (bit[x] > bit[y]) { return false; } return x < y; }); return arr; } };/<code>
Java
<code>class Solution { public int[] sortByBits(int[] arr) { int[] bit = new int[10001]; List list = new ArrayList(); for (int x : arr) { list.add(x); bit[x] = get(x); } Collections.sort(list, new Comparator() { public int compare(Integer x, Integer y) { if (bit[x] != bit[y]) { return bit[x] - bit[y]; } else { return x - y; } } }); for (int i = 0; i < arr.length; ++i) { arr[i] = list.get(i); } return arr; } public int get(int x) { int res = 0; while (x != 0) { res += x % 2; x /= 2; } return res; } }/<code>
Golang
<code>func onesCount(x int) (c int) { for ; x > 0; x /= 2 { c += x % 2 } return } func sortByBits(a []int) []int { sort.Slice(a, func(i, j int) bool { x, y := a[i], a[j] cx, cy := onesCount(x), onesCount(y) return cx < cy || cx == cy && x < y }) return a }/<code>
C
<code>int* bit; int get(int x) { int res = 0; while (x) { res += (x % 2); x /= 2; } return res; } int cmp(void* _x, void* _y) { int x = *(int*)_x, y = *(int*)_y; return bit[x] == bit[y] ? x - y : bit[x] - bit[y]; } int* sortByBits(int* arr, int arrSize, int* returnSize) { bit = malloc(sizeof(int) * 10001); memset(bit, 0, sizeof(int) * 10001); for (int i = 0; i < arrSize; ++i) { bit[arr[i]] = get(arr[i]); } qsort(arr, arrSize, sizeof(int), cmp); free(bit); *returnSize = arrSize; return arr; }/<code>
複雜度分析
- 時間複雜度:O(n log n),其中 n 為整數數組 arr 的長度。
- 空間複雜度:O(n),其中 n 為整數數組 arr 的長度。
方法二:遞推預處理
我們定義 bit[i] 為數字 i 二進制表示下數字 1 的個數,則可以列出遞推式:
所以我們線性預處理 bit 數組然後去排序即可。
C++
<code>class Solution { public: vector sortByBits(vector& arr) { vector bit(10001, 0); for (int i = 1;i <= 10000; ++i) { bit[i] = bit[i>>1] + (i & 1); } sort(arr.begin(),arr.end(),[&](int x,int y){ if (bit[x] < bit[y]) { return true; } if (bit[x] > bit[y]) { return false; } return x < y; }); return arr; } };/<code>
Java
<code>class Solution { public int[] sortByBits(int[] arr) { List list = new ArrayList(); for (int x : arr) { list.add(x); } int[] bit = new int[10001]; for (int i = 1; i <= 10000; ++i) { bit[i] = bit[i >> 1] + (i & 1); } Collections.sort(list, new Comparator() { public int compare(Integer x, Integer y) { if (bit[x] != bit[y]) { return bit[x] - bit[y]; } else { return x - y; } } }); for (int i = 0; i < arr.length; ++i) { arr[i] = list.get(i); } return arr; } }/<code>
Golang
<code>var bit = [1e4 + 1]int{} func init() { for i := 1; i <= 1e4; i++ { bit[i] = bit[i>>1] + i&1 } } func sortByBits(a []int) []int { sort.Slice(a, func(i, j int) bool { x, y := a[i], a[j] cx, cy := bit[x], bit[y] return cx < cy || cx == cy && x < y }) return a }/<code>
C
<code>int* bit; int cmp(void* _x, void* _y) { int x = *(int*)_x, y = *(int*)_y; return bit[x] == bit[y] ? x - y : bit[x] - bit[y]; } int* sortByBits(int* arr, int arrSize, int* returnSize) { bit = malloc(sizeof(int) * 10001); memset(bit, 0, sizeof(int) * 10001); for (int i = 1; i <= 10000; ++i) { bit[i] = bit[i >> 1] + (i & 1); } qsort(arr, arrSize, sizeof(int), cmp); free(bit); *returnSize = arrSize; return arr; }/<code>
複雜度分析
- 時間複雜度:O(n log n),其中 n 為整數數組 arr 的長度。
- 空間複雜度:O(n),其中 n 為整數數組 arr 的長度。
BY /
本文作者:力扣
聲明:本文歸“力扣”版權所有,如需轉載請聯繫。