搜狐笔试编程题,AC2道第三道80%
第三道不晓得啥情况
1
public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while (in.hasNext()) { int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } System.out.println(jumpCount(nums)); } } public static int jumpCount(int[] nums) { if (nums == null || nums.length == 0) return -1; int jumpCount = 0; int lastJump = 0; int jump = 0; for (int position = 0; position <= jump && position < nums.length; position++) { if(position == jump && position == nums.length - 1){ jumpCount++; } if (position > lastJump) { jumpCount++; lastJump = jump; } jump = Math.max(jump, nums[position] + position); } if (jump < nums.length - 1) return -1; return jumpCount; }
2
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { System.out.println(removeKdigits(in.next(), in.nextInt())); } } public static String removeKdigits(String numberString, int n) { if (numberString.length() == 0 || numberString.length() <= n) return "0"; Stack<Integer> stack = new Stack<Integer>(); for (int i = 0; i < numberString.length(); i++) { int currentInt = numberString.charAt(i) - '0'; while (!stack.isEmpty() && currentInt > stack.peek() && numberString.length() - i - 1 >= (numberString.length() - n) - stack.size()) { stack.pop(); } if (stack.size() < numberString.length() - n) stack.push(currentInt); } return build(stack); } public static String build(Stack<Integer> stack) { StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) sb.insert(0, stack.pop()); while (sb.length() > 0 && sb.charAt(0) == '0') sb.deleteCharAt(0); if (sb.length() == 0) return "0"; return sb.toString(); }
3
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { System.out.println(minLength(in.next())); } } public static int minLength(String t) { String targetLoop = t + t; String cutTarget = "ABCDE"; if (cutTarget.length() > targetLoop.length()) return 0; HashMap<Character, Integer> target = new HashMap<Character, Integer>(); String cutResult = ""; for (int i = 0; i < cutTarget.length(); i++) { char c = cutTarget.charAt(i); if (target.containsKey(c)) { target.put(c, target.get(c) + 1); } else { target.put(c, 1); } } HashMap<Character, Integer> map = new HashMap<Character, Integer>(); int l = 0; int minLength = targetLoop.length() + 1; int count = 0; for (int i = 0; i < targetLoop.length(); i++) { char c = targetLoop.charAt(i); if (target.containsKey(c)) { if (map.containsKey(c)) { if (map.get(c) < target.get(c)) { count++; } map.put(c, map.get(c) + 1); } else { map.put(c, 1); count++; } } if (count == cutTarget.length()) { char sc = targetLoop.charAt(l); while (!map.containsKey(sc) || map.get(sc) > target.get(sc)) { if (map.containsKey(sc) && map.get(sc) > target.get(sc)) map.put(sc, map.get(sc) - 1); l++; sc = targetLoop.charAt(l); } if (i - l + 1 < minLength) { cutResult = targetLoop.substring(l, i + 1); minLength = i - l + 1; } } } return t.length() - cutResult.length(); }