[LeetCode] 442. Find All Duplicates in an Array
来源:互联网 发布:php数组最大长度限制 编辑:程序博客网 时间:2024/06/05 15:07
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) { const int n = nums.size(); for (int i = 0; i < n; i++) nums[(nums[i] - 1) % n] += n; vector<int> res; for (int i = 0; i < n; i++) if (nums[i] > 2 * n) res.push_back(i + 1); return res; }};
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; }};
阅读全文
0 0
- [LeetCode]Find All Duplicates in an Array
- LeetCode Find All Duplicates in an Array
- Leetcode Find All Duplicates in an Array
- [Leetcode] Find All Duplicates in an Array
- leetCode-Find All Duplicates in an Array
- [leetcode] 442. Find All Duplicates in an Array
- LeetCode 442. Find All Duplicates in an Array
- leetcode:442. Find All Duplicates in an Array
- 442. Find All Duplicates in an Array(LeetCode)
- <leetcode>442. Find All Duplicates in an Array
- [leetcode]442. Find All Duplicates in an Array(java)
- [LeetCode]442. Find All Duplicates in an Array
- Leetcode 442. Find All Duplicates in an Array
- LeetCode笔记:442. Find All Duplicates in an Array
- 【LeetCode】 442. Find All Duplicates in an Array
- [leetcode]442. Find All Duplicates in an Array
- Leetcode 442. Find All Duplicates in an Array
- LeetCode 442. Find All Duplicates in an Array
- 转载:Longest Substring Without Repeating Characters
- 破解was 密码oxr格式的网站
- swagger完整教程。杜绝copy
- vs头文件无法include的问题
- Three.js学习
- [LeetCode] 442. Find All Duplicates in an Array
- Emmet:一个Html/Css快速编辑神器的插件
- Spring(二)
- css html 如何将图片img标签 水平居中 垂直居中 和水平垂直居中,能自适应浏览器缩放。
- Android全沉浸式的方式
- keil ucosiii工程改为 iar工程
- WebService与使用风格RPC/SOA/REST
- Python: decorator
- python基础之numpy