|
| 1 | +package com.leetcode_cn.easy; |
| 2 | +/*******************根据数字二进制下 1 的数目排序*************/ |
| 3 | + |
| 4 | +import java.util.Arrays; |
| 5 | + |
| 6 | +/** |
| 7 | + * 给你一个整数数组 arr 。请你将数组中的元素按照其二进制表示中数字 1 的数目升序排序。 |
| 8 | + * |
| 9 | + * 如果存在多个数字二进制中 1 的数目相同,则必须将它们按照数值大小升序排列。 |
| 10 | + * |
| 11 | + * 请你返回排序后的数组。 |
| 12 | + * |
| 13 | + * |
| 14 | + * |
| 15 | + * 示例 1: |
| 16 | + * |
| 17 | + * 输入:arr = [0,1,2,3,4,5,6,7,8] |
| 18 | + * 输出:[0,1,2,4,8,3,5,6,7] |
| 19 | + * 解释:[0] 是唯一一个有 0 个 1 的数。 |
| 20 | + * [1,2,4,8] 都有 1 个 1 。 |
| 21 | + * [3,5,6] 有 2 个 1 。 |
| 22 | + * [7] 有 3 个 1 。 |
| 23 | + * 按照 1 的个数排序得到的结果数组为 [0,1,2,4,8,3,5,6,7] |
| 24 | + * 示例 2: |
| 25 | + * |
| 26 | + * 输入:arr = [1024,512,256,128,64,32,16,8,4,2,1] |
| 27 | + * 输出:[1,2,4,8,16,32,64,128,256,512,1024] |
| 28 | + * 解释:数组中所有整数二进制下都只有 1 个 1 ,所以你需要按照数值大小将它们排序。 |
| 29 | + * 示例 3: |
| 30 | + * |
| 31 | + * 输入:arr = [10000,10000] |
| 32 | + * 输出:[10000,10000] |
| 33 | + * 示例 4: |
| 34 | + * |
| 35 | + * 输入:arr = [2,3,5,7,11,13,17,19] |
| 36 | + * 输出:[2,3,5,17,7,11,13,19] |
| 37 | + * 示例 5: |
| 38 | + * |
| 39 | + * 输入:arr = [10,100,1000,10000] |
| 40 | + * 输出:[10,100,10000,1000] |
| 41 | + * |
| 42 | + * |
| 43 | + * 提示: |
| 44 | + * |
| 45 | + * 1 <= arr.length <= 500 |
| 46 | + * 0 <= arr[i] <= 10^4 |
| 47 | + * |
| 48 | + */ |
| 49 | +public class SortIntegersByTheNumberOfOneBits { |
| 50 | + |
| 51 | + public int[] sortByBits(int[] arr) { |
| 52 | + |
| 53 | + // int[] 转 Integer[] |
| 54 | + Integer[] temp = Arrays.stream(arr).boxed().toArray(Integer[] :: new); |
| 55 | + // 排序 ‘1’的个数一样就按照大小 否则按照‘1’的数量 |
| 56 | + Arrays.sort(temp, (o1, o2) -> { |
| 57 | + int o1Count = Integer.bitCount(o1); |
| 58 | + int o2Count = Integer.bitCount(o2); |
| 59 | + return o1Count == o2Count ? o1 - o2 : o1Count - o2Count; |
| 60 | + }); |
| 61 | + // Integer[] 转 int[] |
| 62 | + return Arrays.stream(temp).mapToInt(Integer::intValue).toArray(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 循环并使用Integer.bitCount计算数字中1的个数,乘以10000000(题目中不会大于10^4)然后加上原数字, |
| 67 | + * |
| 68 | + * 放入数组中,并对数组进行排序,最后 % 10000000获取原来的数字 |
| 69 | + * |
| 70 | + * @param arr |
| 71 | + * @return |
| 72 | + */ |
| 73 | + public int[] sortByBits1(int[] arr) { |
| 74 | + |
| 75 | + for (int i = 0; i < arr.length; i++) { |
| 76 | + arr[i] = Integer.bitCount(arr[i]) * 10000000 + arr[i]; |
| 77 | + } |
| 78 | + // 升序排序 |
| 79 | + Arrays.sort(arr); |
| 80 | + for (int i = 0; i < arr.length; i++) { |
| 81 | + arr[i] = arr[i] % 10000000; |
| 82 | + } |
| 83 | + return arr; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 二进制计数法 |
| 88 | + * @param n |
| 89 | + * @return |
| 90 | + */ |
| 91 | + int count_binary_one(int n) |
| 92 | + { |
| 93 | + int count = 0; |
| 94 | + while(n > 0) |
| 95 | + { |
| 96 | + n &= (n - 1); // 计数完后依次从右往左抹掉一个 '1' 变成 '0' |
| 97 | + ++count; |
| 98 | + } |
| 99 | + return count; |
| 100 | + } |
| 101 | + |
| 102 | + public static void main(String[] args) { |
| 103 | + |
| 104 | + |
| 105 | + } |
| 106 | +} |
0 commit comments