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 ...
随机推荐
- [python] 排序的动态展示
两句闲话 本文所说的排序是指基于交换的排序.因此,按理来说,本文应该叫基于交换的排序的动态展示,但是这样太拗口了. 效果展示 最终效果如下. 实现方法 需要说明的是,在这里是通过pygame来实现图形 ...
- 【Android】Android 学习记录贴
官网 教程学习笔记 Genymotion 安卓虚拟器太慢,用Genymotion(装载eclipse的插件) 利用Genymotion运行Android应用程序 1.首先,点击 来启动或者创建您要使用 ...
- python库之selectors
在之前的博客中已经总结过分别在windows和linux操作系统下实现socket高并发(I/O异步)的方法,可以参考基于epoll的TP传输层实现和Windows之IOCP 下面对Python中实现 ...
- Java JDK安装和配置(Windows)
安装和配置JDK JDK中自带了JRE,不需要单独下载, 打开JDK安装, 选择安装目录,下一步,装完JDK,会问是否安装JRE,选下一步, 最后还会问是否安装Java FX, 装完后就全部完成了JD ...
- idea的有些路径问题
resource文件在xml的路径 http://www.cnblogs.com/zqr99/p/7642712.html resource文件在io流的读取 this.getClass().getR ...
- 洛谷 2831 (NOIp2016) 愤怒的小鸟——仅+1所以bfs优化
题目:https://www.luogu.org/problemnew/show/P2831 状压dp.跑得很慢.(n^2*2^n) 注意只打一只猪的情况. #include<iostream& ...
- 从JVM的角度解析String
1. 字符串生成过程 我们都知道String s = "hello java";会将“hello java”放入字符串常量池,但是从jvm的角度来看字符串和三个常量池有关,clas ...
- laravel 中使用定时任务
Laravel5.3 Artisan Console 文档地址 http://laravelacademy.org/post/6228.html 1.在服务器上查看定时任务有哪些crontab -e ...
- ONVIF、RTSP/RTP、FFMPEG的开发实录
前言 本文从零基础一步步实现ONVIF协议.RTSP/RTP协议获取IPC实时视频流.FFMPEG解码.开发环境为WIN7 32位 + VS2010. 最终成功获取浩云.海康.大华的IPC实时视频流. ...
- Logstash,flume,sqoop比较
Logstash: 1.插件式组织方式,易于扩展和控制 2.数据源多样不仅限于日志文件,数据处理操作更丰富,可自定义(过滤,匹配过滤,转变,解析......) 3.可同时监控多个数据源(input插件 ...