zoukankan      html  css  js  c++  java
  • LintCode 90. k数和 II

    FS类题目,果然题目都是靠刷的,这个题目和全排列差不多,自己很快就一套撸出了,主要的细节就是添加一个beginNum变量方便控制重复的例如[1,4][4,1]等重复答案,具体可以参照我的代码

    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class kSumII {
        /**
         * @param A: an integer array
         * @param k: a postive integer <= length(A)
         * @paramtarget: an integer
         * @return: A list of lists of integer
         * <p>
         * 90. k数和 II
         * Given n unique integers, number k (1<=k<=n) and target.
         * <p>
         * Find all possible k integers where their sum is target.
         * <p>
         * 样例
         * 给出[1,2,3,4],k=2, target=5,返回 [[1,4],[2,3]]
         */
        public List<List<Integer>> kSumII(int[] A, int k, int targer) {
            // write your code here
            List<List<Integer>> result = new ArrayList<>();
            List<Integer> list = new ArrayList<>();
            if (A == null) {
                return result;
            }
            if (A.length == 0) {
                result.add(list);
                return result;
            }
            boolean[] color = new boolean[A.length];
            for (int i = 0; i < A.length; i++) {
                color[i] = false;
            }
            //先对数组进行排序方便后续运算
            Arrays.sort(A);
            int beginNum = A[0];
            //这里添加一个beginNum是的结果是升序,去除重复
            dfsSum(result, list, color, A, k, targer, beginNum);
            return result;
        }
    
        private void dfsSum(List<List<Integer>> result, List<Integer> list, boolean[] color, int[] a,
                            int k, int targer, int beginNum) {
            if (list.size() == k) {
                result.add(new ArrayList<Integer>(list));
                return;
            }
            for (int i = 0; i < a.length; i++) {
                if (beginNum <= a[i] && color[i] == false && (sumList(list) + a[i]) <= targer &&
                        list.size() < k) {
                    if (list.size() < k - 1) {
                        list.add(a[i]);
                        color[i] = true;
                        dfsSum(result, list, color, a, k, targer, list.get(list.size() - 1));
                        //重置为上一个状态
                        list.remove(list.size() - 1);
                        color[i] = false;
                    } else if (list.size() == k - 1 && (sumList(list) + a[i]) == targer) {
                        list.add(a[i]);
                        color[i] = true;
                        dfsSum(result, list, color, a, k, targer, list.get(list.size() - 1));
                        //重置为上一个状态
                        list.remove(list.size() - 1);
                        color[i] = false;
                    }
                }
            }
        }
    
        public int sumList(List<Integer> list) {
            int sum = 0;
            for (int i = 0; i < list.size(); i++) {
                sum += list.get(i);
            }
            return sum;
        }
    
        @Test
        public void testkSumII() {
            // 给出[1,2,3,4],k=2, target=5,返回 [[1,4],[2,3]]
            int[] nums = {1, 4, 7, 10, 12, 15, 16, 18, 21, 23, 24, 25, 26, 29};
            List<List<Integer>> result = kSumII(nums, 5, 113);
            for (int i = 0; i < result.size(); i++) {
                System.out.println(result.get(i).toString());
            }
        }
    }
  • 相关阅读:
    毕业3年在北京买房,他是怎么赚钱攒钱的?
    Windows Server 2008 如何在IIS中添加MIME类型
    IIS下无法访问.ini后缀文件
    新的一年,我们如何才能收获满满,不留太多遗憾呢?
    你百分之九十九的问题都是因为懒
    为什么你容许陌生人成功,却无法忍受身边人发达
    堆排序
    计数排序
    直接插入排序
    冒泡排序
  • 原文地址:https://www.cnblogs.com/wei1/p/9582053.html
Copyright © 2011-2022 走看看