Data Structure Array: Find the Missing Number
http://www.geeksforgeeks.org/find-the-missing-number/
1. use sum formula, O(n), O(1)
2. use XOR, O(n), O(1)
3. use counting sort, O(n), O(1), changing the array
4. use negtive, O(n), O(1), chaning the array
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; int missing(int arr[], int n) {
for (int i = ; i < n; i++) {
if (abs(arr[i]) > n) continue;
arr[abs(arr[i])-] = -arr[abs(arr[i])-];
}
for (int i = ; i < n; i++) {
if (arr[i] > ) return i + ;
}
} int main() {
int arr[] = {, , , , };
cout << missing(arr, ) << endl;
return ;
}
Data Structure Array: Find the Missing Number的更多相关文章
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-tha ...
- Data Structure Array: Largest subarray with equal number of 0s and 1s
http://www.geeksforgeeks.org/largest-subarray-with-equal-number-of-0s-and-1s/ #include <iostream& ...
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- Data Structure Array: Move all zeroes to end of array
http://www.geeksforgeeks.org/move-zeroes-end-array/ #include <iostream> #include <vector> ...
- Data Structure Array: Find if there is a subarray with 0 sum
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #incl ...
- Data Structure Array: Sort elements by frequency
http://www.geeksforgeeks.org/sort-elements-by-frequency-set-2/ #include <iostream> #include &l ...
- Data Structure Array: Maximum circular subarray sum
http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/ #include <iostream> #include < ...
- Data Structure Array: Find the two numbers with odd occurrences in an unsorted array
http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ #include ...
- Data Structure Array: Longest Monotonically Increasing Subsequence Size
http://www.geeksforgeeks.org/longest-monotonically-increasing-subsequence-size-n-log-n/ #include < ...
随机推荐
- libpointmatcher的filter
Maximum Density Filter Points are only considered for rejection if they exceed a density threshold, ...
- C语言学习笔记(一) 开发环境的搭建
写这个系列的原因是因为最近在学习C语言,记录博客会让自己能够更好的掌握学习到的东西.编程贵在坚持,每天改变一丢丢! C语言开发两个软件,一个是文本编辑工具,Notepad++或者是EditPlus都可 ...
- Java中的split函数的用法
Java中的 split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回: 例如: String str="1234@abc"; String[] a ...
- 170621 - Android ADB forward端口映射和reverse反向代理 使用笔记
个人理解 forward:端口映射 将本地PC指定Port端口,映射到设备手机指定Port端口上.以便解决 PC -> Phone 的访问问题PC 作为Client客户端 可以任意访问 Phon ...
- Eclipse Plugin Installation and Windows User Access Control
I make Eclipse Plugins and I sell them to developers using Eclipse. Most of the visitors to my web s ...
- Unity3d 发动机原理详细介绍
Unity3d 发动机原理详细介绍 www.MyException.Cn 发布于:2013-10-08 16:32:36 浏览:46次 0 Unity3d 引擎原理详细介绍 体系结构 ...
- synchronized 锁优化
synchronized 在jdk 1.7之前是重量级锁,独占锁,非公平锁.jdk1.7之后,synchronized引入了 偏向锁,自旋锁,轻量级锁,重量级锁 自旋锁 当线程在获取锁的时候,如果发现 ...
- Android程序的打包和安装
当我们使用Android Studio的时候,这些步骤都交给它去做了. 编译 classes.dex 文件 编译 resources.arsc 文件 生成资源索引表resources.arsc. 把r ...
- java中什么是bridge method(桥接方法)
java中什么是bridge method(桥接方法) https://blog.csdn.net/z69183787/article/details/81115524
- 验证-- email类型输入框(电子邮件地址)--multiple
如果需要一个用来填写电子邮件地址的输入框,可以使用email类型.这样浏览器可以帮我们验证格式是否正确,而不需要自己写验证规则.原文:HTML5新控件 - email类型输入框(电子邮件地址) 1,只 ...