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] ...
随机推荐
- 转:oracle物化视图学习笔记
最近学习了一下物化视图,正好经理不在,把学习结果贴出来供大家一起研究一下吧. 先看一下物化视图的大概含义吧,感觉baidu的定义还不错 物化视图,它是用于预先计算并保存表连接或聚集等耗时较多的操作的结 ...
- 笔记-mysql 导出查询结果
语法: The SELECT ... INTO OUTFILE 'file_name' [options] form of SELECT writes the selected rows to a f ...
- iOS 手机截屏
百度地图自带截图功能,可以截取路线列表,保存到本地.可是对比发现截下来的图片并不是app中看到的那样,截图中头部加入了搜索的起点和终点,每段路程的详细站点都已展开,而且图片会根据路线的长短自动判断图片 ...
- CSS3鼠标悬停8种动画特效
在线演示 本地下载
- Android震动vibrator(马达)--系统到驱动的流程【转】
本文转载自:https://blog.csdn.net/tianshiyalin/article/details/17136723 一.前言 本人刚学习安卓驱动开发,水平不能说菜,是根本没有水平,在这 ...
- 《Maven实战》第5章 坐标和依赖
5.1 Maven坐标——项目唯一标识 groupId(必须定义):定义Mavan项目隶属的实际项目,如SpringFramework,一个实际项目可包含多个Maven项目 artifactId(必须 ...
- (补充一)CountDownLatch
引言: 在学习单例模式时候,用到了锁synchronized的概念,在多线程中又用到了CountDownLatch的概念 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待, ...
- tp添加分页
//分页开始 $count=M('article')->where($condition)->count(); $p = intval($p) > 0 ? $p : 1; $page ...
- LVS+Keepalived+Tomcat实现高可用性及均衡负载
1.Keepalived简介 Keepalived是Linux下一个轻量级别的高可用解决方案.Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考 ...
- 解决Spark用Maven编译时报Exception in thread "main" java.lang.OutOfMemoryError: PermGen space异常
异常截图: 解决方法: export MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"