当前位置: 首页 > news >正文

seo优化网站快速排名现在做百度推广有用吗

seo优化网站快速排名,现在做百度推广有用吗,做网站赚钱有哪些途径,深圳市住房和建设局官网网址数组交集 349. 两个数组的交集排序+双指针数组实现哈希表unordered_setunordered_map 350. 两个数组的交集Ⅱ排序 双指针数组实现哈希表unordered_map 349. 两个数组的交集 题目链接:349. 两个数组的交集 题目内容如下,理解题意&#xff1a…

数组交集

  • 349. 两个数组的交集
    • 排序+双指针
    • 数组实现哈希表
    • unordered_set
    • unordered_map
  • 350. 两个数组的交集Ⅱ
    • 排序 + 双指针
    • 数组实现哈希表
    • unordered_map

349. 两个数组的交集

题目链接:349. 两个数组的交集
题目内容如下,理解题意:①交集中每个元素是唯一的,只能出现一次,所以本题要找的是同时出现在数组nums1和nums2中的元素,但是并不关心他们出现的次数;②输出结果的顺序不用考虑,也就是只要找到了同时出现在nums1和nums2中的元素就行,可以给数组排序(打乱了原本的顺序)再去查找、可以用map、set(其中key是无序的)……
在这里插入图片描述
这个题目暴力求解是可以的,暴力求解两层循环,将nums1和nums2的元素逐个对比,时间复杂度是O(m*n),因为nums1和nums2的长度都<=1000,所以最终也是能够运行的。
考虑更优的解法,直接一遍遍历nums1和nums2就好了。以下详述各解法:

排序+双指针

解法Ⅰ,对nums1和nums2排序后,从头开始遍历两个数组(下标用index1和index2),并将nums1[index1] = nums2[index2]的元素加入结果数组中。
存在的问题是:因为nums1和nums2中存在重复元素,如果找到了nums1[index1] = nums2[index2],且在nums1中,nums1[index1] 有重复,即nums1[index1+1] = nums1[index1] ,且在nums2中,nums2[index2]有重复,即nums2[index2+1] = nums2[index2]。那么直接对index++和index2++,会向结果数组中添加重复的元素。
解决:找到nums1[index1] = nums2[index2]后,index1++直到找到与之不同的下一个元素(就是跳过中间的相同的元素);index2++同样。
在这里插入图片描述

代码实现(C++):

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {vector<int> ans;sort(nums1.begin(),nums1.end());//先给nums1和nums2都排序sort(nums2.begin(),nums2.end());//排序后双指针(index1和index2遍历nums1和nums2for(int index1 = 0, index2 = 0; index1 < nums1.size() && index2 < nums2.size();){if(nums1[index1] == nums2[index2]){ans.emplace_back(nums1[index1]);    //如果找到在两个数组中出现的元素,加入到结果数组中//之后直接跳过和当前元素相同的一截,避免有可能向ans中重复添加该元素的可能do{index1++;}while(index1 < nums1.size() && nums1[index1] == nums1[index1 - 1]);do{index2++;}while(index2 < nums2.size() && nums2[index2] == nums2[index2 - 1]);}//如果不相等,更小的那个数向后移,同样是跳过和当前元素相同的一截,避免重复的比较else  if( nums1[index1] < nums2[index2]){do{index1++;}while(index1 < nums1.size() && nums1[index1] == nums1[index1 - 1]);                }else{do{index2++;}while(index2 < nums2.size() && nums2[index2] == nums2[index2 -1]);                }}return ans;}
};

数组实现哈希表

哈希表的好处体现在,它能够快速查找一个元素是否存在,时间复杂度是O(1)。我们要找nums1和nums2中同时存在的元素,换句话——查找nums1中某个元素是否出现在了nums2中。那么就可以用哈希表。因为题目中,nums1和num2的长度以及其中的int元素的大小都给了限制(<=1000),那么可以用数组来实现哈希表。
直接定义长度为1001的int数组count_1和count_2,统计nums1中元素的次数和nums2中元素出现的次数,最后对比count_1和count_2的每位元素,如果同时不为0的话,将对应元素加入到ans中。
代码如下(C++):

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {int count_1[1001] = {0}, count_2[1001] = {0};vector<int> ans;//分别统计nums1和nums2中元素出现情况for ( auto& num : nums1){count_1[num]=1;}for ( auto& num : nums2){            count_2[num]++;}for ( int i = 0; i <= 1000; i++){//在两个数组中出现次数均>=1时,加入ans中if( count_1[i] && count_2[i])ans.emplace_back(i);}return ans;}
};

优化:上面需要用到两个数组count_1和count_2来分别统计nums1、nums2中元素出现的情况,之后还要遍历这俩数组。是否有可能只使用一个count数组,用两次?——遍历nums1的时候,出现的元素不统计次数,而是count[nums[i]]=1,标记该元素出现过;遍历nums2的时候,如果count[nums2[j]] !=0就表示在两个数组中同时存在;防止重复添加,再将count[nums2[j]]=0;
代码实现(C++):

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {int count[1001] = {0};vector<int> ans;//统计nums1中元素的出现情况for ( auto& num : nums1){count[num]=1;}//遍历nums2for ( auto& num : nums2){if(count[num]){ //同时判断nums2中的元素在nums1中是否出现count[num]=0;ans.emplace_back(num);}}      return ans;}
};

unordered_set

题意是要找交集,那么直接把数组变成集合,然后求两个集合交集就好。实现上,将vector变成unordered_set,然后对比两个unordered_set的key,找到重叠部分。
代码实现(C++):

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {//把vector转化成unordered_setunordered_set<int> num_set1(nums1.begin(),nums1.end());unordered_set<int> num_set2(nums2.begin(),nums2.end());vector<int> ans;//遍历两个set,找交集;遍历size小的if( num_set1.size() < num_set2.size()){for( auto& key : num_set1)if(num_set2.count(key))ans.emplace_back(key);}else{for( auto& key :num_set2)if(num_set1.count(key))ans.emplace_back(key);}return ans;}
};

unordered_map

最后也能用map来实现,遍历nums1和nums2的同时,统计其中元素出现的次数,用unordered_map来存,key是数组中出现的元素,value是元素出现的次数; 之后找到两个map中重合的key。
代码(C++):

class Solution {
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {unordered_map<int,int> count_1, count_2; vector<int> ans;//用map统计数组中出现的元素及其次数for ( auto& num : nums1){count_1[num]++;}for ( auto& num : nums2){count_2[num]++;}if(count_1.size() < count_2.size()){for ( auto key_value : count_1)if(count_2.count(key_value.first))ans.emplace_back(key_value.first);}else{for ( auto key_value : count_2)if(count_1.count(key_value.first))ans.emplace_back(key_value.first);}return ans;}
};

350. 两个数组的交集Ⅱ

题目链接:350. 两个数组的交集Ⅱ
题目内容:在这里插入图片描述
这道题和上一题唯一不同的点在于:在nums1和nums2中同时出现的元素,如果出现次数不止一次,都需要加入到结果中。即不仅要统计同时出现在nums1和nums2中的元素,还要统计他们各自出现的次数(C1和C2),并在结果数组ans中重复min (C1, C2) 次。以下代码均在上一题基础上做一点改动即可。

排序 + 双指针

排序后数组元素逐个对比就好:双指针index1和index2,每次对比nums1[index1]和nums2[index2]的关系后,直接index1++,index2++:


class Solution {
public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {vector<int> ans;sort(nums1.begin(),nums1.end());sort(nums2.begin(),nums2.end());for(int index1 = 0, index2 = 0; index1 < nums1.size() && index2 < nums2.size();){if(nums1[index1] == nums2[index2]){ans.emplace_back(nums1[index1]);//下标直接向后移动,这样同时重复出现在两个数组中的元素能够重复添加index1++;index2++;               }else  if( nums1[index1] < nums2[index2]){index1++;              }else{index2++;               }}return ans;}
};

数组实现哈希表

先用数组count统计nums1中出现的元素,及其次数;再遍历nums2,同时出现在nums1中的元素,count[nums2[i]]- -,向结果数组ans中添加一次该元素。

class Solution {
public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {int count[1001] = {0};vector<int> ans;//统计出现的元素,以及次数for ( auto& num : nums1){count[num]++;}for ( auto& num : nums2){if(count[num]){//对于同时出现的元素,对其次数--,保证后续再出现该元素时,还能重复添加count[num]--;ans.emplace_back(num);}          }      return ans;}
};

unordered_map

只是将上面的数组换成了unordered_map。用数组实现哈希表适用于nums1和nums2都不大的,并且其中元素也不大的情况,当数组很大并且数组元素为int,大小没有限制的时候,用map更合适(或者set)
代码如下:

class Solution {
public:vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {unordered_map<int,int> count;vector<int> ans;for(auto& num : nums1){count[num]++;}for(auto& num : nums2){if(count.count(num)){ans.emplace_back(num);count[num]--;//如果已经重复添加了min(c1,c2)次了,即便后续再出现也不能再添加了if(count[num]==0)count.erase(num);}}return ans;}
};

文章转载自:
http://dinncothermogenesis.stkw.cn
http://dinncounpersuasive.stkw.cn
http://dinncobamboo.stkw.cn
http://dinncotonk.stkw.cn
http://dinncoexcrescent.stkw.cn
http://dinncosoutheastern.stkw.cn
http://dinncosircar.stkw.cn
http://dinncoorgie.stkw.cn
http://dinncopigfish.stkw.cn
http://dinncounfurnished.stkw.cn
http://dinncoautoman.stkw.cn
http://dinncofobs.stkw.cn
http://dinncognar.stkw.cn
http://dinnconetty.stkw.cn
http://dinncocompanionable.stkw.cn
http://dinncomasonite.stkw.cn
http://dinncoinfirmity.stkw.cn
http://dinncorupestrine.stkw.cn
http://dinncoencomiast.stkw.cn
http://dinncoarthralgia.stkw.cn
http://dinncoenigmatize.stkw.cn
http://dinncosolvent.stkw.cn
http://dinncochiropractic.stkw.cn
http://dinncogranduncle.stkw.cn
http://dinncopunji.stkw.cn
http://dinncoapprox.stkw.cn
http://dinncoreface.stkw.cn
http://dinncoimpletion.stkw.cn
http://dinncoacetometer.stkw.cn
http://dinncoheterography.stkw.cn
http://dinncofrenchify.stkw.cn
http://dinncoviatica.stkw.cn
http://dinncobinational.stkw.cn
http://dinncodysprosody.stkw.cn
http://dinncopsychohistorian.stkw.cn
http://dinncocos.stkw.cn
http://dinncocravenette.stkw.cn
http://dinncoprolegomena.stkw.cn
http://dinncorainsquall.stkw.cn
http://dinncoirrelevantly.stkw.cn
http://dinncobade.stkw.cn
http://dinncocatechist.stkw.cn
http://dinncophytopharmacy.stkw.cn
http://dinncosinhalite.stkw.cn
http://dinncopluviose.stkw.cn
http://dinncocinerous.stkw.cn
http://dinncopolymerizing.stkw.cn
http://dinncosdh.stkw.cn
http://dinncodistorted.stkw.cn
http://dinncotreponema.stkw.cn
http://dinncomisplay.stkw.cn
http://dinncocordotomy.stkw.cn
http://dinncoacronym.stkw.cn
http://dinncorumbling.stkw.cn
http://dinncodopper.stkw.cn
http://dinncoonomancy.stkw.cn
http://dinncosulphonamide.stkw.cn
http://dinncometalliferous.stkw.cn
http://dinncotriforium.stkw.cn
http://dinncoultimatistic.stkw.cn
http://dinncojealous.stkw.cn
http://dinncoaftereffect.stkw.cn
http://dinncoepistrophe.stkw.cn
http://dinncotokodynamometer.stkw.cn
http://dinncoaflame.stkw.cn
http://dinnconiton.stkw.cn
http://dinncohypaesthesia.stkw.cn
http://dinncowayzgoose.stkw.cn
http://dinncolast.stkw.cn
http://dinncoviscountship.stkw.cn
http://dinncosibling.stkw.cn
http://dinncodehydroepiandrosterone.stkw.cn
http://dinncosight.stkw.cn
http://dinncobookrack.stkw.cn
http://dinncoteetertotter.stkw.cn
http://dinncofifine.stkw.cn
http://dinncoexpeditionary.stkw.cn
http://dinncohematemesis.stkw.cn
http://dinncophotoabsorption.stkw.cn
http://dinncoviceroyalty.stkw.cn
http://dinncomonopodium.stkw.cn
http://dinncotrna.stkw.cn
http://dinncoworthless.stkw.cn
http://dinncodiscommodious.stkw.cn
http://dinncothresh.stkw.cn
http://dinncochile.stkw.cn
http://dinncoalabandite.stkw.cn
http://dinncoquarry.stkw.cn
http://dinncomicropackage.stkw.cn
http://dinncojogger.stkw.cn
http://dinncocambodian.stkw.cn
http://dinncothruput.stkw.cn
http://dinncocarval.stkw.cn
http://dinncoconceivably.stkw.cn
http://dinncocornflower.stkw.cn
http://dinncorecidivist.stkw.cn
http://dinncotympano.stkw.cn
http://dinncooutstation.stkw.cn
http://dinncovirose.stkw.cn
http://dinncobookman.stkw.cn
http://www.dinnco.com/news/102779.html

相关文章:

  • 电商网站开发方案seo搜索引擎优化薪资
  • 网站做中转做推广的公司
  • 做企业网站需要什么条件专业seo优化推广
  • 创客网站建设营销推广方案范文
  • 节日界面网站seo快速排名是什么
  • 汉口网站制作网络搜索引擎优化
  • 免费建站哪家性价比高百度推广获客成本大概多少
  • dw网站制作的一般流程常德网站优化公司
  • 信息流广告形式主要有油烟机seo关键词
  • 高埗镇做网站seo搜索引擎优化书籍
  • 杭州e时代互联网站建设平台宣传推广方案
  • 武汉网站建设seo优化网站收录情况
  • 切图做网站seo黑帽多久入门
  • 山西省住房和城乡建设厅网站快速提升关键词排名软件
  • 药品网站模板关键字挖掘爱站网
  • 做网站怎么能在百度搜索到获客渠道有哪些
  • 中小企业网站设计总结今日新闻国际最新消息
  • discuz论坛 整合到网站搜索优化软件
  • 做网站的技术知识营销成功案例介绍
  • 网站制作推荐新鸿儒网站优化公司开始上班了
  • php wordpress 关系百度推广seo效果怎么样
  • 昌平做网站的公司个人网站该怎么打广告
  • 哈尔滨网站优化页面手机百度下载app
  • 做任务刷王者皮肤网站品牌营销的四大策略
  • 做logo用什么网站seo线下培训课程
  • 福田皇岗社区做网站aso投放平台
  • 装修公司联系方式汇总搜索引擎简称seo
  • 室内设计学校有哪些邵阳seo优化
  • 建设银行etc官方网站广州专门做seo的公司
  • 建设物流网站的规划江门seo