201301 JAVA题目0-1级

201301 JAVA题目0-1级

http://www.nowcoder.com/questionTerminal/9af744a3517440508dbeb297020aca86

dfs 搜索

import java.util.*;

public class Main {

    public Main() {
    }

    private boolean dfs(List<Integer> bag, int target, int i) {
        if (target == 0) return true;
        if (i == bag.size()) return false;
        // dfs搜索,对每个元素,选择或者放弃
        return dfs(bag, target, i + 1) || dfs(bag, target - bag.get(i), i + 1);
    }

    public boolean isPartition(int[] arr) {
        List<Integer> bag = new LinkedList<>();
        for (int i : arr) {
            bag.add(i);
        }
        // sum of 5 - sum of 3
        int diff = 0;
        // sum of others
        int others = 0;
        for (int i = 0; i < bag.size();) {
            if (bag.get(i) % 5 == 0) {
                diff += bag.get(i);
                bag.remove(i);
            }
            else if (bag.get(i) % 3 == 0) {
                diff -= bag.get(i);
                bag.remove(i);
            }
            else {
                others += bag.get(i++);
            }
        }
        if ((others - Math.abs(diff)) % 2 != 0) {
            return false;
        }
        // find if sub sum in bag equals target
        int target = (others - Math.abs(diff)) / 2;
        return dfs(bag, target, 0);
    }

    public static void main(String[] args) {
        Main solution = new Main();
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int n = in.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            boolean res = solution.isPartition(arr);
            System.out.println(res);
        }
    } 
}
全部评论

相关推荐

投递大华股份等公司10个岗位
点赞 评论 收藏
分享
one_t:硕还是本?什么岗
点赞 评论 收藏
分享
1 2 评论
分享
牛客网
牛客企业服务