作业帮:最长连续序列(头部插入)
题目描述
给定一个未排序的整数数组,找出最长连续序列的长度。 要求算法的时间复杂度为 O(n)。 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。 来源:力扣(LeetCode) 128 链接:https://leetcode-cn.com/problems/longest-consecutive-sequence 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码1:
import java.util.HashMap; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] vals= str.substring(1, str.length()-1).split(","); long[] num = new long[vals.length]; for (int i = 0; i < num.length; i++) { num[i] = Long.valueOf(vals[i].trim()); } System.out.println(getLongestLegth(num)); } public static long getLongestLegth(long[] num){ long max = 0; HashMap<Long,Long> map = new HashMap<Long,Long>(); for (int i = 0; i < num.length; i++) { if(! map.containsKey(num[i])){ long left = map.get(num[i]-1)==null? 0: map.get(num[i]-1);//找到当前数的左边连续长度left long right = map.get(num[i]+1)==null? 0: map.get(num[i]+1);//找到当前述的右边连续长度right long current = 1 + left + right;//连续最大长度 if(current > max){ max = current;//更新最大长度 } map.put(num[i], current); map.put(num[i]-left, current);//更新左边界最大长度 map.put(num[i]+right, current);//更新右边界最大长度 } } return max; } }
代码2:
import java.util.HashSet; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] vals= str.substring(1, str.length()-1).split(","); int[] num = new int[vals.length]; for (int i = 0; i < num.length; i++) { num[i] = Integer.valueOf(vals[i].trim()); } System.out.println(getIntegerestLegth(num)); } public static int getIntegerestLegth(int[] num){ int max = 0; if(num.length<2){ return num.length; } HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < num.length; i++) { set.add(num[i]); } for (int i = 0; i < num.length; i++) { if(set.remove(num[i])){ int currentLongest = 1; int current = num[i]; while(set.remove(current-1)){ current --; } currentLongest += (num[i]-current); current = num[i]; while(set.remove(current+1)){ current ++; } currentLongest += (current-num[i]); max = Math.max(max, currentLongest); } } return max; } }
代码3
import java.util.Arrays; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] vals= str.substring(1, str.length()-1).split(","); int[] num = new int[vals.length]; for (int i = 0; i < num.length; i++) { num[i] = Integer.valueOf(vals[i].trim()); } System.out.println(getIntegerestLegth(num)); } public static int getIntegerestLegth(int[] num){ int max = 0; if(num.length<2){ return num.length; } Arrays.sort(num); PriorityQueue<Integer> maxQueue = new PriorityQueue<Integer>((a,b)->b-a); int currentLongest = 1; for (int i = 0; i < num.length -1; i++) { if(num[i+1] != num[i]){ if(num[i+1] - num[i] ==1){ currentLongest++; }else{ currentLongest = 1; } } maxQueue.offer(currentLongest); } return maxQueue.peek(); } }