LeetCode:缺失的第一个正数【41】

题目描述

给定一个未排序的整数数组,找出其中没有出现的最小的正整数。

示例 1:

  输入: [1,2,0]
  输出: 3
示例 2:

  输入: [3,4,-1,1]
  输出: 2
示例 3:

  输入: [7,8,9,11,12]
  输出: 1
说明:

你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。

题目分析

  参照链接:https://leetcode-cn.com/problems/first-missing-positive/solution/tong-pai-xu-python-dai-ma-by-liweiwei1419/

Java题解

class Solution {
public int firstMissingPositive(int[] nums) {
int len = nums.length;
for(int i = 0;i<len;){
int t = nums[i];
if(t>0&&t<=len&&t!=nums[t-1])
swap(nums,i,t-1);
else
i++;
} for (int i = 0; i < len; i++) {
if (i + 1 != nums[i]) {
return i + 1;
}
}
return len + 1;
} //数组元素交换
public void swap(int[] nums,int i,int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}

LeetCode:缺失的第一个正数【41】的更多相关文章

  1. LeetCode缺失的第一个正数

    LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...

  2. Java实现 LeetCode 41 缺失的第一个正数

    41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...

  3. Leetcode 41.缺失的第一个正数

    缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...

  4. LeetCode(41):缺失的第一个正数

    Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...

  5. LeetCode 41. 缺失的第一个正数(First Missing Positive)

    题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...

  6. leetcode 41缺失的第一个正数

    time O(n) space O(1) class Solution { public: int firstMissingPositive(vector<int>& nums) ...

  7. 【LeetCode 41】缺失的第一个正数

    题目链接 [题解] 先明确一点假设给的数字有n个. 那么最后的答案最情况下就是n+1 首先我们先判断一下所给的数组里面有没有1 如果没有直接返回1 否则. 把数组中所有的范围超过n或者小于1的数字全都 ...

  8. leetcode之缺失的第一个正数

    给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0]输出: 3示例 2: 输入: [3,4,-1,1]输出: 2示例 3: 输入: [7,8,9,11,12] ...

  9. [Leetcode] first missing positve 缺失的第一个正数

    Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...

随机推荐

  1. js元素remove

    Element.prototype.remove = function() {        this.parentElement.removeChild(this);    };

  2. crc计算工具推荐

    比较好的crc计算工具,32位64位系统都可以用的.crc的校验方法也很多.推荐使用 下载地址

  3. KM模板 最大权匹配(广搜版) Luogu P1559 运动员最佳匹配问题

    KM板题: #include <bits/stdc++.h> using namespace std; inline void read(int &num) { char ch; ...

  4. shell grep的基本用法

    grep 1.-i 不区分大写小写 2.-n 区分大小写 3.-E 查找多个条件 4.-c 查找到的结果的行数 5.-w 精确查找 6.取反 7.-q 静默输出

  5. js中数组和字符串的方法总结

    一.数组方法简单总结为以下几种 1.原有: 增.删.改.截.拼.复.排.转 2.ES5扩展: 查.遍历 增: 前增 ,,,,]; console.log(arr.unshift(,,[ console ...

  6. [NOI2019]回家路线

    [NOI2019]回家路线 题目大意: 有\(n\)个站点,\(m\)趟车,每趟车在\(p_i\)时从\(x_i\)出发,\(q_i\)时到达\(y_i\). 若小猫共乘坐了\(k\)班列车,依次乘坐 ...

  7. gulp初体验

    项目流程 安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 常用命令简介: node -v 查 ...

  8. es6学习1:let和const

    一:let   类似var 但是所声明的变量,只在let命令所在的代码块内有效. 1) 不存在变量提升 // var 的情况 console.log(foo); // 输出undefined var ...

  9. 【算法编程 C++ Python】根据前序遍历、中序遍历重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  10. 微信小程序之页面导航栏

    效果图: 页面有点丑,作为初次学习,页面可以要求不那么美观,先学会再说.毕竟后面可以优化的很漂亮. 代码实例如下: <view class="section btn-area" ...