给定无序数组arr,返回其中最长的连续序列的长度(要求值连续,位置可以不连续,例如 3,4,5,6为连续的自然数)
数据范围: ,数组中的值满足
要求:空间复杂度 ,时间复杂度
class Solution { public: /** * max increasing subsequence * @param arr int整型vector the array * @return int整型 */ int MLS(vector<int>& arr) { // write code here int max=0; sort(arr.begin(),arr.end()); for(int i=0;i<arr.size()-1;i++) { int n=1; while((arr[i]+1==arr[i+1])||(arr[i]==arr[i+1])) { if(arr[i]==arr[i+1]) { i++; } else { i++; n++; } } if(n>max) max=n; } return max; } };
class Solution { public: /** * max increasing subsequence * @param arr int整型vector the array * @return int整型 */ int MLS(vector<int>& arr) { // write code here sort(arr.begin(), arr.end()); auto it=unique(arr.begin(), arr.end()); arr.erase(it, arr.end()); int len=1,ans=1; for(int i=1;i<arr.size();i++) { if(arr[i]==(arr[i-1]+1)) { len++; } else { if(ans<len) { ans=len; } len=1; } } return max(len,ans); } };
import java.util.*; public class Solution { /** * max increasing subsequence * @param arr int整型一维数组 the array * @return int整型 */ public int MLS (int[] arr) { if(arr == null || arr.length == 0){ return 0; } int len = arr.length; Arrays.sort(arr); int count = 1; int result = 1; for(int i=0;i<len-1;i++){ if(arr[i+1] - arr[i] == 1){ count++; }else if(arr[i+1] - arr[i] == 0){ continue; }else{ count = 1; } result = Math.max(count, result); } return result; } }
function MLS( arr ) { // write code here const newArr = [...new Set(arr)].sort((a, b) => a - b) let max = 0 let point = -1 let pre = Infinity newArr.forEach((item, index) => { if (point === -1) { max = 1 point = 0 } else { if (item === pre + 1) { max = Math.max(max, index - point + 1) } else { point = index } } pre = item }) return max }
//运行时间:343ms 占用内存:117184KB import java.util.*; public class Solution { /** * max increasing subsequence * @param arr int整型一维数组 the array * @return int整型 */ public int MLS (int[] arr) { // write code here int count=0; int ans=0; boolean has[]=new boolean[100000006]; for(int i=0;i<arr.length;i++){has[arr[i]]=true;} for(int i=1;i<=100000;i++){ if(has[i]){count++;} else{ ans=Math.max(ans,count); count=0; } } return Math.max(ans,count); } }
// 换个方向,不连续的时候记录,代码会简单许多 public int MLS (int[] arr) { int ans = 0; Arrays.sort(arr); int l = 0; int repeat = 0; for (int i = 1; i < arr.length; i++) { if (arr[i] == arr[i - 1]) { repeat++; continue; } if (arr[i] != arr[i - 1] + 1) { ans = Math.max(ans, i - l - repeat); l = i; repeat = 0; } } // 最后一个元素 if (l != arr.length - 1) { ans = Math.max(ans, arr.length - l - repeat); } return ans; }
import java.util.*; public class Solution { /** * max increasing subsequence * @param arr int整型一维数组 the array * @return int整型 */ public int MLS (int[] arr) { // write code here // 先对arr排序 Arrays.sort(arr); // <arr[i], {index1,index2...}> // 存在值相同的情况 Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); int len = arr.length; for(int i=0;i<len;i++){ int num = arr[i]; if(map.keySet().contains(num)){ map.get(num).add(i); }else{ List<Integer> list = new ArrayList<Integer>(); list.add(i); map.put(num, list); } } if(len == 0) return 0; int max = 1; // dp[i] 表示以arr[i]为结尾的元素所能表示最长连续子序列长度 int[] dp = new int[len]; // 初始时,每个arr[i]结尾的元素所能表示的最长连续子序列长度为1 Arrays.fill(dp,1); for(int i=1;i<len;i++){ int curValue = arr[i]; if(map.keySet().contains(curValue-1)){ List<Integer> indexList = map.get(curValue-1); for(int index : indexList){ dp[i] = Math.max(dp[index]+1,dp[i]); } max = Math.max(max, dp[i]); } } return max; } }
//手动去重+排序 import java.util.*; public class Solution { /** * max increasing subsequence * @param arr int整型一维数组 the array * @return int整型 */ public int MLS (int[] arr) { // write code here if(arr.length==1) return 1; int len; int res=0; Arrays.sort(arr); int i=1; while(i<arr.length){ len=1; if(arr[i]==arr[i-1]+1){ while(i<arr.length&&arr[i]==arr[i-1]+1){ i++; len++; while(i<arr.length&&arr[i]==arr[i-1]) i++; } } else i++; res=Math.max(res,len); } return res; } }
public class Solution { public int MLS (int[] arr) { if(arr.length == 0) return 0; int n = arr.length; int max = 1; Set<Integer> set = new HashSet<>(); for(int num:arr) set.add(num); //先将数组中的值都存储在set集合中 for(int num:arr){ if(set.contains(num-1)) continue; //如果集合中包含比当前值小1的,则结束本次循环 int start = num; //不存在比当前值小1的,则去寻找以当前值开始的连续子序列 while(set.contains(start+1)){ start++; } //得到较长的子序列 max = Math.max(max,start-num+1); } return max; } }
class Solution { public: int MLS(vector<int>& arr) { set<int> set; int max_ans = 1; for(int num:arr) set.insert(num);//初始化set for(int num:arr){ if(set.count(num-1)) continue;//这一步不能少(否则超时),保证只从最小的数开始查 int cnt = 1; while(set.count(++num)) cnt++; max_ans = max(max_ans,cnt); } return max_ans; } };
#include <unordered_map> class Solution { public: /** * max increasing subsequence * @param arr int整型vector the array * @return int整型 */ int MLS(vector<int>& arr) { // write code here unordered_map<int, int> hash_map; for(int x:arr){ hash_map[x] = 1; } int res = 0; for(int i = 0; i < arr.size(); i++){ int tmp = 1; hash_map.erase(arr[i]); auto l = hash_map.find(arr[i] - 1); auto r = hash_map.find(arr[i] + 1); while(l != hash_map.end() || r != hash_map.end()){ if(l != hash_map.end()){ auto tmp_i = l; l = hash_map.find(l->first - 1); tmp++; hash_map.erase(tmp_i); } if(r != hash_map.end()){ auto tmp_i = r; r = hash_map.find(r->first + 1); tmp++; hash_map.erase(tmp_i); } } res = max(res, tmp); } return res; } };
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * max increasing subsequence * @param arr int整型vector the array * @return int整型 */ int MLS(vector<int>& arr) { sort(arr.begin(), arr.end()); map<int, int> ma; vector<int> a; for (int i = 0; i < arr.size(); i ++ ) { if (!ma[arr[i]]) a.push_back(arr[i]); ma[arr[i]] ++; } int num = 1, ans = 1; for (int i = 0; i < a.size(); i ++ ) { int j = i + 1; if (j > a.size() - 1) break; if (a[i] + 1 == a[j]) { num ++; ans = max(num, ans); } else { num = 1; } } return ans; } };
import java.util.*; public class Solution { public int MLS (int[] arr) { Arrays.sort(arr); int ans = 0; for (int i = 1, c = 1; i < arr.length; i++) { if (arr[i] == arr[i - 1] + 1) { c++; } else if (arr[i] > arr[i - 1]) { c = 1; } ans = Math.max(ans, c); } return ans; } }
public int MLS (int[] arr) { // write code here Arrays.sort(arr); int res = Integer.MIN_VALUE, cnt = 1; for (int i = 0 ; i < arr.length - 1; i++) { if (arr[i + 1] == arr[i] + 1) cnt++; else if (arr[i + 1] != arr[i]) { res = Math.max(res, cnt); cnt = 1; } } res = Math.max(res, cnt); return res; }
public int MLS (int[] arr) { // write code here Arrays.sort(arr); int res=0; for(int i=0;i<arr.length;i++){ int num=1; while(i+1<arr.length && arr[i+1]-arr[i]<=1){ if(arr[i]+1==arr[i+1]){ num++; } i++; } res=Math.max(res,num); } return res; }
import java.util.*; public class Solution { /** * max increasing subsequence * @param arr int整型一维数组 the array * @return int整型 */ //1 2 3 4 100 200 public int MLS (int[] arr) { // write code here Arrays.sort(arr); int max=1; int count=1; for(int i=1;i<arr.length;i++){ if(arr[i]==arr[i-1]){ continue; } if(arr[i]-arr[i-1]==1){ count++; }else{ count=1; } max=Math.max(count,max); } return max; } }