[亚麻社招OA]Reorder Log Files(背景:会员非会员排序新老版本文件排序)

it2025-11-28  9

Amazon is planning to release a new order prioritization algorithm to optimize fulfilling Prime deliveries on time. All orders (Prime or otherwise) are given an alphanumeric ID code. However, Prime orders are given additional metadata that consists of a space delimited list of lowercase English letters, whereas non-Prime orders are given metadata that consists only of a space delimited string of positive integers. Each order is therefore defined as their alphanumeric id code, followed by a space, followed by their space delimited metadata.

You have been tasked with sorting a list of all orders in the order queue to assist in prioritization of fulfillment. They should be sorted according to the following order:

1. The Prime orders should be returned first, sorted by lexicographic sort of the alphabetic metadata.

2. Only in the case of ties, the identifier should be used as a backup sort.

3. The remaining non-Prime orders must all come after, in the original order they were given in the input.

Write a function or method to sort the orders according to this system.

Input

The input to the function/method consists of two arguments:

numOrders, an integer representing the number of orders,

orderList, a list of strings representing all of the orders.

Output

Return a list of strings representing the correctly prioritized orders.

Note

The order identifier consists of only lower case English character and numbers.

 

题意:

亚麻的每个订单第一段为认证号,后面接纯字母代表prime order; 后面接纯数字代表non-prime order。要求给prime order 按照字典顺序排在前面,non-prime order按照其原始顺序加到队尾。

 

Example:

Input: {"a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"} Output: {"g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"}

 

思路:重写Comparator

完全类似https://www.cnblogs.com/liuliu5151/p/11026126.html 的解法

 

代码

1 class Solution { 2 public String[] reorderLogFiles(String[] logs) { 3 Comparator<String> myComp = new Comparator<String>() { 4 @Override 5 public int compare(String s1, String s2) { 6 String[] split1 = s1.split(" ", 2); 7 String[] split2 = s2.split(" ", 2); 8 boolean isDigit1 = Character.isDigit(split1[1].charAt(0)); 9 boolean isDigit2 = Character.isDigit(split2[1].charAt(0)); 10 if(!isDigit1 && !isDigit2) { 11 int comp = split1[1].compareTo(split2[1]); 12 if(comp != 0) 13 return comp; 14 return split1[0].compareTo(split2[0]); 15 } 16 return isDigit1 ? (isDigit2 ? 0 : 1) : -1; 17 } 18 }; 19 Arrays.sort(logs, myComp); 20 return logs; 21 } 22 }

 

转载于:https://www.cnblogs.com/liuliu5151/p/11026129.html

最新回复(0)