Skip to content

Commit 987f1d0

Browse files
authored
feat: update solutions for lc No.1356 (#5048)
1 parent 663738f commit 987f1d0

52 files changed

Lines changed: 408 additions & 300 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
- [乘积小于 K 的子数组](/solution/0700-0799/0713.Subarray%20Product%20Less%20Than%20K/README.md) - `双指针`
5353
- [位 1 的个数](/solution/0100-0199/0191.Number%20of%201%20Bits/README.md) - `位运算``lowbit`
5454
- [合并区间](/solution/0000-0099/0056.Merge%20Intervals/README.md) - `区间合并`
55-
<!-- 排序算法、待补充 -->
55+
<!-- 排序算法、待补充 -->
5656

5757
### 2. 数据结构
5858

solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md

Lines changed: 65 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ class Solution {
120120
class Solution {
121121
public:
122122
vector<int> sortByBits(vector<int>& arr) {
123-
for (int& v : arr) {
124-
v += __builtin_popcount(v) * 100000;
123+
for (int& x : arr) {
124+
x += __builtin_popcount(x) * 100000;
125125
}
126-
sort(arr.begin(), arr.end());
127-
for (int& v : arr) {
128-
v %= 100000;
126+
ranges::sort(arr);
127+
for (int& x : arr) {
128+
x %= 100000;
129129
}
130130
return arr;
131131
}
@@ -151,15 +151,28 @@ func sortByBits(arr []int) []int {
151151

152152
```ts
153153
function sortByBits(arr: number[]): number[] {
154-
const countOnes = (n: number) => {
155-
let res = 0;
156-
while (n) {
157-
n &= n - 1;
158-
res++;
159-
}
160-
return res;
161-
};
162-
return arr.sort((a, b) => countOnes(a) - countOnes(b) || a - b);
154+
const n = arr.length;
155+
156+
for (let i = 0; i < n; ++i) {
157+
arr[i] += bitCount(arr[i]) * 100000;
158+
}
159+
160+
arr.sort((a, b) => a - b);
161+
162+
for (let i = 0; i < n; ++i) {
163+
arr[i] %= 100000;
164+
}
165+
166+
return arr;
167+
}
168+
169+
function bitCount(i: number): number {
170+
i = i - ((i >>> 1) & 0x55555555);
171+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
172+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
173+
i = i + (i >>> 8);
174+
i = i + (i >>> 16);
175+
return i & 0x3f;
163176
}
164177
```
165178

@@ -168,13 +181,18 @@ function sortByBits(arr: number[]): number[] {
168181
```rust
169182
impl Solution {
170183
pub fn sort_by_bits(mut arr: Vec<i32>) -> Vec<i32> {
171-
arr.sort_by(|a, b| {
172-
let res = a.count_ones().cmp(&b.count_ones());
173-
if res == std::cmp::Ordering::Equal {
174-
return a.cmp(&b);
175-
}
176-
res
177-
});
184+
let n = arr.len();
185+
186+
for i in 0..n {
187+
arr[i] += arr[i].count_ones() as i32 * 100000;
188+
}
189+
190+
arr.sort();
191+
192+
for i in 0..n {
193+
arr[i] %= 100000;
194+
}
195+
178196
arr
179197
}
180198
}
@@ -183,91 +201,46 @@ impl Solution {
183201
#### C
184202

185203
```c
186-
/**
187-
* Note: The returned array must be malloced, assume caller calls free().
188-
*/
189-
int countOnes(int n) {
190-
int res = 0;
191-
while (n) {
192-
n &= n - 1;
193-
res++;
204+
static int bitCount(int x) {
205+
int cnt = 0;
206+
while (x) {
207+
x &= (x - 1);
208+
++cnt;
194209
}
195-
return res;
210+
return cnt;
196211
}
197212

198-
int cmp(const void* _a, const void* _b) {
199-
int a = *(int*) _a;
200-
int b = *(int*) _b;
201-
int res = countOnes(a) - countOnes(b);
202-
if (res == 0) {
203-
return a - b;
213+
static int cmp(const void* a, const void* b) {
214+
int x = *(const int*) a;
215+
int y = *(const int*) b;
216+
217+
int cx = bitCount(x);
218+
int cy = bitCount(y);
219+
220+
if (cx != cy) {
221+
return cx - cy;
204222
}
205-
return res;
223+
return x - y;
206224
}
207225

226+
/**
227+
* Note: The returned array must be malloced, assume caller calls free().
228+
*/
208229
int* sortByBits(int* arr, int arrSize, int* returnSize) {
209-
qsort(arr, arrSize, sizeof(int), cmp);
210230
*returnSize = arrSize;
211-
return arr;
212-
}
213-
```
214-
215-
<!-- tabs:end -->
216-
217-
<!-- solution:end -->
218-
219-
<!-- solution:start -->
220231

221-
### 方法二
222-
223-
<!-- tabs:start -->
224-
225-
#### Java
226-
227-
```java
228-
class Solution {
229-
public int[] sortByBits(int[] arr) {
230-
int n = arr.length;
231-
Integer[] t = new Integer[n];
232-
for (int i = 0; i < n; ++i) {
233-
t[i] = arr[i];
234-
}
235-
Arrays.sort(t, (a, b) -> {
236-
int x = Integer.bitCount(a), y = Integer.bitCount(b);
237-
return x == y ? a - b : x - y;
238-
});
239-
for (int i = 0; i < n; ++i) {
240-
arr[i] = t[i];
241-
}
242-
return arr;
232+
int* res = (int*) malloc(sizeof(int) * arrSize);
233+
if (!res) {
234+
return NULL;
243235
}
244-
}
245-
```
246-
247-
#### C++
248236

249-
```cpp
250-
class Solution {
251-
public:
252-
vector<int> sortByBits(vector<int>& arr) {
253-
sort(arr.begin(), arr.end(), [&](auto& a, auto& b) -> bool {
254-
int x = __builtin_popcount(a), y = __builtin_popcount(b);
255-
return x < y || (x == y && a < b);
256-
});
257-
return arr;
237+
for (int i = 0; i < arrSize; ++i) {
238+
res[i] = arr[i];
258239
}
259-
};
260-
```
261240

262-
#### Go
241+
qsort(res, arrSize, sizeof(int), cmp);
263242

264-
```go
265-
func sortByBits(arr []int) []int {
266-
sort.Slice(arr, func(i, j int) bool {
267-
a, b := bits.OnesCount(uint(arr[i])), bits.OnesCount(uint(arr[j]))
268-
return a < b || (a == b && arr[i] < arr[j])
269-
})
270-
return arr
243+
return res;
271244
}
272245
```
273246

0 commit comments

Comments
 (0)