fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. String line = sc.nextLine().trim();
  7.  
  8. // 提取 [] 中的内容
  9. line = line.substring(line.indexOf('[') + 1, line.indexOf(']'));
  10.  
  11. if (line.length() == 0) {
  12. System.out.println(0);
  13. return;
  14. }
  15.  
  16. String[] parts = line.split(",");
  17. int[] nums = new int[parts.length];
  18.  
  19. for (int i = 0; i < parts.length; i++) {
  20. nums[i] = Integer.parseInt(parts[i].trim());
  21. }
  22.  
  23. // 核心逻辑
  24. Set<Integer> set = new HashSet<>();
  25. for (int num : nums) set.add(num);
  26.  
  27. int maxLen = 0;
  28.  
  29. for (int num : set) {
  30. if (!set.contains(num - 1)) {
  31. int cur = num;
  32. int len = 1;
  33.  
  34. while (set.contains(cur + 1)) {
  35. cur++;
  36. len++;
  37. }
  38.  
  39. maxLen = Math.max(maxLen, len);
  40. }
  41. }
  42.  
  43. System.out.println(maxLen);
  44. }
  45. }
Success #stdin #stdout 0.12s 56500KB
stdin
nums=[1,3,5,7,9,11]
stdout
1