1. 第一题:
思路:有序的,直接遍历比较就可以了
import java.util.*;
// AC
public class Main1 {
public static class ListNode
{
int val;
ListNode next;
ListNode(int x)
{
val = x;
}
}
public static ListNode createLinkedList(int arr[])
{
if(arr.length == 0)
{
return null;
}
ListNode head = new ListNode(arr[0]);
ListNode current = head;
for(int i = 1; i < arr.length; i++)
{
current.next = new ListNode(arr[i]);
current = current.next;
}
return head;
}
public static void getPublicNode(ListNode a, ListNode b)
{
while(a != null && b != null)
{
if(a.val < b.val)
{
b = b.next;
}
else if(a.val > b.val)
{
a = a.next;
}
else
{
System.out.print(a.val + " ");
a = a.next;
b = b.next;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a[] = new int[n];
for(int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
int m = sc.nextInt();
int b[] = new int[m];
for(int i = 0; i < m; i++)
{
b[i] = sc.nextInt();
}
ListNode list1 = createLinkedList(a);
ListNode list2 = createLinkedList(b);
getPublicNode(list1, list2);
}
}
// 测试1:
// 输入:
// 6
// 6 5 4 3 2 1
// 5
// 6 5 3 2 1
//
// 输出:
// 6 5 3 2 1
2. 第三题:
思路:字符串次数前k个(前k大),后k个(前k小),哈希表排序
import java.util.*;
// AC
public class Main3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
HashMap<String, Integer> map = new HashMap<>();
String temp;
for (int i = 0; i < N; ++i) {
temp = sc.next();
map.put(temp, map.getOrDefault(temp, 0) + 1);
}
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> !o1.getValue().equals(o2.getValue()) ? o2.getValue() - o1.getValue()
: o1.getKey().compareTo(o2.getKey()));
for (int i = 0; i < K; ++i) {
System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
}
list = new ArrayList<>(map.entrySet());
list.sort((o1, o2) -> !o1.getValue().equals(o2.getValue()) ? o1.getValue() - o2.getValue()
: o1.getKey().compareTo(o2.getKey()));
for (int i = 0; i < K; ++i) {
System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
}
}
}
// 测试1:
// 输入:
// 4 2
// 1 2 3 4
//
// 输出:
// 1 1
// 2 1
// 1 1
// 2 1
// 测试2:
// 输入:
// 4 2
// 1 2 1 4
//
// 输出:
// 1 2
// 2 1
// 2 1
// 4 1
3. 第四题:
思路:
找规律,原中位数有俩 mid1(a) 和 mid2(b)。
删除的数字 < mid2,则中位数为 mid2。
否则,为mid1
import java.util.*;
// AC
public class Main4 {
// 方法1: 通过 55%
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();
// int a[] = new int[n];
// int b[] = new int[n];
// for(int i = 0; i < n; i++)
// {
// a[i] = sc.nextInt();
// b[i] = a[i];
// }
// Arrays.sort(b);
// int count = 0;
// int temp[] = new int[b.length - 1];
// for(int i = 0; i < a.length; i++)
// {
// for(int j = 0; j < b.length; j++)
// {
// if(b[j] != a[i])
// {
// temp[count] = b[j];
// count++;
// }
// }
// System.out.println(temp[(count - 1) / 2 ]);
// count = 0;
// }
// }
// 方法2: AC
// 思路:找规律,原中位数有俩 mid1(a) 和 mid2(b)。
// 删除的数字 < mid2,则中位数为 mid2。
// 否则,为mid1
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
sc.close();
int[] arr1 = arr.clone();
Arrays.sort(arr);
// System.out.println(Arrays.toString(arr));
// System.out.println(Arrays.toString(arr1));
int a = arr[n / 2 - 1];
int b = arr[n / 2];
for (int i = 0; i < n; i++) {
if (arr1[i] < b) System.out.println(b);
else System.out.println(a);
}
}
}
// 测试1:
// 输入:
// 6
// 1 2 3 4 5 6
//
// 输出:
// 4 4 4 3 3 3
// 测试2:
// 输入:
// 5
// 2 6 10 13 15
//
// 输出:
// 10 10 10 6 6