9.20 哔哩哔哩Java开发工程师笔试
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ public ListNode longestList (ListNode head) { // write code here if (head == null) { return null; } boolean isEvenNumber = false; if (head.val % 2 == 0) { isEvenNumber = true; } int count = 0; ListNode tmp = head; if (!isEvenNumber) { count+= 1; } while (tmp.next != null) { if (count % 2 == 0) { if (tmp.next.val % 2 == 0) { tmp.next = tmp.next.next; continue; } } else { if (tmp.next.val % 2 != 0) { tmp.next = tmp.next.next; continue; } } count++; tmp = tmp.next; } return head; } }
第二题
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class Main { static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int n = Integer.parseInt(buf.readLine()); int[] nums = new int[n]; String[] str = buf.readLine().split(" "); for (int i = 0; i < n; i++) { nums[i] = Integer.parseInt(str[i]); } char[] ch = buf.readLine().toCharArray(); int countB = 0, countR = 0; for (int i = 0; i < ch.length; i++) { if (ch[i] == 'B') { countB++; } else { countR++; } } long[] numb = new long[countB]; long[] numR = new long[countR]; int idxB = 0, idxR = 0; for (int i = 0; i < ch.length; i++) { if (ch[i] == 'B') { numb[idxB++] = nums[i]; } else { numR[idxR++] = nums[i]; } } Arrays.sort(numb); Arrays.sort(numR); long[] res = new long[4]; res[0] = numb[countB - 1] * numR[countR -1]; res[1] = numb[0] * numR[0]; res[2] = numb[countB - 1] * numR[0]; res[3] = numR[countR - 1] * numb[0]; Arrays.sort(res); System.out.println(res[3]); } }