当前位置:网站首页>【C语言刷LeetCode】1054. 距离相等的条形码(M)
【C语言刷LeetCode】1054. 距离相等的条形码(M)
2022-07-29 05:53:00 【kinbo88】
【
在一个仓库里,有一排条形码,其中第 i 个条形码为 barcodes[i]。
请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。 你可以返回任何满足该要求的答案,此题保证存在答案。
示例 1:
输入:barcodes = [1,1,1,2,2,2]
输出:[2,1,2,1,2,1]
示例 2:
输入:barcodes = [1,1,1,1,2,2,3,3]
输出:[1,3,1,3,2,1,2,1]
提示:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/distant-barcodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
】
1. 见到数组先考虑排序,排序方式时按照次数排序,让人想到哈希
2. 不重复,那么先放偶数,再放奇数位置
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
typedef struct {
int key;
int val;
UT_hash_handle hh;
} UTHASH;
int Cmp(UTHASH *a, UTHASH *b) {
return b->val - a->val;
}
int* rearrangeBarcodes(int* barcodes, int barcodesSize, int* returnSize){
UTHASH *mymap = NULL;
int i;
int idx = 0;
int *retarr = malloc(sizeof(int) * barcodesSize);
for (i = 0; i < barcodesSize; i++) {
UTHASH *find = NULL;
HASH_FIND_INT(mymap, &barcodes[i], find);
if (find != NULL) {
find->val += 1;
} else {
UTHASH *new = malloc(sizeof(UTHASH));
new->key = barcodes[i];
new->val = 1;
HASH_ADD_INT(mymap, key, new);
}
}
HASH_SORT(mymap, Cmp);
UTHASH *cur, *tmp;
HASH_ITER(hh, mymap, cur, tmp) { // 遍历HASH表
while (cur->val > 0) { // 当前元素没有使用完时
retarr[idx] = cur->key; // 把元素放入retarr
idx = idx + 2; // idx 跳着放,即先填偶数位
if (idx >= barcodesSize) { // 如果idx 到达retarr尾部
idx = 1; // 开始填奇数位
}
cur->val--; // 每次放一个元素,元素的个数减一
}
}
*returnSize = barcodesSize;
return retarr;
}
/*
1. 见到数组先考虑排序,排序方式时按照次数排序,让人想到哈希
2. 不重复,那么先放偶数,再放奇数位置
*/边栏推荐
- Excerpts from good essays
- 【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
- 矩阵分解与梯度下降
- MySQL: what happens in the bufferpool when you crud? Ten pictures can make it clear
- 王树尧老师运筹学课程笔记 00 写在前面
- The core of openresty and cosocket
- 【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
- Unity免费元素特效推荐
- 吴恩达老师机器学习课程笔记 02 单变量线性回归
- IDEA找不到Database解决方法
猜你喜欢

Recurrent neural network RNN

Teacher Wu Enda's machine learning course notes 02 univariate linear regression

Idea cannot find a database solution

Unity free element special effect recommendation

LDAP brief description and unified authentication description

How to write controller layer code gracefully?

10道面试常问JVM题

Ali gave several SQL messages and asked how many tree search operations need to be performed?

剑指 Offer II 115:重建序列

Junda technology | applicable to "riyueyuan" brand ups wechat cloud monitoring card
随机推荐
Simulation volume leetcode [normal] 061. rotating linked list
Basic knowledge of MySQL (high frequency interview questions)
吴恩达老师机器学习课程笔记 02 单变量线性回归
王树尧老师运筹学课程笔记 00 写在前面
leetcode-592:分数加减运算
DBAsql面试题
基于C语言设计的学籍管理系统
王树尧老师运筹学课程笔记 08 线性规划与单纯形法(单纯形法)
模拟卷Leetcode【普通】081. 搜索旋转排序数组 II
SS command details
Jetpack Compose 中的键盘处理
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
Federal learning backdoor attack summary (2019-2022)
IO stream - file - properties
Database multi table query joint query add delete modify query
Unity free element special effect recommendation
Salesforce中过滤器Filter使用的相对日期
Ping principle
【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
【备忘】关于ssh为什么会失败的原因总结?下次记得来找。