搜狐笔试编程题,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();
    }
全部评论
好***,第一道最少跳那个死活A不过,穷举都不行
点赞 回复 分享
发布于 2016-09-21 17:10
没有一道完全AC的渣渣路过~。・゚(゚⊃ω⊂゚)゚・。エーンエーン
点赞 回复 分享
发布于 2016-09-21 17:12
项链那道题也是A不过,40%,不知道什么原因,感觉不难呀。。
点赞 回复 分享
发布于 2016-09-21 17:24
项链那个60%然后TLE。其余两道a了。。
点赞 回复 分享
发布于 2016-09-21 17:27
能讲讲袋鼠过河的思路吗?代码没注释没看明白
点赞 回复 分享
发布于 2016-09-22 01:48

相关推荐

美团 后端开发 总包n(15%是股票)
点赞 评论 收藏
分享
感性的干饭人在线蹲牛友:🐮 应该是在嘉定这边叭,禾赛大楼挺好看的
点赞 评论 收藏
分享
11-18 09:44
Java
小白也想要offer:简历别放洋屁,搞不还还放错了,当然你投外企除外,以上纯属个人观点
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务