Problem
You have an array of logs. Each log is a space delimited string of words.
For each log, the first word in each log is an alphanumeric identifier. Then, either:
Each word after the identifier will consist only of lowercase letters, or;
Each word after the identifier will consist only of digits.
We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.
Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order.
Return the final order of the logs.
Example 1: 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"]
Note:
0 <= logs.length <= 100
3 <= logs[i].length <= 100
logs[i] is guaranteed to have an identifier, and a word after the identifier.
class Solution { public String[] reorderLogFiles(String[] logs) { Comparatorcomparator = new Comparator () { @Override public int compare(String s1, String s2) { int i1 = s1.indexOf(" "); int i2 = s2.indexOf(" "); char c1 = s1.charAt(i1+1); char c2 = s2.charAt(i2+1); if (c1 <= "9") { return c2 <= "9" ? 0 : 1; } else if (c2 <= "9") { return -1; } else { //all letters int res = s1.substring(i1+1).compareTo(s2.substring(i2+1)); if (res == 0) return s1.substring(0,i1).compareTo(s2.substring(0,i2)); else return res; } } }; Arrays.sort(logs, comparator); return logs; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72989.html
摘要:題目鏈接題目分析給定一個數組,每一個元素是一條日志。剩余部分為全為小寫字母的字符串稱為字符日志或全為數字的字符串稱為數字日志。給定的數組中確定會至少有一個字母。遍歷完成后,對字符日志進行排序。在其后拼接數字日志數組,并返回即可。 D54 937. Reorder Log Files 題目鏈接 937. Reorder Log Files 題目分析 給定一個數組,每一個元素是一條日志。 ...
Problem Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not modify the values in the lists nodes, only nodes itself may be changed. Example 1: Given 1->2->...
摘要:要找到后半部分的起點,就是用快慢指針。不過該題我們不能直接拿到中間,而是要拿到中間的前一個節點,這樣才能把第一個子鏈表的末尾置為空,這里的技巧就是快慢指針循環的條件是。注意因為不能有額外空間,我們最好用迭代的方法反轉鏈表。 Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→...
Problem Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] nums[i-1]) swap(nums, i, i-1); } } } private void swap(int[] nums, int i, int j) { ...
Problem Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive ...
閱讀 3570·2023-04-25 14:20
閱讀 1191·2021-09-10 10:51
閱讀 1152·2019-08-30 15:53
閱讀 458·2019-08-30 15:43
閱讀 2313·2019-08-30 14:13
閱讀 2794·2019-08-30 12:45
閱讀 1204·2019-08-29 16:18
閱讀 1161·2019-08-29 16:12