LeetCode: First Missing Positive 解题报告
Q:
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.
寻找数组中第一个未出现的正整数,题目本身比较常见,关键在于题目要求只能使用常数额外空间。
A:
转自Annie大神的解释
http://www.cnblogs.com/AnnieKim/archive/2013/04/21/3034631.html:
虽然不能再另外开辟非常数级的额外空间,但是可以在输入数组上就地进行swap操作。
思路:交换数组元素,使得数组中第i位存放数值(i+1)。最后遍历数组,寻找第一个不符合此要求的元素,返回其下标。整个过程需要遍历两次数组,复杂度为O(n)。
下图以题目中给出的第二个例子为例,讲解操作过程。
主页君的代码:
注意的地儿是:
1. 判断是否swap时要判目标值是不是相同(会有重复情况),否则会导致死循环。
2. 如果没找到,返回数组下标+1
1 package Algorithms.array; 2 3 public class FirstMissingPositive { 4 public static void main(String[] strs) { 5 int[] in = {1,2,0}; 6 //int[] in = {3,4,-1,1}; 7 System.out.println(firstMissingPositive(in)); 8 } 9 10 public static int firstMissingPositive(int[] A) { 11 if (A == null) { 12 return 0; 13 } 14 15 int len = A.length; 16 for (int i = 0; i < len; i++) { 17 // 1. The number should be in the range. 18 // 2. The number should be positive. 19 // 注意:和将要交换的值不可以相同,否则会死循环 20 while (A[i] <= len && A[i] > 0 && A[A[i] - 1] != A[i]) { 21 swap(A, i, A[i] - 1); 22 } 23 } 24 25 for (int i = 0; i < len; i++) { 26 if (A[i] != i + 1) { 27 return i + 1; 28 } 29 } 30 31 return len + 1; 32 } 33 34 public static void swap(int[] A, int i, int j) { 35 int tmp = A[i]; 36 A[i] = A[j]; 37 A[j] = tmp; 38 } 39 }
View Code
Git代码链接
LeetCode: First Missing Positive 解题报告相关推荐
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- LeetCode First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [LeetCode] First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- LeetCode Maximum Product Subarray 解题报告
LeetCode 新题又更新了.求:最大子数组乘积. https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子 ...
- 【LeetCode】77. Combinations 解题报告(Python C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [Leetcode] 625. Minimum Factorization 解题报告
题目: Given a positive integer a, find the smallest positive integer b whose multiplication of each di ...
- [leetcode] 28. Implement strStr() 解题报告
题目链接:https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the fi ...
- LeetCode 488 Zuma Game 解题报告
原文链接: http://hankerzheng.com/blog/Leetcode-Zuma-Game- Problem Description LeetCode 488 Zuma Game Thi ...
- 【LeetCode】Palindrome Partitioning 解题报告
[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
最新文章
- rust怎么传送坐标_梦幻西游电脑版:金银锦盒修改后资金怎么攥?仅需知道六个小技巧!...
- 1119 Pre- and Post-order Traversals (30 分)【难度: 难 / 知识点: 树的构建】
- 通过History Trends Unlimited通过统计笔记本Edge浏览器Top10网页历史访问量(截止至2021.11.23)
- Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流
- jq-实战之表格筛选
- linux如何查看实时优先级,Linux进程优先级系统——设置实时进程优先级
- yii2 html form,YII2中ajax通过post提交form表单数据报400错误的解决方法
- java mysql模糊查询_java实现的连接数据库及模糊查询功能示例
- 什么是web前端开发和后端开发?
- SE3可能是苹果最失败的手机,销量不达预期致上市半月降价促销
- Sketch for Mac(矢量绘图工具)
- 开源建站系统的开源组件风险
- activity has leaked window
- Windows + VS Code搭建 Go 开发环境
- AMAX 深度学习服务器重装系统
- 通用技术和信息技术合格考知识点_【精品课】高中信息技术学考+高中通用技术学考-总复习(2019-2020-全考点60小时精讲)...
- 大气数据计算机系统的作用,《大气数据计算机系统》肖建德编.pdf
- 怎么在yocto里面添加自己的代码模块
- 惠普暗影精灵u盘启动linux,Win10+CentOS7 双系统 U盘安装
- IM软件应用及市场分析
热门文章
- 解决UnicodeEncodeError。python的docker镜像增加locale 中文支持
- shell的算术运算
- 修改linux下默认的python版本
- Unity 2D 跑酷道路动起来
- Handler: Service中使用Toast
- SpringBoot文件上传下载
- Needle in a haystack: efficient storage of billions of photos 【转】
- .NET下一种简单的调试诊断方法
- 2-11 支付宝集福卡_实现分析
- 12月19日绝地求生服务器维护公告,绝地求生12月19日更新到几点 绝地求生正式服更新维护公告...