1356. Sort Integers by The Number of 1 Bits Easy

@problem@discussion
#Array#Bit Manipulation#Sorting#Counting



1/**
2 * [1356] Sort Integers by The Number of 1 Bits
3 *
4 * You are given an integer array arr. Sort the integers in the array in ascending order by the number of 1's in their binary representation and in case of two or more integers have the same number of 1's you have to sort them in ascending order.
5 * Return the array after sorting it.
6 *  
7 * Example 1:
8 * 
9 * Input: arr = [0,1,2,3,4,5,6,7,8]
10 * Output: [0,1,2,4,8,3,5,6,7]
11 * Explantion: [0] is the only integer with 0 bits.
12 * [1,2,4,8] all have 1 bit.
13 * [3,5,6] have 2 bits.
14 * [7] has 3 bits.
15 * The sorted array by bits is [0,1,2,4,8,3,5,6,7]
16 * 
17 * Example 2:
18 * 
19 * Input: arr = [1024,512,256,128,64,32,16,8,4,2,1]
20 * Output: [1,2,4,8,16,32,64,128,256,512,1024]
21 * Explantion: All integers have 1 bit in the binary representation, you should just sort them in ascending order.
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	1 <= arr.length <= 500
27 * 	0 <= arr[i] <= 10^4
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/
33// discuss: https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn sort_by_bits(arr: Vec<i32>) -> Vec<i32> {
39        vec![]
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_1356() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.