LeetCode Array Easy 268. Missing Number
Given an array containing n distinct numbers taken from
0, 1, 2, ..., n, find the one that is missing from the array.Example 1:
Input: [,,]
Output:Example 2:
Input: [,,,,,,,,]
Output:Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
问题描述,一个数组包含n个不重复的数从0,1,2,3,...n 找到那个不在数组中的数
我的思路:先将数组排序再进行比较.但是这样子效率很低,时间复杂度在O=n+排序所需时间;
public int MissingNumber(int[] nums) {
Array.Sort(nums);
for(int i=; i < nums.Length; i++){
if(i != nums[i])
return i;
}
return nums.Length;
}

第二种思路: 从0到n的和为(0+n)(n+1)/2 再计算数组的和。两和的差就是结果。该算法时间复杂度为O(n)
public int MissingNumber(int[] nums) {
int n = nums.Length;
int sum = n*(n+)/;
int sums = nums.Sum();
return sum-sums;
}

LeetCode Array Easy 268. Missing Number的更多相关文章
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode&Python] Problem 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode Array Easy 485. Max Consecutive Ones
Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...
- LeetCode Array Easy 283. Move Zeroes
Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- 微信小程序(1)--新建项目
这些天看了一下最近特别火的微信小程序,发现和vue大同小异. 新建项目 为方便初学者了解微信小程序的基本代码结构,在创建过程中,如果选择的本地文件夹是个空文件夹,开发者工具会提示,是否需要创建一个 q ...
- Mysql --09 Innodb核心特性——事务
目录 Innodb核心特性--事务 1.什么是事务 2.事务的通俗理解 3.事务ACID特性 4.事务流程举例 5.事务的控制语句 6.事务隐式提交情况 7.事务日志redo基本功能 8.redo数据 ...
- python进行两个大数相加
python进行两个大数相加:由于int类型32位或64位都有长度限制,超出会内存溢出,无法计算,那么解决方法如下: 思路: 1.将超长数转换成字符串 2.进行长度补零,即让两个要计算的字符串长度一样 ...
- python常用函数 L
lstrip(str) 删除字符串左边的字符,支持传入参数. 例子: ljust(int) 格式化字符串,左对齐,支持传入填充值. 例子: loads(json) 将json字符串转换为dict. 例 ...
- docker-compose运行ES, Kibana和Cerebro
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11764003.html docker-compose.yaml version: '2.2' serv ...
- MySQL使用版本号实现乐观锁
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11608581.html 乐观锁适用于读多写少的应用场景 乐观锁Version图示 Project D ...
- 【leetcode】722. Remove Comments
题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...
- 【leetcode】1023. Camelcase Matching
题目如下: A query word matches a given pattern if we can insert lowercaseletters to the pattern word so ...
- php ltrim()函数 语法
php ltrim()函数 语法 ltrim()函数怎么用? php ltrim()函数用于删除字符串左边的空格或其他预定义字符,语法是ltrim(string,charlist),返回经过charl ...
- 4412 4路pwm输出
一.4412 xpwmTOUT1 这是4412的GPD0_1路,itop中被使用为LCD的背光电路的pwm功能.因此如果使用教程中的代码,同样操作GPD0_1是行不通的. 会出现错误,所以需要解除在内 ...