当前位置:网站首页>【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. 不重复,那么先放偶数,再放奇数位置
*/
边栏推荐
- 【冷冻电镜|论文阅读】子断层平均 M 软件解读:Multi-particle cryo-EM refinement with M
- 吴恩达老师机器学习课程笔记 05 Octave教程
- 【冷冻电镜】RELION4.0之subtomogram对位功能源码分析(自用)
- 猜数字//第一次使用生成随机数
- The difference between pairs and ipairs
- 【技能积累】写邮件时的常用表达
- Teacher wangshuyao's operations research course notes 07 linear programming and simplex method (standard form, base, base solution, base feasible solution, feasible base)
- Teacher Wu Enda's machine learning course notes 04 multiple linear regression
- IO流 - File - properties
- MySql基础知识(高频面试题)
猜你喜欢
游戏资产的革命
【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读
矩阵分解与梯度下降
基于C语言设计的学籍管理系统
How to write controller layer code gracefully?
阿里一面,给了几条SQL,问需要执行几次树搜索操作?
SSH免密登录-两台虚拟机建立免密通道 双向信任
Software definition boundary SDP
Federal learning backdoor attack summary (2019-2022)
随机推荐
【flask入门系列】Flask-SQLAlchemy的安装与配置
Teacher Wu Enda's machine learning course notes 04 multiple linear regression
How to write controller layer code gracefully?
leetcode-592:分数加减运算
IO流 - File - properties
【冷冻电镜入门】加州理工公开课课程笔记 Part 3: Image Formation
leetcode-1331:数组序号转换
Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
mysql查询区分大小写
Teacher Wang Shuyao's notes on operations research 09 linear programming and simplex method (Application of simplex table)
Database multi table query joint query add delete modify query
量子机器学习中的安全性问题
Simulation volume leetcode [ordinary] 172. Zero after factorial
Jetpack Compose 中的键盘处理
10道面试常问JVM题
线程 - 线程安全 - 线程优化
剑指 Offer II 115:重建序列
Teacher wangshuyao's notes on operations research 02 fundamentals of advanced mathematics
Teacher Wu Enda's machine learning course notes 02 univariate linear regression