【基本数据结构】5.8任务
基本数据结构
Leetcode.1两数之和
Leetcode.187重复的DNA序列
Leetcode.706设计哈希映射
Leetcode.652寻找重复的子树
Leetcode.560和为 K 的子数组
Leetcode.547省份数量
Leetcode.684冗余连接
Leetcode.692前K个高频单词
Leetcode.295数据流的中位数
Leetcode.352将数据流变为多个不相交区间
一、Leetcode.1两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int,int>hash;for(int i=0;i<nums.size();i++){if(hash.count(target-nums[i]))return {hash[target-nums[i]],i};hash[nums[i]]=i;}return {-1,-1};}
二、Leetcode.187重复的DNA序列
DNA序列 由一系列核苷酸组成,缩写为 ‘A’, ‘C’, ‘G’ 和 ‘T’.。
例如,“ACGAATTCCG” 是一个 DNA序列 。
在研究 DNA 时,识别 DNA 中的重复序列非常有用。
给定一个表示 DNA序列 的字符串 s ,返回所有在 DNA 分子中出现不止一次的 长度为 10 的序列(子字符串)。你可以按 任意顺序 返回答案。
vector<string> findRepeatedDnaSequences(string s) {unordered_map<string,int>hash;vector<string>ans;for(int i=0;i+9<=s.size()-1;i++){string str=s.substr(i,10);hash[str]++;if(hash[str]==2)ans.push_back(str);}return ans;}
三、Leetcode.706设计哈希映射
不使用任何内建的哈希表库设计一个哈希映射(HashMap)。
int N=20011;vector<list<pair<int,int>>>h;MyHashMap() {h=vector<list<pair<int,int>>>(N);}list<pair<int,int>>::iterator find(int key){int t=key%N;for(auto it=h[t].begin();it!=h[t].end();it++){if(it->first==key)return it;}return h[t].end();}void put(int key, int value) {auto it=find(key);int t=key%N;if(it==h[t].end())h[t].push_back({key,value});else it->second=value;}int get(int key) {auto it=find(key);int t=key%N;if(it==h[t].end())return -1;else return it->second;}void remove(int key) {auto it=find(key);int t=key%N;if(it==h[t].end())return;else h[t].erase(it);}
四、Leetcode.652寻找重复的子树
对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可。
unordered_map<string,int>h;vector<TreeNode*>ans;//1,2, , , string dfs(TreeNode*root){if(root==NULL)return "";string str="";str+=to_string(root->val)+",";str+=dfs(root->right)+",";str+=dfs(root->left);h[str]++;if(h[str]==2)ans.push_back(root);return str;}vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {dfs(root);return ans;}
五、Leetcode.560和为 K 的子数组
给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的子数组的个数 。
int subarraySum(vector<int>& nums, int k) {unordered_map<int,int>h;int sum=0;h[0]=1;int ans=0;for(int i=0;i<nums.size();i++){sum+=nums[i];ans+=h[sum-k];h[sum]++;}return ans;}
六、Leetcode.547省份数量
有 n 个城市,其中一些彼此相连,另一些没有相连。如果城市 a 与城市 b 直接相连,且城市 b 与城市 c 直接相连,那么城市 a 与城市 c 间接相连。
省份 是一组直接或间接相连的城市,组内不含其他没有相连的城市。
vector<int>p;int find(int x){if(x!=p[x])p[x]=find(p[x]);return p[x];}int findCircleNum(vector<vector<int>>& isConnected) {int n=isConnected.size();p=vector<int>(n);for(int i=0;i<n;i++)p[i]=i;int ans=n;for(int i=0;i<n;i++){for(int j=0;j<i;j++){if(isConnected[i][j]==0)continue;if(find(i)!=find(j)){p[find(i)]=find(j);ans--;}}}return ans;}
七、Leetcode.684冗余连接
请找出一条可以删去的边,删除后可使得剩余部分是一个有着 n 个节点的树。如果有多个答案,则返回数组 edges 中最后出现的边。
vector<int>p;int find(int x){if(x!=p[x])p[x]=find(p[x]);return p[x];}vector<int> findRedundantConnection(vector<vector<int>>& edges) {int n=edges.size();p=vector<int>(n+1);for(int i=i;i<=n;i++)p[i]=i;for(auto e:edges){int x=e[0];int y=e[1];if(find(x)==find(y))return {x,y};p[find(x)]=find(y);}return {-1,-1};}
八、Leetcode.692前K个高频单词
给定一个单词列表 words 和一个整数 k ,返回前 k 个出现次数最多的单词。
返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率, 按字典顺序 排序。
unordered_map<string,int>hash;typedef pair<int,string> PIS;priority_queue<PIS>heap;vector<string> topKFrequent(vector<string>& words, int k) {for(int i=0;i<words.size();i++){hash[words[i]]++;}for(auto item:hash){PIS t(-item.second,item.first);heap.push(t);//cout<<item.second<<item.first<<endl;if(heap.size()>k){//cout<<heap.top().second<<endl;heap.pop();}}vector<string>ans(k);for(int i=k-1;i>=0;i--){ans[i]=heap.top().second;//cout<<ans[k]heap.pop();}return ans;}
九、Leetcode.295数据流的中位数
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
priority_queue<int>down;priority_queue<int,vector<int>,greater<int>>up;MedianFinder() {}void addNum(int num) {if(down.empty()||num>=down.top())up.push(num);else{down.push(num);up.push(down.top());down.pop();}if(up.size()>down.size()+1){down.push(up.top());up.pop();}}double findMedian() {if(down.size()+up.size()&1==1)return up.top();else return (down.top()+up.top())/2.0;}
十、Leetcode.352将数据流变为多个不相交区间
给你一个由非负整数 a1, a2, …, an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。
map<int, int> interval;SummaryRanges() {}void addNum(int val) {auto up = interval.upper_bound(val);auto down = (up == interval.begin() ? interval.end() : prev(up));if (down != interval.end() && (val >= down->first && val <= down->second)) {} else if ((up != interval.end() && up->first - 1 == val) && (down != interval.end() && down->second + 1 == val)) {int l = down->first, r = up->second;interval.erase(down->first);interval.erase(up->first);interval.emplace(l, r);} else if (down != interval.end() && down->second + 1 == val) {int l = down->first, r = val;interval.erase(l);interval.emplace(l, r);} else if (up != interval.end() && up->first - 1 == val) {int l = val, r = up->second;interval.erase(up->first);interval.emplace(l, r);} else {interval.emplace(val, val);}cout << interval.size() << endl;}vector<vector<int>> getIntervals() {vector<vector<int>> ans;for (auto& it : interval) {ans.push_back({it.first, it.second});}return ans;}
【基本数据结构】5.8任务相关推荐
- 数据结构(08)— 线性单链表基本操作
1. 线性单链表数据结构 // 假定每个结点的类型用 SNode 表示 typedef struct SNodeTag {int data; // 所存储的数据元素SNodeTag *next; // ...
- 数据结构(06)— 线性循环链表实战
1. 循环链表定义 单链的循环链表结点的存储结构和单链表的存储结构一样, 所不同的是: 最后一个结点的 next 域指向头结点, 而不是"空".这样, 由表尾很容易找到表头. 但若 ...
- 数据结构(05)— 线性单链表实战
1. 设计思路 本项目的实质是完成对考生信息的建立.查找.插入.修改.删除等功能,可以首先定义项目的数据结构,然后将每个功能写成一个函数来完成对数据的操作,最后完成主函数以验证各个函数功能并得出运行结 ...
- 数据结构(04)— 线性顺序表实战
1. 设计思路 本实战的实质是完成对学生成绩信息的建立.查找.插入.修改.删除等功能,可以首先定义项目的数据结构,然后将每个功能写成一个函数来完成对数据的操作,最后完成主函数以验证各个函数功能并得出运 ...
- 数据结构(03)— 数据处理基本操作(数据的查找、新增、删除、修改)
我们先来看一个关于查找的例子.查找,就是从复杂的数据结构中,找到满足某个条件的元素.通常可从以下两个方面来对数据进行查找操作: 根据元素的位置或索引来查找: 根据元素的数值特征来查找. 针对上述两种 ...
- 数据结构(02)— 时间复杂度与空间复杂度转换
1. 时间复杂度转化为空间复杂度 常用的降低时间复杂度的方法有递归.二分法.排序算法.动态规划等,降低空间复杂度的核心思路就是,能用低复杂度的数据结构能解决问题,就千万不要用高复杂度的数据结构. ...
- OpenCV 笔记(09)— 常用的数据结构和函数(Vec、Point、Scalar、Size、Rect、cvtColor)
1. Vec 对象类型 Vec 是一个主要用于数值向量的模板类.我们可以定义向量的类型和组件的数量: Vec<double, 19> myVector 我们还可以使用任何的预定义类型: t ...
- 数据结构与算法——线性结构——线性表及其表示
-"一,线性结构 1.顺序储存结构直接表示 多项式. 1).使用数组来表示多项式.(用数组下标来表示指数,值来表示系数) 可以表示成: 2).使用结构数组来表示.(把系数和指数看成一个二元组 ...
- 队列:实用程序服务和数据结构
队列:实用程序服务和数据结构 Queues: utility services and data structures 队列实用程序服务 Nucleus RTOS有四个API调用,它们提供与队列相关的 ...
- 管道:实用程序服务和数据结构
管道:实用程序服务和数据结构 Pipes: utility services and data structures 管道公用设施 Nucleus RTOS有四个API调用,它们提供与管道相关的实用程 ...
最新文章
- java能否访问PHP的服务器,PHP与Java进行通信的实现方法
- 找不到 tools.jar。请检查 C:\Program Files\Java\jre1.8.0_151 是否包含有效的 JDK 安装
- @Autowired注解警告Field injection is not recommended
- ORACLE中date类型字段的处理
- C# DataTable的Distinct解决方案及表的复制
- vsftp 简易部署使用
- 柯洁:我受够了AI围棋;联发科校园招聘,应届生年薪约45万元;Chrome 100发布 | 极客头条...
- 编译doubango时,ffmpeg先用 2.8.15,再用4.2.2
- 用华为eNSP仿真校园网
- 编写DBUtil及测试
- AutoJs学习-多点取色
- windows 安全删除硬件并弹出媒体 选项灰色、无法弹出
- 【Proteus仿真】DAC0832+51单片机汇编实现三角波输出
- js模板字符串自定义类名_详解JavaScript ES6中的模板字符串
- Python爬虫框架Scrapy入门(三)爬虫实战:爬取长沙链家二手房
- crosscheck命令
- ioredis自定义函数以及在ts声明文件库添加声明
- work转换html,翻转换图.html · 张斯佗罗夫柯基/personal work notes - Gitee.com
- window.event.keyCode - 兼容chrom和Firefox
- 【k8s的持久化存储】PV、PVC、StorageClass讲解
热门文章
- 华为emui10已经含有鸿蒙,华为emui10.0系统将让众多用户收益!
- winscp 列出‘/users‘的目录项时出错解决方法
- 父爱如山——别因为工作而忘记家人
- 《我看电商》读书笔记
- Spark GraphX在淘宝的实践
- python 网格形变_网格形变
- java网络爬虫论文_毕业设计(论文)-基于JAVA的网络爬虫的设计与实现.doc
- mysql解压后都有没有bin_解压版mysql安装
- 资源分享 | 字体 | 电脑字体素材
- nginx使用Brotli压缩