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: [3,0,1]
Output: 2
Example 2
Input: [9,6,4,2,3,5,7,0,1]
Output: 8
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[一个数还不是0]返回0:下次多想想一个数的情况
[思维问题]:
[一句话思路]:
就是for一遍
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
设置了一个返回值result,不知道怎么初始化。倒不如直接{里面出错结果} + 外面默认正常结果
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
直接{里面出错结果} + 外面默认正常结果
[复杂度]:Time complexity: O(n) Space complexity: O(1 就地for循环)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
First Missing Positive 越讨论越复杂的一道题
Find the Duplicate Number 快慢指针:好吧可以往这个方向思考
[代码风格] :
class Solution {
public int missingNumber(int[] nums) {
//ini = sort
Arrays.sort(nums);
//cc
if (nums[0] != 0) {
return 0;
}
//for
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i + 1] != nums[i] + 1) {
return nums[i] + 1;
}
}
//return
return nums.length;
}
}
268. Missing Number序列中遗失的数字的更多相关文章
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数.案例 1输入: [3,0,1]输出: 2案例 2输入: [9,6,4,2,3,5,7, ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- 268. Missing Number@python
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 (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number -- 找出0-n中缺失的一个数
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
随机推荐
- Supervisor进程监控
安装 yum install -y python-setuptools easy_install supervisor echo_supervisord_conf > /etc/supervis ...
- Spring、Spring MVC、Struts2优缺点整理
Spring 及其优点 大部分项目都少不了Spring的身影,为什么大家对他如此青睐,而且对他的追捧丝毫没有减退之势呢 Spring是什么: Spring是一个轻量级的DI和AOP容器框架. 说它轻量 ...
- Java实现三种常用的查找算法(顺序查查找,折半查找,二叉排序树查找)
public class Search { public class BiTreeNode{ int m_nValue; BiTreeNode m_pLeft; BiTreeNode m_pRight ...
- 转载FPGA学习之内嵌乘法器调用
补充一点,除法的时候如果直接a/b那么就会调用lpm模块,不管输入是否是常数,乘法的时候输入都是reg型变量会调用硬件乘法器,有一个是常数就会调用lpm模块. 上课的时候一直听老师说真正实践的时候你别 ...
- kafka ksql && docker 安装试用
备注: 使用docker 模式进行安装 1. 准备docker 环境(需要docker-compose) docker 的安装不需要进行多描述了,直接yum 或者源码编译也可以 ...
- 使用 key 登录时分开记录操作历史记录
线上服务器一般都是配置 key 登录,一个账号可以多个工作人员连接,操作命令历史却全部记录在一个文件中,当然后查看某条命令是谁执行的时候就不好查了.这时候我们就可以通过配置 histroy 相关环境变 ...
- mybatis-generator的坑
有天发现mybatis-generator不能用,要加什么根项目前缀, 搞到我重新从github下载,然后发现仓库用不了,原来新项目会改我的maven配置(新坑 后来终于明白要在子项目栏使用手脚架,就 ...
- MSSQL使用sqlbulkcopy批量插入数据
具体代码如下: /// <summary> /// 批量插入数据到BayonetZipFailedPic表 /// </summary> /// <param name= ...
- 安装ruby-pg报错解决
ruby用pgsql做orm的时候,需要安装ruby-pg库,默认编译安装会提示缺少xx头文件 max下,我用的傻瓜式pgsql.app gem install pg -- --with-pg-con ...
- ALUA and SRM
A couple important (ALUA and SRM) notes There’s some internal dialog today on our “VMware Champions” ...