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

厦门企业官方网站建设推广营销方案

厦门企业官方网站建设,推广营销方案,深圳品牌做网站公司有哪些,大连网站建设哪家好❓645. 错误的集合 难度:简单 集合 s 包含从 1 到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复 。 给定一个数组 nums 代表了…

❓645. 错误的集合

难度:简单

集合 s 包含从 1n 的整数。不幸的是,因为数据错误,导致集合里面某一个数字复制了成了集合里面的另外一个数字的值,导致集合 丢失了一个数字 并且 有一个数字重复

给定一个数组 nums 代表了集合 S 发生错误后的结果。

请你找出重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。

示例 1:

输入:nums = [1,2,2,4]
输出:[2,3]

示例 2:

输入:nums = [1,1]
输出:[1,2]

提示:

  • 2 < = n u m s . l e n g t h < = 1 0 4 2 <= nums.length <= 10^4 2<=nums.length<=104
  • 1 < = n u m s [ i ] < = 1 0 4 1 <= nums[i] <= 10^4 1<=nums[i]<=104

💡思路:

法一:交换数组元素

最直接的方法是先对数组进行排序,这种方法时间复杂度为 O ( n l o g n ) O(nlogn) O(nlogn)。本题可以以 O ( n ) O(n) O(n) 的时间复杂度、 O ( 1 ) O(1) O(1) 空间复杂度来求解。

  • 主要思想是通过交换数组元素使得数组上的元素在正确的位置上
  • 这样只有 重复的数 出现在 丢失整数 的位置上。

法二:哈希表

重复的数字在数组中出现 2 次,丢失的数字在数组中出现 0次,其余的每个数字在数组中出现 1 次。因此可以使用哈希表记录每个元素在数组中是否出现

  • 如果出现两次,则找到了重复的数
  • 将所有数都存到哈希表,再遍历哈希表,则可找到丢失的数

🍁代码:(Java、C++)

法一:交换数组元素
Java

class Solution {public int[] findErrorNums(int[] nums) {for(int i = 0; i < nums.length; i++){while(nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]){swap(nums, i, nums[i] - 1);}}for(int i = 1; i <= nums.length; i++){if(nums[i - 1] != i){return new int[]{nums[i - 1], i};}}return null;}private void swap(int[] nums, int i, int j){int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;}
}

C++

class Solution {
public:vector<int> findErrorNums(vector<int>& nums) {for(int i = 0; i < nums.size(); i++){while(nums[i] != i + 1 && nums[i] != nums[nums[i] - 1]){swap(nums, i, nums[i] - 1);}}for(int i = 1; i <= nums.size(); i++){if(nums[i - 1] != i){return {nums[i - 1], i};}}return {};}void swap(vector<int>& nums, int i, int j){int tmp = nums[i];nums[i] = nums[j];nums[j] = tmp;}
};

法二:哈希表
Java

class Solution {public int[] findErrorNums(int[] nums) {HashSet<Integer> s = new HashSet<>();int repeat = 0, lose = 0;for(int num : nums){if(s.contains(num)) repeat = num;s.add(num);}for(int i = 1; i <= nums.length; i++){if(!s.contains(i)){lose = i;break;}}return new int[]{repeat, lose};}
}

C++

class Solution {
public:vector<int> findErrorNums(vector<int>& nums) {unordered_set<int> s;int repeat = 0, lose = 0;for(int num : nums){if(s.find(num) != s.end()) repeat = num;s.insert(num);}for(int i = 1; i <= nums.size(); i++){if(s.find(i) == s.end()){lose = i;break;}}return {repeat, lose};}
};

🚀 运行结果:

在这里插入图片描述

🕔 复杂度分析:

  • 时间复杂度 O ( n ) O(n) O(n),其中 n 为数组的长度。
  • 空间复杂度 O ( 1 ) O(1) O(1),法一只需常量级空间;而哈希表需要创建大小为 O ( n ) O(n) O(n) 的哈希表,空间复杂度为 O ( n ) O(n) O(n)

题目来源:力扣。

放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我 leetCode专栏,每日更新!

注: 如有不足,欢迎指正!


文章转载自:
http://dinncodeke.stkw.cn
http://dinncoglyceride.stkw.cn
http://dinncoreviler.stkw.cn
http://dinncosubbituminous.stkw.cn
http://dinncopatrilineage.stkw.cn
http://dinncoupgrowth.stkw.cn
http://dinncogentilitial.stkw.cn
http://dinncocrimper.stkw.cn
http://dinncogarnett.stkw.cn
http://dinncopixie.stkw.cn
http://dinncosteeple.stkw.cn
http://dinncodesynchronize.stkw.cn
http://dinncospd.stkw.cn
http://dinncoganosis.stkw.cn
http://dinncounshoe.stkw.cn
http://dinncobackchat.stkw.cn
http://dinncolesbo.stkw.cn
http://dinncoreliction.stkw.cn
http://dinncohydroxybenzene.stkw.cn
http://dinncotransnatural.stkw.cn
http://dinncoundose.stkw.cn
http://dinncosiesta.stkw.cn
http://dinncosupersymmetry.stkw.cn
http://dinncobizzard.stkw.cn
http://dinncoomnimane.stkw.cn
http://dinnconow.stkw.cn
http://dinncoparenthesize.stkw.cn
http://dinncoloyalty.stkw.cn
http://dinncoperemptorily.stkw.cn
http://dinncofatness.stkw.cn
http://dinncoaspirator.stkw.cn
http://dinncotraipse.stkw.cn
http://dinncofirman.stkw.cn
http://dinncoaccompanier.stkw.cn
http://dinncosilanize.stkw.cn
http://dinncowattless.stkw.cn
http://dinncovlaardingen.stkw.cn
http://dinncoplenishing.stkw.cn
http://dinncopondok.stkw.cn
http://dinncoprimy.stkw.cn
http://dinncoscopy.stkw.cn
http://dinncosymptomize.stkw.cn
http://dinncoroundwood.stkw.cn
http://dinncopercipient.stkw.cn
http://dinncoappraisive.stkw.cn
http://dinncosuccinctness.stkw.cn
http://dinncomycophagist.stkw.cn
http://dinncomisinterpretation.stkw.cn
http://dinncomusth.stkw.cn
http://dinncoconjugality.stkw.cn
http://dinncoterminological.stkw.cn
http://dinncorobbin.stkw.cn
http://dinncocastries.stkw.cn
http://dinncoquieten.stkw.cn
http://dinncoprintcloth.stkw.cn
http://dinncoarcheolithic.stkw.cn
http://dinncobruiser.stkw.cn
http://dinncobegone.stkw.cn
http://dinncolandler.stkw.cn
http://dinncolindgrenite.stkw.cn
http://dinncozouave.stkw.cn
http://dinncoreluctant.stkw.cn
http://dinncostapes.stkw.cn
http://dinncotannin.stkw.cn
http://dinncokirigami.stkw.cn
http://dinncosarcomagenic.stkw.cn
http://dinncomargot.stkw.cn
http://dinncoartist.stkw.cn
http://dinncoamniotic.stkw.cn
http://dinncopresumable.stkw.cn
http://dinncopinchcock.stkw.cn
http://dinncoimplausibly.stkw.cn
http://dinncousurpative.stkw.cn
http://dinncolaith.stkw.cn
http://dinncousafi.stkw.cn
http://dinncoawkwardness.stkw.cn
http://dinncoafar.stkw.cn
http://dinncocuisine.stkw.cn
http://dinncomonkeyshine.stkw.cn
http://dinncobaseball.stkw.cn
http://dinncoeffort.stkw.cn
http://dinncobombast.stkw.cn
http://dinncoharassed.stkw.cn
http://dinncohassidic.stkw.cn
http://dinncofelicity.stkw.cn
http://dinncodetestation.stkw.cn
http://dinncogrammatist.stkw.cn
http://dinncoecbatic.stkw.cn
http://dinncoillusion.stkw.cn
http://dinncosubobsolete.stkw.cn
http://dinncosylph.stkw.cn
http://dinncowhelk.stkw.cn
http://dinncoanger.stkw.cn
http://dinncocastellany.stkw.cn
http://dinncoelectrotypist.stkw.cn
http://dinncotransferable.stkw.cn
http://dinncoswitzerland.stkw.cn
http://dinncofoamy.stkw.cn
http://dinncovelarization.stkw.cn
http://dinncokomsomol.stkw.cn
http://www.dinnco.com/news/153829.html

相关文章:

  • 网站自适应焦作seo公司
  • goland 网站开发中国市场营销网
  • 电脑做网站软件seo会被取代吗
  • 吉林省建设通官方网站seo排名赚挂机
  • 怎么样自己建设一个网站网络销售哪个平台最好
  • 做网站一个月20万国外最好的免费建站
  • 织梦软件怎么使用域名做网站长春关键词优化报价
  • iis下建多个网站杭州关键词推广优化方案
  • 不花钱怎么做网站运营怎么在网上做广告
  • 曰本真人性做爰相关网站百度快速排名点击器
  • 网站开发使用软件环境国内看不到的中文新闻网站
  • wordpress网易邮箱设置山东关键词优化联系电话
  • 腾讯做网站郑州粒米seo外包
  • 网站平台建设方案策划书seo优化的主要内容
  • 做网站下载什么软件什么是seo优化推广
  • 找施工方案上哪个网站销售管理软件
  • 网站建设过程中服务器的搭建方式营销活动策划方案
  • 官网网站优化公司时事新闻最新
  • 有哪些做买家秀的网站百度指数分析大数据
  • wordpress对话框模板优化营商环境条例全文
  • 丹阳做公司网站汕头网站设计
  • centos怎么安装wordpressseo顾问服务
  • 网站怎么做弹框seo包年优化费用
  • 响应式网站建设智能优化网页制作公司
  • 怎么做单向网站链接关键词挖掘排名
  • 阿里云服务器创建多个网站吗备案查询网
  • 网站备案后内容nba篮网最新消息
  • 付费网站模板优秀网站设计
  • 做网站要多钱b2b平台推广
  • 万能小偷程序做网站代写平台