乘风破浪:LeetCode真题_041_First Missing Positive
乘风破浪:LeetCode真题_041_First Missing Positive
一、前言
这次的题目之所以说是难,其实还是在于对于某些空间和时间的限制。
二、First Missing Positive
2.1 问题

2.2 分析与解决
读了题目我们或许还不理解题意,其实是说正常的数组应该是[1,2,3,4....]这样的数组,或者打乱次序的,但是对于不是这样的数组,我们需要从中找到从那个数字开始缺少的,这个数字就是最小的数字,比如2,3,4,那么缺少的就是1。比如1,2,4,就是少了3。这样我们就理解了。但是时间复杂度要求O(n),空间使用是常数级别的。
我们仔细思考一下,其实下标和这个是有关系的,正常的,最终都可以变成num[i]=i+1,于是我们可以将数组进行一边扫描,如果已经满足这种性质的不用动,如果num[i]的值小于1或者大于长度的都是不应该存在的,因此置为一个标志,便于以后报错。如果在范围之内的,我们就让它们物归原位。比如num[i]=x,那么这个元素应该在下标为x-1的位置上,因此我们将num[x-1]和num[i]上的元素进行交换。交换的时候如果发现这两个值相等,则将非本位的值置为零,否者直接交换。
class Solution {
// let's rearrange the numbers in the array between 1 and length
// in order (in place), leaving a 0 for numbers not present,
// and ignoring the numbers out of this range.
// then in second pass find the first zero occurence, or if none,
// return length+1
// for example, [3,4,-1,1] will become [1,0,3,4]
public int firstMissingPositive(int[] nums) {
for (int i=0; i<nums.length;) {
int n = nums[i];
if (n<1 || n>nums.length) {
nums[i]=0; // out of range, remove
i++;
} else if (n-1==i) {
i++; // in range and in position, leave as is
} else {
// in range but not in position, swap
int temp = nums[n-1];
nums[n-1]=n;
nums[i]=(temp==n)?0:temp;//这里最妙的就是没有i++,以此确保下一次还从这个地方开始
}
}
for (int i=0; i<nums.length; i++) {
if (nums[i]==0)
return i+1;
}
return nums.length+1;
}
}

三、总结
遇到问题需要和实际的条件相联系,另外需要认真的判断所有的可能。
乘风破浪:LeetCode真题_041_First Missing Positive的更多相关文章
- LeetCode刷题 fIRST MISSING POSITIVE
Given an unsorted integer array,find missing postive integer. For example , Given [1,2,0]return 3, a ...
- 乘风破浪:LeetCode真题_008_String to Integer (atoi)
乘风破浪:LeetCode真题_008_String to Integer (atoi) 一.前言 将整型转换成字符串,或者将字符串转换成整型,是经常出现的,也是必要的,因此我们需要熟练的掌握,当然也 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_038_Count and Say
乘风破浪:LeetCode真题_038_Count and Say 一.前言 这一道题目,很类似于小学的问题,但是如果硬是要将输入和结果产生数值上的联系就会产生混乱了,因此我们要打破思维定势. ...
- 乘风破浪:LeetCode真题_037_Sudoku Solver
乘风破浪:LeetCode真题_037_Sudoku Solver 一.前言 这次我们对于上次的模型做一个扩展并求解. 二.Sudoku Solver 2.1 问题 2.2 分析与解决 这道题 ...
- 乘风破浪:LeetCode真题_036_Valid Sudoku
乘风破浪:LeetCode真题_036_Valid Sudoku 一.前言 有的时候对于一些基础知识的掌握,对我们是至关重要的,比如ASCII重要字符的表示,比如一些基本类型的长度. 二.Valid ...
- 乘风破浪:LeetCode真题_035_Search Insert Position
乘风破浪:LeetCode真题_035_Search Insert Position 一.前言 这次的问题比较简单,也没有限制时间复杂度,但是要注意一些细节上的问题. 二.Search Insert ...
- 乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array
乘风破浪:LeetCode真题_034_Find First and Last Position of Element in Sorted Array 一.前言 这次我们还是要改造二分搜索,但是想法却 ...
随机推荐
- GoogLeNetv2 论文研读笔记
Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift 原文链接 摘要 ...
- [转]Reporting Services 中的身份验证类型
本文转自:https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/cc281310%28v%3dsql.100%2 ...
- C# 数组深拷贝
数组深拷贝,即完全复制出一份新的数组,两个数组内容完全相同. 一般有四种方法: 1. 循环遍历复制 2. 数组的成员方法:CopyTo CopyTo方法用作将源数组全部拷贝到目标数组中,可以指定目标数 ...
- winform窗体 控件【公共控件】
Button 按钮 布局 AutoSize 自动匹配尺寸 Location 确定控件位置,相对左上角坐标 Margin 控件与控件之间的距离 Size ...
- Java Switch支持的类型问题
常见支持类型为int,byte,short,char及枚举类型.以上是JDK1.6以前的版本.JDK1.7时,又增加了String. 参考资料:1.java switch支持的数据类型 2.java中 ...
- 一道很好的mysql面试练习题,having综合应用
写一条SQL语句,求出2门以及2门以上不及格的科目平均分 >要出现2门以及2门以上的学科不及格 >计算该考生所有学科的平均分,不单是,不及格的那几门 #创建表: create table ...
- pycharm 光标快速移动到括号外或行尾
coupon = models.ForeignKey("Coupon", on_delete=models.CASCADE) pycharm中编写程序, 需要将光标快速移动到“” ...
- 从零开始学习html(十一)CSS盒模型——下
六.盒模型--边框(一) <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type& ...
- html基础标签下
1.1 单标签 ◆注释标签 ctrl+/ ◆水平线标签 <hr> ◆换行标签 <br> 1.2 双标签 ◆段落标签 <p></p> ◆ ...
- innerHTML在ie9有部分无法添加
在高版本的浏览器,innerHTML就如正常时候,里面可以套任何字符串,但是在ie9下,innerHTML不能是table ,tr td等标签字符串,解决方法如下: 在table添加一个tr var ...