简体   繁体   English

在Java中将队列用于int数组,找到两个加起来等于特定和n的数字

[英]Use of queues in Java for an array of int, find two numbers that add up to specific sum n

I need to write a piece of code using Queues in Java that allows me to find, using 2 integers out of the given array that together give me a specific sum integer of x. 我需要使用Java中的Queues编写一段代码,该代码使我能够使用给定数组中的2个整数进行查找,这些整数共同为我提供了x的特定总和。

Let's say: 比方说:

int[] array = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = 5;

I understand what a queue is and the principal between enqueue and dequeue. 我了解什么是队列以及入队与出队之间的主体。 I did the code without using queues with a double for loop but I don't really understand how to do it using queues. 我没有在代码中使用带有double for循环的队列,但是我不太了解如何使用队列。

Can anyone give me a hint or provide help? 谁能给我提示或提供帮助? It would be very appreciated. 将不胜感激。 (I'm also allowed to use stacks but queues seemed simpler to me). (我也被允许使用堆栈,但是队列对我来说似乎更简单)。

Is it okay to sort the values, eg use a PriorityQueue ? 可以对值进行排序,例如使用PriorityQueue吗?

In that case, just use 2 PriorityQueue s, one sorted ascending, one sorted descending. 在这种情况下,只需使用2个PriorityQueue ,一个排序为升序,一个排序为降序。 peek() at both queues and add the values. 在两个队列中进行peek()并添加值。 If the sum is too low, poll() the first queue, if too high, poll() the second queue. 如果总和太低,则在第一个队列中poll() ,如果太高,则在第二个队列中poll() Repeat until the sum is correct, or until the queues are empty. 重复直到总和正确,或者直到队列为空。

Alternatively, use a single Deque , add the values after sorting, and work from both ends. 或者,使用单个Deque ,在排序后添加值,然后从两端开始工作。

I tried to find a way to use only regular queues to work this out. 我试图找到一种仅使用常规队列来解决此问题的方法。 So far, it will print all the possible pairs that add up to the specific sum you want. 到目前为止,它将打印所有可能的对,它们加起来就是您想要的特定总和。 In this case I just put 5. However, an issue that came up is that I got duplicate pairs when iterating through the queue. 在这种情况下,我只放入5。但是,出现的问题是,在队列中进行迭代时,我得到了重复的对。 It seems to be because of the while loop...I coded this in a hurry, but hopefully it gives you some hints and helped in some way! 似乎是由于while循环的缘故...我匆忙编写了此代码,但希望它能给您一些提示并以某种方式有所帮助!

import java.util.Queue;
import java.util.LinkedList;

public class QueueSums {

public static void main(String args[]) {
    int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    Queue<Integer> queue = new LinkedList<>();

    for (int i : array) {
        queue.add(i);
    }

    System.out.println(queue);

    while (iterateQueue(queue) == false) {
        queue.poll();
        iterateQueue(queue);
    }

}

public static boolean iterateQueue(Queue<Integer> queue) {
    Queue<Integer> queueCopy = new LinkedList<>();
    queueCopy.addAll(queue);

    int sum = 5;
    System.out.println(sum);

    int num1 = queueCopy.peek();

    while (queueCopy.size() > 0) {
        int num2 = queueCopy.peek();

        if (sum == num1 + num2) {
            System.out.println(num1 + " + " + num2 + " works");
            return true;
        }

        else {
            queueCopy.poll();
        }
    }
    return false;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找数组中的3个数字是否加起来 - find if 3 numbers in an array add up to a sum 给定固定长度的int数组,找到最小的数字,然后相加并返回剩余数字的总和 - Given a fixed length int array, find the lowest number, and then add and return the sum of the remaining numbers 如何使用IntStream来汇总int数组的特定索引号? - How can I use an IntStream to sum specific indexed numbers of an int array? 查找数组中两个数字的总和是否等于 k - Find if sum of two numbers in an array is equal to k 用两个数字的特定数字和对数字进行排队的算法 - Algorithm to line up numbers with a specific digit sum of two numbers 在 O(n * log n) 时间内找到 Array 中加起来等于目标数的两个元素 - Find Two elements in the Array that add up to the Target number in O(n * log n) Time Java:找到二维数字数组的总和 - Java: find sum of 2d array of numbers Java/数字数组中任意两个值的总和 - Java/ Sum of any two values in an array of numbers 查找一个int数组的总和-忽略6到7之间的所有数字(包括6和7) - Find the sum of an int array - leaving out all numbers between a 6 and a 7, inclusive 练习面试题,求数组中两个连续数的最大和Java - Practicing Interview Question, Find Largest sum Of Two Consecutive Numbers In An Array Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM