搜狐笔试编程题,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

相关推荐

11-24 00:11
已编辑
广东工业大学 算法工程师
避雷深圳&nbsp;&nbsp;yidao,试用期&nbsp;6&nbsp;个月。好嘛,试用期还没结束,就直接告诉你尽快找下一家吧,我谢谢您嘞
牛客75408465号:笑死,直属领导和 hr 口径都没统一,各自说了一些离谱的被裁理由,你们能不能认真一点呀,哈哈哈哈哈😅😅😅
点赞 评论 收藏
分享
11-13 20:32
门头沟学院 Java
面向未来编程code:我没看到他咋急,他不就问你个问题。。。
点赞 评论 收藏
分享
11-07 13:31
怀化学院 Java
勇敢牛牛不怕难:又疯一个
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务