Hello Java.
题目描述:
在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。
请你重新排列这些条形码,使其中两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。
示例:
输入:[1,1,1,2,2,2]
输出:[2,1,2,1,2,1]
输入:[1,1,1,1,2,2,3,3]
输出:[1,3,1,3,2,1,2,1]
提示:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
题目链接:
https://leetcode-cn.com/problems/distant-barcodes/
代码:
class Solution {
public int[] rearrangeBarcodes(int[] barcodes) {
/* 入参判断 */
if(null == barcodes || barcodes.length == 0) {
return barcodes;
}
/* 统计次数 */
int len = barcodes.length;
int[] cnt = new int[10001];
for(int i = 0; i < len; i++) {
cnt[barcodes[i]]+