Java 将封闭范围之间的奇数添加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51221867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:01:11  来源:igfitidea点击:

Add odd numbers between a closed range to an array

javaarraysalgorithm

提问by user6248190

I am doing code challenge where given two integers land r, I have to print all the odd numbers between iand r(iand rinclusive). The function must return an array of integers denoting the odd numbers between land r.

我做的代码的挑战,其中给出了两个整数lr,我不得不之间打印所有的奇数irir含)。该函数必须返回一个整数数组,表示l和之间的奇数r

This is what I have so far

这是我到目前为止

static int[] oddNumbers(int l, int r) {
    int[] theArray = new int[r];
    for (int i = l; i < theArray.length; i++) {
        if (i % 2 != 0) {
            for (int n = 0; n < theArray.length; n++) {
                theArray[n] = i;
            }
        }
    }
    return theArray;

}

So at the moment this code if you give it values 2 and 5 should return 3,5. However, this only returns 33333. What am I doing wrong? And how can I improve this code?

所以目前这个代码如果你给它值 2 和 5 应该返回 3,5。但是,这只返回 33333。我做错了什么?我该如何改进这段代码?

采纳答案by Tim Biegeleisen

I would approach this by advancing the lower bound in the range to the next odd number, if not already odd, and then proceeding from there.

我会通过将范围内的下限推进到下一个奇数来解决这个问题,如果不是奇数,然后从那里开始。

static int[] oddNumbers(int l, int r) {
    if (r <= l) return null;

    l = (l % 2) == 0 ? l + 1 : l;
    int size = ((r - l) / 2) + 1;

    int[] theArray = new int[size];

    for (int i=0; i < size; ++i) {
        theArray[i] = l + (i*2);
    }

    return theArray;
}

The real difficulty here is in formulating the logic to map a range of odd numbers onto a flat array. Once we have done this, we can see that populating the array only requires a simple for loop.

这里真正的困难在于制定将一系列奇数映射到平面数组的逻辑。完成此操作后,我们可以看到填充数组只需要一个简单的 for 循环。

回答by oleg.cherednik

static int[] oddNumbers(int l, int r) {
    int[] theArray = new int[((r - l) / 2) + 1];
    for (int i = l % 2 == 0 ? l : l + 1, j = 0; i <= r; i += 2, j++)
        theArray[j] = i;


    return theArray;

}

回答by Andrew Tobilko

I would delegate the work to a stream or list which can easily and dynamically manage the size of a selection.

我会将工作委托给一个流或列表,它们可以轻松动态地管理选择的大小。

Why should we bother about the size of a resulting array if we could make a simple transformation from an appropriate structure at the end?

如果我们可以在最后从一个适当的结构进行简单的转换,为什么我们还要为结果数组的大小而烦恼呢?

static int[] oddNumbers(int l, int r) {
    return IntStream.iterate(l, i -> i <= r, i -> ++i)  
                    .filter(i -> i % 2 != 0)
                    .toArray();
}

回答by roxch

We are in 2018 and do already have the amazing Streamssince Java 8! With that you can solve such things in a single line! So it's worth it to take a look at it:

我们在 2018 年,并且已经拥有Streams自 Java 8 以来的惊人!有了它,您可以在一行中解决此类问题!所以值得一看:

static int[] oddNumbers(int i, int j){
        return IntStream.rangeClosed(i, j).filter(num -> num % 2 == 1).toArray();
    }

(unless you really want to learn algorithms and not just solve the challenge!)

(除非你真的想学习算法而不仅仅是解决挑战!)