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 ...
随机推荐
- 西交利物浦大学Java PAPER CODE: CSE105/12-13/S1/Resit Coursework
Question 6:What is an accessor method?What is a mutator method? 答案参考:http://www.xmsydw.com Number An ...
- VC++ windows开机自启动设置
设置开机启动 很多软件要求软件能够在开机时自启动,下面讲讲如何设置开机自启动. Windows设置程序的开机启动的方法有很多,这里只讲其中的一种,该方法同时适用于32位和64位的操作系统,只需将需要开 ...
- java中的修辞
1.final修辞 final修辞变量分为两种:基本类型(byte,short,int,long,float,double,char,boolean)也是值类型,则一旦初始化,值不能改变:应用类型(S ...
- LA3890 Most Distant Point from the Sea
题意 PDF 分析 可以二分答案,检验就用半平面交,如果平面非空则合法. 时间复杂度\(O(T n \log^2 n)\) 代码 #include<iostream> #include&l ...
- 一线互联网公司必备——最为详细的Docker入门吐血总结
在计算机技术日新月异的今天, Docker 在国内发展的如火如荼. 特别是在一线互联网公司 Docker 的使用是十分普遍的,甚至成为了一些企业面试的加分项,不信的话看看下面这张图. ...
- hdu 4609 3-idiots——FFT
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4609 答案就是随便选三条边的方案 - 不合法的方案. 不合法的方案就是算出 x+y = k 的方案数 g[ ...
- 18.Selenium+Python案例 -- 豆瓣
一.具体代码实现: from selenium import webdriver driver = webdriver.Firefox() driver.get('https://www.douban ...
- 利用bat合并两个hex文件
单片机程序如果有IAP功能的话,就会生成两个hex文件,一个是Boot,一个是App,如果给让生产烧录两个文件,就会降低生产效率,所以在烧录前最好将两个文件合并成一个文件,烧录一次即可,合并方法如下: ...
- linux(centos7) 安装nginx
linux(centos7) 安装nginx 1.14(stable) 版本 Nginx配置文件常见结构的从外到内依次是「http」「server」「location」等等,缺省的继承关系是从外到内, ...
- 【转】使用JMeter测试你的EJB
对EJB进行一些性能基准测试是非常有必要和有帮助的,测试的方法和工具有很多,不过我最近发现,Apache JMeter是进行基准测试的一个优秀工具.可惜的是,JMeter没有提供一个可测试任意EJB的 ...