修复两个方法,这些方法应该移除重复和反向顺序的成对。

huangapple go评论96阅读模式
英文:

Fixing 2 methods that are supposed to remove duplicate and reverse order pairs

问题

只学习并希望转行职业...我需要帮助纠正第二个和第三个方法,使它们正确打印!我能够拼凑出这个代码(花了很长时间),我需要哪些代码来修复第二个和第三个方法?

我希望让它正常工作,然后回过头来自学,因为这是我的学习风格!

第二个和第三个方法都在错误地打印这些对:(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("所有配对(包括重复和颠倒顺序的配对),其和为 " + 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("只显示一次的所有配对(包括颠倒顺序的配对,但不包括重复的配对),其和为 "
                + 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("只显示一次的所有配对(不包括颠倒顺序的配对和重复的配对),其和为 " + 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);
        }
    }
}
英文:

Just learning and hoping to change careers...I need help correcting the 2nd and 3rd methods so they print correctly! I was able to hack this together(which took a very long time), what code do I need to fix the 2nd and 3rd methods?

I'd like to get it working and then go back and teach myself as that's my learning style!

Both the 2nd and 3rd methods are incorrectly printing these pairs: (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(&quot;All pairs(including duplicates and reverse order pairs) whose sum is &quot; + sum + &quot;:&quot;);
for (int i = 0; i &lt; array.length; i++) {
for (int j = i + 1; j &lt; array.length; j++) {
if (array[i] + array[j] == sum) {
System.out.println(&quot;(&quot; + array[i] + &quot;,&quot; + array[j] + &quot;)&quot; + &quot;(&quot; + array[j] + &quot;,&quot; + array[i] + &quot;)&quot;);
}
}
}
}
// 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(&quot;All pairs only once(including reverse order pairs but excluding duplicates) whose sum is &quot;
+ sum + &quot;:&quot;);
Map&lt;Integer, Integer&gt; pairs = new HashMap&lt;Integer, Integer&gt;();
for (int i = 0; i &lt; array.length; i++) {
if (pairs.containsKey(sum - array[i]))
System.out.println(&quot;(&quot; + array[i] + &quot;,&quot; + (sum - array[i]) + &quot;)&quot;);
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(&quot;All pairs only once(excluding reverse order pairs and duplicates) whose sum is &quot; + sum + &quot;:&quot;);
Map&lt;Integer, Integer&gt; pairs = new HashMap&lt;Integer, Integer&gt;();
for (int i = 0; i &lt; array.length; i++) {
if (pairs.containsKey(sum - array[i]))
System.out.println(&quot;(&quot; + array[i] + &quot;,&quot; + (sum - array[i]) + &quot;)&quot;);
else
pairs.put(array[i], 0);
}
}
}

答案1

得分: 2

有很多种方法可以做到这一点。其中一种方法是将数组创建成一个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("所有和为 " + 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("只出现一次的所有配对(包括逆序配对,但不包括重复项):");
        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("只出现一次的所有配对(不包括逆序配对和重复项):");
        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;
                }
            }
        }
    }
}

输出:

所有和为 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)
只出现一次的所有配对(包括逆序配对,但不包括重复项):
(1,9)
(9,1)
(4,6)
(6,4)
(5,5)
只出现一次的所有配对(不包括逆序配对和重复项):
(1,9)
(4,6)
(5,5)

第二种方法和第三种方法之间的主要区别在于打印配对的次数。在第二种方法中,您需要将配对打印两次(一次按照出现顺序,另一次按照逆序),除非配对具有相同的元素。

英文:

There can be many ways to do it. One of the ways is to create a List out of the array and then set the used elements to null as you process the list.

Do it as follows:

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(&quot;All pairs(including duplicates and reverse order pairs) whose sum is &quot; + sum + &quot;:&quot;);
for (int i = 0; i &lt; array.length; i++) {
for (int j = i + 1; j &lt; array.length; j++) {
if (array[i] + array[j] == sum) {
System.out.println(&quot;(&quot; + array[i] + &quot;,&quot; + array[j] + &quot;)&quot; + &quot;(&quot; + array[j] + &quot;,&quot; + array[i] + &quot;)&quot;);
}
}
}
}
static void findUniquePairs(int[] array, int sum) {
System.out.println(&quot;All pairs only once(including reverse order pairs but excluding duplicates) whose sum is &quot;
+ sum + &quot;:&quot;);
List&lt;Integer&gt; list = Arrays.stream(array).boxed().collect(Collectors.toList());
int x = 0, y = 0;
for (int i = 0; i &lt; array.length; i++) {
for (int j = i + 1; j &lt; 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(&quot;(&quot; + x + &quot;,&quot; + y + &quot;)&quot;);
}
System.out.println(&quot;(&quot; + y + &quot;,&quot; + x + &quot;)&quot;);
list.set(i, null);
list.set(j, null);
x = 0;
y = 0;
}
}
}
}
static void findComboPairs(int[] array, int sum) {
System.out.println(
&quot;All pairs only once(excluding reverse order pairs and duplicates) whose sum is &quot; + sum + &quot;: &quot;);
List&lt;Integer&gt; list = Arrays.stream(array).boxed().collect(Collectors.toList());
int x = 0, y = 0;
for (int i = 0; i &lt; array.length; i++) {
for (int j = i + 1; j &lt; 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(&quot;(&quot; + x + &quot;,&quot; + y + &quot;)&quot;);
list.set(i, null);
list.set(j, null);
x = 0;
y = 0;
}
}
}
}
}

Output:

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)

The main difference between the 2nd and the 3rd method is the number of times you print the pairs. In the 2nd method, you have to print the pairs twice (once in the order of occurrence and another in the reverse order) except when the pair has same elements.

huangapple
  • 本文由 发表于 2020年4月6日 01:01:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61046240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定