总结---美团笔试4.19---技术岗
一、
- 思路:将数值与索引绑定,遍历相邻元素即可
- 注意:可以用Arrays.sort或者TreeMap实现
- 代码
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int i = 0; i < t; i++) { int n = in.nextInt(); int[] val = new int[n]; for (int j = 0; j < n; j++) { val[j] = in.nextInt(); } Integer[] idx = new Integer[n]; for (int j = 0; j < n; j++) { idx[j] = j; } Arrays.sort(idx, Comparator.comparingInt(k -> val[k])); int pos = 0, neg = 0; for (int j = 0; j < n - 1; j++) { if (idx[j + 1] > idx[j]) { pos++; } else { neg++; } } System.out.println(pos + " " + neg); } } }
暑期笔试总结 文章被收录于专栏
记录一些公司的暑期笔试感悟