442. Find All Duplicates in an Array 难度:medium
来源:互联网 发布:mac在ps没有足够ram 编辑:程序博客网 时间:2024/06/06 04:01
题目:
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:[4,3,2,7,8,2,3,1]Output:[2,3]
思路:
把数组元素当做下一个位置坐标,标记一下,如果再遇到就加入到返回数组中。
程序:
class Solution {public: vector<int> findDuplicates(vector<int>& nums) { vector<int> res; if(nums.size() <= 1) return res; for(int i = 0;i < nums.size();i++) { int next = abs(nums[i]) - 1; if(nums[next] < 0) res.push_back(next + 1); else nums[next] = -nums[next]; } return res; }};
0 0
- 442. Find All Duplicates in an Array 难度:medium
- 题解——Leetcode 442. Find All Duplicates in an Array 难度:Medium
- 442. Find All Duplicates in an Array[Medium]
- 442. Find All Duplicates in an Array Difficulty : Medium
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array**
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array
- hibernate4-基于主键的单(双)向一对一的关联映射
- 从AlphaGo/Master到最强大脑小度机器人——我们要怎么正确对待人工智能
- 改IP学习心得
- redis3.0.7源码阅读(十三)main函数
- 文韬互联:我将windows服务器换成了linux系统
- 442. Find All Duplicates in an Array 难度:medium
- MongoDB
- java字符串数组初始化和赋值
- 文章标题
- LeetCode 476. Number Complement
- 《python 入门经典》读书笔记
- Codeforces 545D Queue【贪心+模拟】
- bindService启动的Service的生命周期
- Maya配置