给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?
示例:
输入:
[4,3,2,7,8,2,3,1]
输出:
[2,3]
详见:https://leetcode.com/problems/find-all-duplicates-in-an-array/description/

C++:

方法一:

class Solution {
public:
vector<int> findDuplicates(vector<int>& nums) {
vector<int> res;
for(int i=0;i<nums.size();++i)
{
int idx=abs(nums[i])-1;
if(nums[idx]<0)
{
res.push_back(idx+1);
}
nums[idx]=-nums[idx];
}
return res;
}
};

方法二:

class Solution {
public:
vector<int> findDuplicates(vector<int>& nums)
{
vector<int> res;
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != nums[nums[i] - 1])
{
swap(nums[i], nums[nums[i] - 1]);
--i;
}
}
for (int i = 0; i < nums.size(); ++i)
{
if (nums[i] != i + 1)
{
res.push_back(nums[i]);
}
}
return res;
}
};

参考:https://www.cnblogs.com/grandyang/p/6209746.html

442 Find All Duplicates in an Array 数组中重复的数据的更多相关文章

  1. LeetCode 442. 数组中重复的数据(Find All Duplicates in an Array) 17

    442. 数组中重复的数据 442. Find All Duplicates in an Array 题目描述 Given an array of integers, 1 ≤ a[i] ≤ n (n ...

  2. Leetcode#442. Find All Duplicates in an nums(数组中重复的数据)

    题目描述 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O(n)时间复杂度内解 ...

  3. Java实现 LeetCode 442 数组中重复的数据

    442. 数组中重复的数据 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次. 找到所有出现两次的元素. 你可以不用到任何额外空间并在O( ...

  4. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  5. [Swift]LeetCode442. 数组中重复的数据 | Find All Duplicates in an Array

    Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...

  6. leetcode 217. Contains Duplicate 287. Find the Duplicate Number 442. Find All Duplicates in an Array 448. Find All Numbers Disappeared in an Array

    后面3个题都是限制在1-n的,所有可以不先排序,可以利用巧方法做.最后两个题几乎一模一样. 217. Contains Duplicate class Solution { public: bool ...

  7. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  8. 442. Find All Duplicates in an Array - LeetCode

    Question 442. Find All Duplicates in an Array Solution 题目大意:在数据中找重复两次的数 思路:数组排序,前一个与后一个相同的即为要找的数 Jav ...

  9. php去除数组中重复数据

    <?php /** * 去除数组中重复数据 * by www.jbxue.com **/ $input = array("a" => "green" ...

随机推荐

  1. MySql 基本操作语句整理

    数据库 DATABASE: 创建 CREATTE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] cha ...

  2. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 【部分题解】

    2014牡丹江亚洲区域赛邀请赛 B题:图论题目 题解:这里 K题:想法题 分析:两种变化.加入和交换.首先:星号是n的话最少须要的数字是n+1,那么能够首先推断数字够不够,不够的话如今最前面添数字,假 ...

  3. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

  4. System.Diagnostics.Debug.WriteLine 在OutPut中无输出

    TextWriterTraceListener writer = new TextWriterTraceListener(System.Console.Out);              Debug ...

  5. hive计算网页停留时长

    hive表结构例如以下: create table pv_user_info( session_id string, user_id string, url string, starttime big ...

  6. C语言 字符串操作 笔记

    /* C语言字符串的操作笔记 使用代码和注释结合方式记录 */ # include <stdio.h> # include <string.h> int main(void) ...

  7. 3.2.1 配置构建Angular应用——简单的笔记存储应用——展示功能

    本节我们会通过构建一个简单的笔记存储应用(可以载入并修改一组简单的笔记)来学习如何应用Angular的特性.这个应用用到的特性有: 在JSON文件中存储笔记 展示.创建.修改和删除笔记 在笔记中使用M ...

  8. 有oracle 10g,但没有安装arcgis,又想使用空间数据库的解决方案

    我在一台虚拟机中部署系统进行测试,配置如下: OS:WIN2008 R2 SP1 X64 DB: oracle 12c 结果系统报错,查找原因,原来是oracle里还不支持arcgis的一些所谓的空间 ...

  9. 让Linq的OrderBy支持动态字段

    使用linq的OrderBy,如果明确知道是哪个字段,当然很容易: IQueryable<User> userQuery = ...; userQuery.OrderBy(u => ...

  10. 浏览器同部署了https的服务器交互的过程

    1 浏览器发起https请求 2 https服务器发送自己的公钥给浏览器 3 浏览器用https服务器发送过来的公钥加密一个用于双方通信的的对称密码 4 https服务器用自己的私钥解密,获取对称密码 ...