LeetCode OJ:First Missing Positive (第一个丢失的正数)
在leetCode上做的第一个难度是hard的题,题目如下:
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.
关键是要实现0(N)的时间复杂度以及常数级别的空间复杂度,先贴上我写的函数,完全不能达到上面的要求,只能实现NlgN的时间复杂度:
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
sort(nums.begin(), nums.end());
int sz = nums.size();
if(sz == ) return ;
int index;
for (index = ; index < sz; index++){
if (nums[index] <= )
continue;
else
break;
}
if (nums[index] != || index == sz) return ; //当没有正数的情况或正数的第一个数不是1的情况
while (index < sz){
if (nums[index + ] != nums[index] && nums[index + ] != nums[index] + ) //两个判断主要是为了防止vector中重复的数字出现。
return nums[index] + ;
index++;
}
return nums[index] + ;
}
};
由于达不到时间以及空间复杂度的要求,实在想不出来,我去看了下别人写的,现在由于vector可能会出现重复的数,我暂时不知带怎样去解决,只有先这样,回头有时间再回来填坑。
LeetCode OJ:First Missing Positive (第一个丢失的正数)的更多相关文章
- [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- leetCode 41.First Missing Positive (第一个丢失的正数) 解题思路和方法
First Missing Positive Given an unsorted integer array, find the first missing positive integer. Fo ...
- [leetcode]41. First Missing Positive第一个未出现的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 041 First Missing Positive 第一个缺失的正数
给一个未排序的数组,找出第一个缺失的正整数.例如,[1,2,0] 返回 3,[3,4,-1,1] 返回 2.你的算法应该在 O(n) 的时间复杂度内完成并且使用常数量的空间.详见:https://le ...
- LeetCode 41 First Missing Positive(找到数组中第一个丢失的正数)
题目链接: https://leetcode.com/problems/first-missing-positive/?tab=Description 给出一个未排序的数组,求出第一个丢失的正数. ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
随机推荐
- Linux学习笔记—vim程序编辑器
vi和vim vim是vi的升级版,支持vi的所有指令 vi的使用 vi分为三种模式:一般模式.编辑模式.命令行模式 一般模式 以vi打开一个文件就直接进入一般模式了,这个模式下可以使用上下左右按键来 ...
- go——字典(二)
字典是一种数据结构,用于存储一系列无序的键值对. 字典是基于键来存储值.字典功能强大的地方是能够基于键快速检索数据. 键就像索引一样,指向与键关联的值. 1.内部实现 字典是一个集合,可以使用类似处理 ...
- xpath(待补充)
from lxml import etree html=""" <div> <ul> <li>1</li> <li ...
- vue-cli中的build.js配置文件详细解析
转载自:https://www.cnblogs.com/ye-hcj/p/7096341.html这是vue-cli脚手架工具的生产环境配置入口 package.json中的"build&q ...
- netty应用
http://www.blogjava.net/yongboy/archive/2013/05/13/399203.html http://shentar.me/tag/netty-2/ 代理 htt ...
- WPF MVVM TreeView 实现 右键选中 右键菜单
1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown=& ...
- 20145120黄玄曦《网络对抗》Web安全基础实践
20145120黄玄曦<网络对抗>Web安全基础实践 回答问题 (1)SQL注入攻击原理,如何防御 SQL注入原理简单地说大概是,通过构造特殊的SQL命令提交表单,让服务器执行构造的恶意S ...
- MD5key.java
代码如下: package com.lekou.utils; public class MD5key { ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; , , , , , , , , ...
- 简单介绍java Enumeration(转)
Enumeration接口 Enumeration接口本身不是一个数据结构.但是,对其他数据结构非常重要. Enumeration接口定义了从一个数据结构得到连续数据的手段.例如,Enumeratio ...
- SpringBoot2.0整合Sharding-Jdbc
maven: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spri ...