Java实现LeetCode_0041_FirstMissingPositive
package javaLeetCode.hard;
import java.util.Arrays;
public class FirstMissingPositive_41 {
public static void main(String[] args) {
int []nums = {0,2,2,1,1};
System.out.println(firstMissingPositive(nums));
}//end main()
/**
*
* */
/*
* Test Data:
* 1. {3,4,-1,1}--2
* 2. {7,8,9,11,12}--1
* 3. {1,2,0}--3
* 4. {0}--1
* 5. {1000,-1}--1
* 6. {0,2,2,1,1}--3
* */
public static int firstMissingPositive(int[] nums) {
if (nums == null) {
return -1;
} // end if
int firstPos = -1;// First positive position;
int misPos = -1;// Missing positive;
Arrays.sort(nums);
// Find the position of the first positive number.
if (nums.length == 0 || nums[0] > 1 || nums[nums.length - 1] < 0) {
misPos = 1;
} else {
for (int i = 0; i < nums.length; i++) {
if (nums[i] >= 0) {
firstPos = i;
if (nums[firstPos] > 1) {
return 1;
} else {
break;
} // end if
} else {
continue;
} // end if
} // end for
//
for (; firstPos < nums.length - 1; firstPos++) {
if (nums[firstPos + 1] - nums[firstPos] > 1) {
misPos = nums[firstPos] + 1;
break;
} else {
continue;
} // end if
} // end for
// If the firstPos equals last index of the array of "nums".
if (firstPos == nums.length - 1) {
misPos = nums[nums.length - 1] + 1;
} // end if
} // end if
return misPos;
}// end firstMissingPositive()
}//end FirstMissingPositive_41
Java实现LeetCode_0041_FirstMissingPositive的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- [hdu5348]图上找环,删环
http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给一个无向图,现在要将其变成有向图,使得每一个顶点的|出度-入度|<=1 思路:分为两步,(1 ...
- FZU2105 线段树 (按位操作)
题目: Given N integers A={A[0],A[1],...,A[N-1]}. Here we have some operations: (元素和操作元素 < 16) Opera ...
- python --设置Python3 设置为机器默认
echo alias python=python3 >> ~/.bashrc source ~/.bashrc 反之,如果仍然需要Python2.7 ,则改成2.7就行. update : ...
- Unity3D UGUI Image与父级保持比例缩放
using UnityEngine; using System.Collections; using UnityEngine.UI; public class X_RectAutoSize : Mon ...
- Form action 方法上传文件
<form method="post" id="form1" runat="server" enctype="multipa ...
- easyui及读取xml
本地测试地址例如http://localhost:6541/TreeExam/AuthorityTree TreeExam 是TreeExamController AuthorityTree是Tree ...
- Netty框架问题记录1--多线程下批量发送消息导致消息被覆盖
业务背景 项目是基于Netty实现的实时课堂项目,课堂中老师需要对试卷进行讲解,则老师向服务器发送一个打开试卷信息的请求,服务器获取试卷信息,将试卷信息发送给所有的客户端(学生和老师). 发送给学生的 ...
- CF915D Almost Acyclic Graph
题目链接:http://codeforces.com/contest/915/problem/D 题目大意: 给出一个\(n\)个结点\(m\)条边的有向图(无自环.无重边,2 ≤ n ≤ 500, ...
- 自定义reaml创建使用实现认证
注意清空shiro.ini 创建User对象 package cn.zys.Bean; public class User { private Integer id; private String u ...
- thymeleaf将对象ModelList数据抛到HTML页面