tableau OA Aloha8


import
java.util.*; public class MyClass { public static void main(String args[]) { String inputString = "1:4 , 5:10,11:20 "; //String inputString = "1:2,5:10"; String[] inputStringArray = inputString.split("\\s*,\\s*"); int[][] intervals = new int[inputStringArray.length][2]; for (int i = 0; i < intervals.length; i++) { String s = inputStringArray[i]; intervals[i][0] = Integer.valueOf(s.split(":")[0].trim()); System.out.println("intervals[i][0] = " + intervals[i][0]); intervals[i][1] = Integer.valueOf(s.split(":")[1].trim()); System.out.println("intervals[i][1] = " + intervals[i][1]); } int[][] res = merge(intervals); StringBuilder sb = new StringBuilder(); for (int i=0;i <res.length; i++) { sb.append(String.valueOf(res[i][0])); sb.append(":"); sb.append(String.valueOf(res[i][1])); sb.append(","); } //最后输出也要是string String str = sb.toString(); System.out.println(str.substring(0, str.length() - 1)); } public static int[][] merge(int[][] intervals) { if (intervals.length <= 1) return intervals; // Sort by ascending starting point Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0])); List<int[]> result = new ArrayList<>(); int[] newInterval = intervals[0]; result.add(newInterval); //距离是1也要改 for (int[] interval : intervals) { if (interval[0] <= newInterval[1] + 1) // Overlapping intervals, move the end if needed newInterval[1] = Math.max(newInterval[1], interval[1]); else { // Disjoint intervals, add the new interval to the list newInterval = interval; result.add(newInterval); } } return result.toArray(new int[result.size()][]); } }

NEXT是一个单词 NEXTLINE是一整行
system.exit(0) 不打印
BigInteger要用compareTo,add(new BigInteger("1"))

 

posted @ 2021-10-02 10:01  苗妙苗  阅读(40)  评论(0编辑  收藏  举报