LeetCode | 442. Find All Duplicates in an Array 限制时间复杂度O(n)空间复杂度O(1)的题

Givenan array of integers, 1 ≤ a[i] ≤ n (n =size of array), some elements appear twice andothers appear once.

Findall the elements that appear twice in this array.

Couldyou do it without extra space and in O(n) runtime?

Example:

Input:

[4,3,2,7,8,2,3,1]

 

Output:

[2,3]

这题要求给你一个数组,要你求出这个数组所有出现过两次的数字,并且要求时间复杂度不要超过O(n),空间复杂度不要超过O(1)

这里有两个非常重要的思路,就是,要求空间复杂度不要超过O(1),其实就是要求你定义的常数要是有限多个,并且不能定义长度随着n增大而增大的数组,其实就是不能定义额外的数组,但是你可以使用一开始给你的数组,这样也是符合规矩的,比如这一题一开始给你一个num数组,你就可以使用这个数组,而不会触犯规矩

其二就是其实只要添加一个bool数组就能轻松解决这一题,但是题目要求不能定义额外的数组,因此我们可以利用原数组,由于原数组每一个数都是正数,所以我们可以使用原数组每一个数字的正负号来代替bool数组的功能,这样就能够轻松在O(n)的时间内得到结果

class Solution {

public:

   vector<int> findDuplicates(vector<int>&nums) {

         vector<int>result;

 

         for (int i = 0;i < nums.size(); i++)

         {

                  intindex = abs(nums[i]) - 1;

                  if(nums[index] < 0)

                          result.push_back(index+1);

                  else

                          nums[index]= -nums[index];

         }

 

         return result;

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
string.find的复杂度取决于字符串的长度和查找的目标字符串的长度。在最坏情况下,使用string.find的复杂度为O(n*m),其中n是字符串的长度,m是目标字符串的长度。这是因为find函数需要遍历整个字符串来查找目标字符串。然而,在实际应用中,由于字符串的查找操作通常是在常数时间内完成的,因此可以将string.find的复杂度视为O(1)。总之,string.find的复杂度取决于具体的应用场景和字符串的长度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [LeetCode 3. 无重复字符的最长子串(O(1)空间复杂度,带大家回忆C++ string::find())](https://blog.csdn.net/qq_41138191/article/details/124015001)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [使用Python的BeautifulSoup库的简单爬虫示例.txt](https://download.csdn.net/download/weixin_44609920/88225605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [时间复杂度空间复杂度(C站最详细的)](https://blog.csdn.net/wh128341/article/details/119166041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值