快手笔试技术类笔试A卷前俩题
//暴力第一题 //学生队列距离问题 AC 100%
public int[] DistanceToHigher(int[] height) { // write code here // [175, 173, 174, 163, 182, 177] //[0,1,2,1,0,1] int[] ans = new int[height.length]; //int maxIndex = ans[0]; for (int i = height.length - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { if (height[j] > height[i]){ ans[i] = i - j; break; } } } // for (Integer i : ans) { // System.out.print(i + " "); // } return ans; }
第二题
AC 100%要求O(N) 我没做对
public void PrintIndex() { Scanner scanner = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int counter = 0; while (scanner.hasNextInt()) { int count = 0; int temp = scanner.nextInt(); list.add(temp); if (list.size() > 1) { for (int i = 0; i < list.size(); i++) { if (list.get(i) > temp) { count++; } } if (count == 1) { counter++; System.out.print(list.size() - 1 + " "); } } } try { } catch (Exception e) { e.printStackTrace(); } finally { if (counter == 0) { System.out.print(-1); } } }
第二题开始没看到O(N)做了个O(n^2)的 还比较恶心 题目说输入输出字符串。。。 我还去处理字符串去了
public void PrintIndex() { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] strings = s.split(" "); int[] ans = new int[strings.length]; for (int i = 0; i < strings.length; i++) { ans[i] = Integer.parseInt(strings[i]); } int counter = 0; for (int i = 1; i < ans.length; i++) { int count = 0; for (int j = 0; j < i; j++) { if (ans[j] > ans[i]) { count++; } } if (count == 1) { System.out.println(i); counter++; } } if (counter == 0) { System.out.println(-1); } }