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 < ...
随机推荐
- C#利用SharpZipLib解压或压缩文件(支持多层目录递归压缩)
需要下载ICSharpCode.SharpZipLib.dll using System; using System.Collections.Generic; using System.Linq; u ...
- 无法调试存储过程,无法启动T-SQL调试
用本机管理员或者具有SYSADMIN角色的帐号登录,不要用.,用实例名来连接
- vim命令行模式
1. 激活命令行模式 : 进入命令行模式 <Esc> 退出命令行模式 2. 常用命令 :p 打印 (:print) :e 读入文件 (:edit) :w 写入文件 (:write) :t ...
- js 正则表达式 取反
http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp 以匹配中文为例 const test_value = '李钊鸿' if (/[^\u4e00 ...
- lua学习笔记(十三)
math库 定义在math中 所有三角函数都使用弧度 指数和对数函数 取整函数 伪随机数math.random 调用时没有参数返回0~1之间的随 ...
- web开发中比较常用的html标签
作为一名web开发人员,总结了一下经常用到的html标签,如下:<html> <head> <!--meta头标签 表示页面三秒后跳转到新浪页面--> <me ...
- memcached使用总结
我的linux版本信息:Linux version 4.4.0-78-generic (buildd@lgw01-11) (gcc version 5.4.0 20160609 (Ubuntu 5.4 ...
- 【Mac + Pycharm】之实用东西以及配置东西
一.新建.py文件时默认模板: 步骤:File => Preferences for New Projects => Editor => File and Code Template ...
- 公告板shader
Shader "Custom/LightPoint" { Properties { _MainTex ("Main Tex", 2D) = "whit ...
- Linux时间时区详解与常用时间函数
时间与时区 整个地球分为二十四时区,每个时区都有自己的本地时间. Ø UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT = UTC),格林威治时间和UTC时间都用秒数来计 ...