当前位置:网站首页>【C语言刷LeetCode】2295. 替换数组中的元素(M)
【C语言刷LeetCode】2295. 替换数组中的元素(M)
2022-07-30 02:02:00 【kinbo88】
【
给你一个下标从 0 开始的数组 nums ,它包含 n 个 互不相同 的正整数。请你对这个数组执行 m 个操作,在第 i 个操作中,你需要将数字 operations[i][0] 替换成 operations[i][1] 。
题目保证在第 i 个操作中:
operations[i][0] 在 nums 中存在。
operations[i][1] 在 nums 中不存在。
请你返回执行完所有操作后的数组。
示例 1:
输入:nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
输出:[3,2,7,1]
解释:我们对 nums 执行以下操作:
- 将数字 1 替换为 3 。nums 变为 [3,2,4,6] 。
- 将数字 4 替换为 7 。nums 变为 [3,2,7,6] 。
- 将数字 6 替换为 1 。nums 变为 [3,2,7,1] 。
返回最终数组 [3,2,7,1] 。
示例 2:
输入:nums = [1,2], operations = [[1,3],[2,1],[3,2]]
输出:[2,1]
解释:我们对 nums 执行以下操作:
- 将数字 1 替换为 3 。nums 变为 [3,2] 。
- 将数字 2 替换为 1 。nums 变为 [3,1] 。
- 将数字 3 替换为 2 。nums 变为 [2,1] 。
返回最终数组 [2,1] 。
提示:
n == nums.length
m == operations.length
1 <= n, m <= 105
nums 中所有数字 互不相同 。
operations[i].length == 2
1 <= nums[i], operations[i][0], operations[i][1] <= 106
在执行第 i 个操作时,operations[i][0] 在 nums 中存在。
在执行第 i 个操作时,operations[i][1] 在 nums 中不存在。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/replace-elements-in-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
】
这次hash要替换key,所以需要先删除,再添加的操作
typedef struct {
int key;
int idx;
UT_hash_handle hh;
} UTHASH;
int Cmp (UTHASH *a, UTHASH *b)
{
return a->idx - b->idx;
}
int* arrayChange(int* nums, int numsSize, int** operations, int operationsSize, int* operationsColSize, int* returnSize){
UTHASH *mymap = NULL;
int i;
int *retarr = malloc(sizeof(int) * numsSize);
for (i = 0; i < numsSize; i++) {
UTHASH *new = malloc(sizeof(UTHASH));
new->key = nums[i];
new->idx = i;
HASH_ADD_INT(mymap, key, new); // 每个元素互不相同,所以直接添加
}
for (i = 0; i < operationsSize; i++) {
int key = operations[i][0];
UTHASH *find = NULL;
HASH_FIND_INT(mymap, &key, find); // 按照题意肯定能找到
UTHASH *new = malloc(sizeof(UTHASH));
new->key = operations[i][1];
new->idx = find->idx;
HASH_DEL(mymap, find); // 删掉这个节点
HASH_ADD_INT(mymap, key, new); // 把新节点加进去
}
HASH_SORT(mymap,Cmp); // 排序
UTHASH *cur, *tmp;
int idx = 0;
HASH_ITER(hh, mymap, cur, tmp) {
retarr[idx++] = cur->key;
}
*returnSize = numsSize;
return retarr;
}
/*
UTHASH 找到key后直接把key修改需要先用HASH_DEL删掉,再ADD
*/边栏推荐
猜你喜欢

STM32L4R9ZIY6PTR STM32L4高性能嵌入式—MCU

机械设备制造企业如何借助ERP系统,解决成本核算难题?

JS开发3D建模软件

LeetCode 2342. Digital and equal number of one of the biggest and

解决vscode的Network不显示问题

Google浏览器打开axure产品原型的解决方案

LeetCode 2348. Number of all-zero subarrays

1.2Recyclerview实现Item点击事件

The display and hiding of widgets for flutter learning

JS develops 3D modeling software
随机推荐
The role of interface testing
1.2Recyclerview实现Item点击事件
在服务器上运行node流程
fluttter学习之ButtonStyle 、MaterialStateProperty
Leetcode68. 文本左右对齐
Leetcode70. 爬楼梯
win11 自带远程桌面使用(包含非局域网使用以及win11升级为专业版)
LeetCode 2342. Digital and equal number of one of the biggest and
go 双向流模式
十一、uni-app生成弹窗及换行
The display and hiding of widgets for flutter learning
性能测试理论1 | 性能测试难点问题梳理
自动配置和 thymeleaf模板引擎
「MySQL」- 基础增删改查
解决vscode的Network不显示问题
数据流图、数据字典
How Junior Testers Grow Fast
Type-C charging and OTG chip - LDR6028A
视觉系统设计实例halcon-winform-11.菜单折叠与展示
API interface batch test