Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 74f5b43

Browse files
feat: add solutions to lc problem: No.1356 (doocs#626)
No.1356.Sort Integers by The Number of 1 Bits
1 parent c65ee2f commit 74f5b43

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,30 @@
7171
<!-- 这里可写当前语言的特殊实现逻辑 -->
7272

7373
```python
74-
74+
class Solution:
75+
def sortByBits(self, arr: List[int]) -> List[int]:
76+
arr.sort(key=lambda x: (bin(x).count("1"), x))
77+
return arr
7578
```
7679

7780
### **Java**
7881

7982
<!-- 这里可写当前语言的特殊实现逻辑 -->
8083

8184
```java
82-
85+
class Solution {
86+
public int[] sortByBits(int[] arr) {
87+
int n = arr.length;
88+
for (int i = 0; i < n; i++) {
89+
arr[i] += Integer.bitCount(arr[i]) * 100000;
90+
}
91+
Arrays.sort(arr);
92+
for (int i = 0; i < n; i++) {
93+
arr[i] %= 100000;
94+
}
95+
return arr;
96+
}
97+
}
8398
```
8499

85100
### **...**

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,28 @@ The sorted array by bits is [0,1,2,4,8,3,5,6,7]
6666
### **Python3**
6767

6868
```python
69-
69+
class Solution:
70+
def sortByBits(self, arr: List[int]) -> List[int]:
71+
arr.sort(key=lambda x: (bin(x).count("1"), x))
72+
return arr
7073
```
7174

7275
### **Java**
7376

7477
```java
75-
78+
class Solution {
79+
public int[] sortByBits(int[] arr) {
80+
int n = arr.length;
81+
for (int i = 0; i < n; i++) {
82+
arr[i] += Integer.bitCount(arr[i]) * 100000;
83+
}
84+
Arrays.sort(arr);
85+
for (int i = 0; i < n; i++) {
86+
arr[i] %= 100000;
87+
}
88+
return arr;
89+
}
90+
}
7691
```
7792

7893
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] sortByBits(int[] arr) {
3+
int n = arr.length;
4+
for (int i = 0; i < n; i++) {
5+
arr[i] += Integer.bitCount(arr[i]) * 100000;
6+
}
7+
Arrays.sort(arr);
8+
for (int i = 0; i < n; i++) {
9+
arr[i] %= 100000;
10+
}
11+
return arr;
12+
}
13+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def sortByBits(self, arr: List[int]) -> List[int]:
3+
arr.sort(key=lambda x: (bin(x).count("1"), x))
4+
return arr

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /