- 浏览: 173222 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
用到了桶排序,我们让第i个位置放值为i + 1的元素。当nums[i]不等于i + 1时,就让nums[nums[i] - 1]] = nums[i], 这样nums[nums[i] - 1]的位置肯定符合nums[i] = i + 1。然后在遍历一遍数组,如果遇到nums[i] 不等于 i + 1时就返回i+ 1,这个数字就是第一个missing的正数。代码如下:
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.
用到了桶排序,我们让第i个位置放值为i + 1的元素。当nums[i]不等于i + 1时,就让nums[nums[i] - 1]] = nums[i], 这样nums[nums[i] - 1]的位置肯定符合nums[i] = i + 1。然后在遍历一遍数组,如果遇到nums[i] 不等于 i + 1时就返回i+ 1,这个数字就是第一个missing的正数。代码如下:
public class Solution { public int firstMissingPositive(int[] nums) { if(nums == null) return 0; for(int i = 0; i < nums.length; i++) { while(nums[i] - 1 != i) { if(nums[i] <= 0 || nums[i] >= nums.length || nums[nums[i] - 1] == nums[i]) break; int tem = nums[nums[i] - 1]; nums[nums[i] - 1] = nums[i]; nums[i] = tem; } } for(int i = 0; i < nums.length; i++) { if(nums[i] != i + 1) return i + 1; } return nums.length + 1; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 225Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 223You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 426The n-queens puzzle is the prob ... -
Spiral Matrix
2016-03-04 03:39 515Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 367All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 553Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 599Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 762Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 732You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 347There are n bulbs that are init ...
相关推荐
First Missing Positive 计数排序 H-Index 基数排序 Maximum Gap 其他 Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II ...
First Missing Positive 广度优先搜索 773. Sliding Puzzle 864. Shortest Path to Get All Keys 深度优先搜索 996. Number of Squareful Arrays 拓扑排序 269. Alien Dictionary 单调栈 42. Trapping Rain Water 85...
268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](./C++/missing-number.cpp) [Python](./Python/missing-number.py) | _O(n)_ | _O(1)_ | Medium | LintCode || 318| [Maximum ...
├── First Missing Positive │ ├── Readme.md │ └── solution.js ├── Trapping Rain Water │ ├── Readme.md │ └── solution.js ├── Wildcard Matching │ ├── Readme.md │ └── ...
leetcode打不开Leetcode Note Tips Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. ...First Missing Positive
leetcode 71 Python用 ...Missing Positive (HARD) leetcode 42. 收集雨水 (HARD) leetcode 44. 通配符匹配 (HARD) leetcode 45. Jump Game II (HARD) Leetcode 51. N-Queens (HARD) Leetcode 52. N-
题:**First Missing Positive 给定一个未排序的整数数组,找到第一个缺失的正整数。 例如,给定 [1,2,0] 返回 3,而 [3,4,-1,1] 返回 2。 您的算法应该在 O(n) 时间内运行并使用恒定空间。 **第 0003 题:**String ...
Methods of fuzzy rule extraction based on rough set theory are rarely ... The data completeness of missing attribute values is first presented.Positive and converse approximations in interval-valued fuzz
收集面试中提出的一些重要问题。 数据结构算法面试问题面试中提出的一些重要问题的集合。 数组...missing-positive / https://leetcode.com/problems/container-with-most-water/ htt
leetcode 2 code_interview leetcode和lintcode的一些题目的解法 是剑指offer 是面试中遇到的有意思的题目总结,比如说BAT,intel,NVIDIA等公司面试的题目记录 ...41-first-missing-positive 01-Two-Sum 求解第K
股票买卖最佳时机leetcode Java项目 这是 ...First_Missing_Positive Generate_All_Parenthesis_2 实现_StrStr Largest_Distance_Between_Nodes_Of_A_Tree Largest_Rectangle_In_Histogram Least_C
41_First_Missing_Positive 299_Bulls_and_Cows 134_Gas_Station 118_Pascal's_Triangle_I 119_Pascal's_Triangle_II 169_Majority_Element 229_Majority_Element_II 274_H_索引 275_H_Index_II 217_Contain_...
缺失的第一个整数](./Array/first-missing-positive.md) [0042 接雨水](./Array/trapping-rain-water.md) [0048 旋转图像](./Array/rotate-image.md) Heap 堆 [0023 合并K个排序链表](./Heap/merge-k-sorted-lists....
* fixed: positive edit began a bit too early * fixed: two ID3 tags after each other made eac3to fail detecting the format * fixed: some VOB files were not detected properly v2.87 * fixed: negative ...
13)..Added: "User" and "Session" columns to processes list, processes list is also sorted by session first 14)..Added: Support for showing current user processes only 15)..Added: Expanding environment...
FASMARM v1.42 This package is an ARM assembler add-on for FASM. FASMARM currently supports the full range of instructions for 32-bit and 64-bit ARM processors and coprocessors up to and including v8...
The ideal lecturer is one who has an interesting teaching style, a diverse academic background, and a good reputation among students. <br> There are both positive and negative aspects to allowing ...
sdk LCS/Telegraphics Wintab* Interface Specification 1.1: 16- and 32-bit API Reference ... The coordinate system will be right-handed, that is, the positive x axis points from left to right, and the ...