当前位置:网站首页>6092. 替换数组中的元素
6092. 替换数组中的元素
2022-07-02 12:10:00 【紫菜(Nori)】
class Solution {
public:
vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
// 保存nums中所有元素的下标信息
// key: 值; value: 下标
map<int, int> map;
for(int i = 0; i < nums.size(); i++){
// 将值和对应的下标插入
map.emplace(nums[i], i);
}
// 遍历所有的操作
for(int i = 0; i < operations.size(); i++){
auto it = map.find(operations[i][0]);
// 由于一定存在,所以这里不用判断
// if(it == end(map)){
// continue;
// }
// 替换对应位置的值
int val = operations[i][1];
nums[it->second] = val;
// 更新或则插入新元素和对应的下标
map[val] = it->second;
}
return nums;
}
};
边栏推荐
- [leetcode] 695 - maximum area of the island
- 05_ queue
- MD5 encryption
- 18_ Redis_ Redis master-slave replication & cluster building
- Wechat Alipay account system and payment interface business process
- MySQL calculate n-day retention rate
- Force deduction solution summarizes the lucky numbers in 1380 matrix
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- 02_线性表_顺序表
- LeetCode刷题——递增的三元子序列#334#Medium
猜你喜欢
How to find a sense of career direction
10_ Redis_ geospatial_ command
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
Bing. Com website
02_ Linear table_ Sequence table
彻底弄懂浏览器强缓存和协商缓存
微信支付宝账户体系和支付接口业务流程
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
yolo格式数据集处理(xml转txt)
Loss function and positive and negative sample allocation: Yolo series
随机推荐
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
Build your own semantic segmentation platform deeplabv3+
LeetCode刷题——验证二叉树的前序序列化#331#Medium
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
LeetCode刷题——去除重复字母#316#Medium
LeetCode刷题——两整数之和#371#Medium
党史纪实主题公益数字文创产品正式上线
03_ Linear table_ Linked list
LeetCode刷题——递增的三元子序列#334#Medium
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
QML pop-up frame, customizable
Engineer evaluation | rk3568 development board hands-on test
高考录取分数线爬取
02. After containerization, you must face golang
提前批院校名称
密码学基础知识
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Map introduction