JavaInterviewExercise:使用以下输入参数创建一个函数来执行定义的任务

问题描述 投票:-2回答:1

使用以下输入参数创建函数以执行定义的任务

findMatch(int[] list, int total, int numToUse) {
/* list = list of integers
total = a single integer
numToUse = how many numbers from the list to use as a maximum

This function should print all combinations of values in ‘list’, 
when summed up return the value in ‘total’
The value provided in ‘numToUse’ indicates the exact values to use 
from ‘list’ to calculate the ‘total’. If the value is 2 then use 
exactly 2 elements from ‘list’. 
If the value is 3, exactly 3 elements from ‘list’. 
In all cases, there should be No Duplicate set reported.

For example:
list[1,5,6,3,6,7]
total = 13
numToUse = 2

Then the following should be printed:
Element 3 + Element 6 = 13
Element 5 + Element 6 = 13

If numToUse = 3 with the same inputs, then
Element 1 + Element 2 + Element 6 = 13

*/
}

执行以下输入:

list = {3,4,6,7,10,3,9,15,17,17, -5, 10,7, -1}
total = 20
numToUse = 2, 3, and 4

请提供代码执行的输出。

java
1个回答
-3
投票
package QueEight;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.lang3.StringUtils;

public class V5_QueEight5 {
    static final Set<List<Integer>> finalList = new HashSet<List<Integer>>();
    static int tot;
    static int[] classIntArray;

    public static void main(String[] args) throws Exception {

        int[] intArray = new int[] { 1, 5, 6, 3, 6, 7 };
        int total = 13;
        int numToUse = 2;
        tot = total;
        classIntArray = intArray;

        List<Integer> intList = Arrays.stream(intArray).boxed().collect(Collectors.toList());
        List<List<Integer>> myLists = listPermutations(intList);

        for (List<Integer> al : myLists) {
            String joinedList = StringUtils.join(al, "");
            // System.out.println(joinedList);
            runGroupTally(joinedList, numToUse);
        }
        Set<List<Integer>> indexSet = convertToIndexSet(finalList);
        printout(indexSet);
    }

    private static void printout(Set<List<Integer>> indexSet) {
        for (List<Integer> li : indexSet) {
            StringBuffer sb = new StringBuffer();
            for (Integer i : li) {
                sb.append("Element " + i + " + ");
            }
            System.out.println(sb.toString() + " = " + tot);
        }
    }

    private static Set<List<Integer>> convertToIndexSet(Set<List<Integer>> finallist2) {
        Set<List<Integer>> returingSet = new HashSet<List<Integer>>();
        for (List<Integer> li : finallist2) {
            List<Integer> temp = new ArrayList<Integer>();
            for (Integer i : li) {
                Integer k = findIndexOf(i, classIntArray);
                temp.add(k);
            }
            returingSet.add(temp);
        }
        return returingSet;
    }

    static int findIndexOf(int V, int[] arr) {
        return IntStream.range(1, arr.length).filter(i->arr[i]==V).findFirst().getAsInt();
    }   

    private static void runGroupTally(String joinedList, int numToUse) {
        Matcher matcher = Pattern.compile("(\\d{" + numToUse + "})").matcher(joinedList);
        while (matcher.find()) {
            // System.out.println(matcher.group(0));
            List<Integer> x = Arrays.stream(matcher.group(0).split("")).map(Integer::parseInt)
                    .collect(Collectors.toList());
            int sum = x.stream().mapToInt(Integer::intValue).sum();
            if (tot == sum) {
                // save the group combination
                finalList.add(x);
            }
        }
    }

    public static List<List<Integer>> listPermutations(List<Integer> list) {
        if (list.size() == 0) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            result.add(new ArrayList<Integer>());
            return result;
        }
        List<List<Integer>> returnMe = new ArrayList<List<Integer>>();
        Integer firstElement = list.remove(0);

        List<List<Integer>> recursiveReturn = listPermutations(list);
        for (List<Integer> li : recursiveReturn) {
            for (int index = 0; index <= li.size(); index++) {
                List<Integer> temp = new ArrayList<Integer>(li);
                temp.add(index, firstElement);
                returnMe.add(temp);
            }
        }
        return returnMe;
    }
}

/*Above Program is NOT scalable for large array*/
© www.soinside.com 2019 - 2024. All rights reserved.