java 修复了2个应该删除重复和逆序对的方法

brtdzjyr  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(107)

只是学习和希望改变职业...我需要帮助纠正第二和第三种方法,使他们正确打印!我能够一起破解这个(这花了很长时间),我需要什么代码来修复第二和第三种方法?
我想让它工作,然后回去自学,因为这是我的学习风格!
第2种和第3种方法都无法正确打印这些对:(五、五)(五、五)(六、四)(九、一)

import java.util.*;

public class TenPairs {

    public static void main(String[] args) {
        int a[] = { 1, 1, 2, 4, 4, 5, 5, 5, 6, 7, 9 };
        findAllPairs(a, 10);
        findUniquePairs(a, 10);
        findComboPairs(a, 10);
    }

    // Method 1 - output all pairs would output: [1,9], [1,9], [4,6], [4,6], [5,5],
    // [5,5], [5,5], [5,5], [5,5],[5,5],[6,4],[6,4][9,1],[9,1]

    static void findAllPairs(int[] array, int sum) {

        System.out.println("All pairs(including duplicates and reverse order pairs) whose sum is " + sum + ":");

        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] + array[j] == sum) {
                    System.out.println("(" + array[i] + "," + array[j] + ")" + "(" + array[j] + "," + array[i] + ")");
                }
            }
        }
    }

    // Method2 - output unique pairs only once would output: [1,9], [4,6], [5,5],
    // [6,4], [9,1]
    static void findUniquePairs(int[] array, int sum) {

        System.out.println("All pairs only once(including reverse order pairs but excluding duplicates) whose sum is "
                + sum + ":");

        Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
        for (int i = 0; i < array.length; i++) {
            if (pairs.containsKey(sum - array[i]))
                System.out.println("(" + array[i] + "," + (sum - array[i]) + ")");
            else
                pairs.put(array[i], 0);
        }
    }

    // Method3 - output the same combo pair only once would output: [1,9], [4,6],
    // [5,5]
    static void findComboPairs(int[] array, int sum) {

        System.out
                .println("All pairs only once(excluding reverse order pairs and duplicates) whose sum is " + sum + ":");

        Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();
        for (int i = 0; i < array.length; i++) {
            if (pairs.containsKey(sum - array[i]))
                System.out.println("(" + array[i] + "," + (sum - array[i]) + ")");
            else
                pairs.put(array[i], 0);
        }
    }
}
goqiplq2

goqiplq21#

有很多方法可以做到这一点,其中一种方法是从数组中创建一个List,然后在处理列表时将使用的元素设置为null
请按以下步骤操作:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TenPairs {

    public static void main(String[] args) {
        int a[] = { 1, 1, 2, 4, 4, 5, 5, 5, 6, 7, 9 };
        findAllPairs(a, 10);
        findUniquePairs(a, 10);
        findComboPairs(a, 10);
    }

    static void findAllPairs(int[] array, int sum) {

        System.out.println("All pairs(including duplicates and reverse order pairs) whose sum is " + sum + ":");

        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[i] + array[j] == sum) {
                    System.out.println("(" + array[i] + "," + array[j] + ")" + "(" + array[j] + "," + array[i] + ")");
                }
            }
        }
    }

    static void findUniquePairs(int[] array, int sum) {

        System.out.println("All pairs only once(including reverse order pairs but excluding duplicates) whose sum is "
                + sum + ":");
        List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());
        int x = 0, y = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (list.get(i) != null) {
                    x = list.get(i);
                }
                if (list.get(j) != null) {
                    y = list.get(j);
                }
                if (x + y == sum) {
                    if (x != y) {
                        System.out.println("(" + x + "," + y + ")");
                    }
                    System.out.println("(" + y + "," + x + ")");
                    list.set(i, null);
                    list.set(j, null);
                    x = 0;
                    y = 0;
                }
            }
        }
    }

    static void findComboPairs(int[] array, int sum) {

        System.out.println(
                "All pairs only once(excluding reverse order pairs and duplicates) whose sum is " + sum + ": ");
        List<Integer> list = Arrays.stream(array).boxed().collect(Collectors.toList());
        int x = 0, y = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (list.get(i) != null) {
                    x = list.get(i);
                }
                if (list.get(j) != null) {
                    y = list.get(j);
                }
                if (x + y == sum) {
                    System.out.println("(" + x + "," + y + ")");
                    list.set(i, null);
                    list.set(j, null);
                    x = 0;
                    y = 0;
                }
            }
        }
    }
}
    • 输出:**
All pairs(including duplicates and reverse order pairs) whose sum is 10:
(1,9)(9,1)
(1,9)(9,1)
(4,6)(6,4)
(4,6)(6,4)
(5,5)(5,5)
(5,5)(5,5)
(5,5)(5,5)
All pairs only once(including reverse order pairs but excluding duplicates) whose sum is 10:
(1,9)
(9,1)
(4,6)
(6,4)
(5,5)
All pairs only once(excluding reverse order pairs and duplicates) whose sum is 10: 
(1,9)
(4,6)
(5,5)

第二种方法和第三种方法的主要区别是打印对的次数。在第二种方法中,除非对具有相同的元素,否则必须打印对两次(一次按出现的顺序,另一次按相反的顺序)。

相关问题