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 ...
随机推荐
- bzoj 1226 学校食堂Dining
Written with StackEdit. Description 小\(F\) 的学校在城市的一个偏僻角落,所有学生都只好在学校吃饭.学校有一个食堂,虽然简陋,但食堂大厨总能做出让同学们满意的菜 ...
- 关于setdefault和defaultdict
c参考链接:http://blog.csdn.net/real_ray/article/details/17919289 defaultdict就是为没有的键给一个默认的值,实际是实现了一个__mis ...
- JavaScript 冒号(:)详解
1.switch语句分支 2.?:三元表达式的false 3.声明对象直接量的成员 4.声明标签 1和2相信地球人都知道吧?如果有人不知道,那我改成地球上的程序员都知道,哈哈 3.对象直接量我们也经常 ...
- 7 函数——《Swift3.0从入门到出家
6 函数 函数就是对某个功能的封装,一个swift程序可能由多个函数组成 swift中定义函数的格式: func 函数名称(参数列表) —>函数返回值类型{ 函数体 return } 函数定义要 ...
- bzoj 1000 A+B Problem (And also my first experience of Emacs)
problem:https://www.lydsy.com/JudgeOnline/problem.php?id=1000 This is my first code under Emacs! #in ...
- JVM知识整理和学习(转载并修改)
JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构的设计原理. 冯·诺依曼体系结构中,指出计算机处理的数据和指令都是二进制数,采用存储程序方式不加区分的存储在同一个存储器里,并且顺序执行,指令由 ...
- MySQL-5.7中InnoDB表数据文件存储位置
学习地址:https://www.cnblogs.com/tongxiaoda/p/7874535.html
- Voting and Shuffling to Optimize Atomic Operations
2iSome years ago I started work on my first CUDA implementation of the Multiparticle Collision Dynam ...
- nagios 监控shell脚本
线上应用shell脚本 参考链接:http://os.51cto.com/art/201301/376725.htm 0--各方面都正常,检查成功完成. 1--资源处于警告状态.某个地方不太妙. 2- ...
- ubuntu crontab 不执行的解决方法
在脚本文件的第二行添加下面一句即可 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 下面是分析解决问题的步骤: 1. ...