gpt4 book ai didi

java - 修复 2 个应该删除重复和反向顺序对的方法

转载 作者:行者123 更新时间:2023-12-01 14:27:47 25 4
gpt4 key购买 nike

只是学习并希望改变职业......我需要帮助来纠正第 2 和第 3 种方法,以便它们正确打印!我能够一起解决这个问题(这花了很长时间),我需要什么代码来修复第二种和第三种方法?

我想让它发挥作用,然后回去自学,因为那是我的学习风格!

第二种和第三种方法都错误地打印了这些对:(5,5)(5,5)(6,4)(9,1)

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);
}
}
}

最佳答案

有很多方法可以做到这一点。其中一种方法是从数组中创建一个 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)

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

关于java - 修复 2 个应该删除重复和反向顺序对的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61046240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com