int[] 与 integer[] 相互转换
int[] 与 integer[] 相互转换
class Solution {
public int[] sortByBits(int[] arr) {
Integer[] integerArray = Arrays.stream(arr)
.boxed()
.toArray(Integer[]::new);
Arrays.sort(integerArray, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int count1 = Integer.bitCount(o1);
int count2 = Integer.bitCount(o2);
if (count1 != count2) {
return count1 - count2;
}
return o1.compareTo(o2);
}
});
return Arrays.stream(integerArray).mapToInt(Integer::intValue).toArray();
}
}

浙公网安备 33010602011771号