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 < ...
随机推荐
- mongoDB 高级查询语法
http://www.cnblogs.com/ITAres/articles/2084794.html本文参考自官方的手册:http://www.mongodb.org/display/DOCS/Ad ...
- 零基础学python-1.5 第一个程序
这一个章节我们来说说怎么建立一个python程序 1.打开idle 2.点击File->new file,然后会弹出一个编辑窗体 3.在编辑窗体里面输入命令代码 程序代码: print(&quo ...
- 关于Linux网络配置
Linux网络配置 一:什么是网络接口卡以及如何查看网络接口的网络信息:在Linux系统中,主机的网络接口卡通常称为“网络接口”,我们可以使用ifconfig命令来查看网络 接口的信息(普通用户使用/ ...
- kafka 小案例【一】---设置但个消息集群
启动kafka服务 [ bin/kafka-server-start.sh config/server.properties ] [root@zhangxs kafka_2.]# bin/kafka- ...
- Apache安全和强化的十三个技巧
Apache是一个很受欢迎的web服务器软件,其安全性对于网站的安全运营可谓生死攸关.下面介绍一些可帮助管理员在Linux上配置Apache确保其安全的方法和技巧. 本文假设你知道这些基本知识: 文档 ...
- [译]GLUT教程 - 动画
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Animation 前面章节我们已经创建了一个白色三角形的窗体.还没到 ...
- Cocos2d-x 3.2 之 别踩白块(第三篇)
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- 动态创建Lambda表达式实现高级查询
需求简介 最近这几天做的东西总算是回归咱的老本行了,给投资管理项目做一个台账的东西,就是类似我们的报表.其 中有一个功能是一个高级查询的需求,在查询条件方面大概有7.8个查询条件.需求就是如果一个条件 ...
- 常见UI组件的一个模板
效果: 代码: using UnityEngine; using UnityEditor; using System.Collections; using System.IO; using Unity ...
- mysql-proxy做客户端连接转发【外网访问内网mysql】
功能 用于外网客户端连接内网的MySQL,将此工具安装在中转服务器上. 软件版本 mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz 简单的配置过程 解压后有 ...