两数组的交 II-LintCode
来源:互联网 发布:java 高性能 高并发 编辑:程序博客网 时间:2024/05/21 17:26
计算两个数组的交
注意事项:
每个元素出现次数得和在数组里一样
答案可以以任意顺序给出
样例:
nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].
挑战 :
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1’s size is small compared to num2’s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
#ifndef C548_H#define C548_H#include<vector>#include<iostream>#include<map>using namespace std;class Solution {public: /* * @param nums1: an integer array * @param nums2: an integer array * @return: an integer array */ vector<int> intersection(vector<int> nums1, vector<int> nums2) { // write your code here vector<int> res; if (nums1.empty() || nums2.empty()) return res; map<int,int> m1,m2; for (auto c : nums1) m1[c]++; for (auto t : nums2) m2[t]++; for (auto it = m2.begin(); it != m2.end(); ++it) { if (m1.find(it->first) != m1.end()) { int num = minVal(it->second, m1[it->first]); while (num>0) { res.push_back(it->first); num--; } } } return res; } int minVal(int a, int b) { return a < b ? a : b; }}; #endif
阅读全文
0 0
- LintCode:两数组的交 II
- LintCode 548 两数组的交 II
- LintCode【两数组的交II】
- 【lintcode笔记】两数组的交 II
- LintCode-两数组的交 II
- lintcode 两数组的交II
- 两数组的交 II -LintCode
- LintCode 两数组的交 II
- 两数组的交 II-LintCode
- lintcode 547两数组的交 lintcode 548两数组的交II
- LintCode做题记录-两数组的交 II
- lintcode 刷题-两数组的交 II Python
- Lintcode——两数组的交 II
- LintCode-[容易] 548. 两数组的交II
- LintCode:两数组的交
- LintCode:两数组的交
- 两数组的交-LintCode
- lintcode 两数组的交
- leetcode题解-668. Kth Smallest Number in Multiplication Table
- G
- "原来我也可以写Android小游戏"
- Device Tree编译工具dtc
- Java之阿里巴巴开发规约插件(eclipse)-yellowcong
- 两数组的交 II-LintCode
- SSM知识补充--我是一个实现者,不是一个技术控
- 系统架构设计师考试经验
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- 浅谈二叉搜索树
- WebView内存泄漏--解决方法
- mysql 免安装版本配置方法
- ES6-数值的扩展-二进制和八进制表示法
- STM32控制APM飞控(四)MAVLINK协议深入理解之数据结构