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();
	}
}
posted @ 2026-02-25 23:04  爱新觉罗LQ  阅读(0)  评论(0)    收藏  举报