实现滑动窗口(java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29564490/
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
Implementing a sliding window (java)
提问by Nina Hain
I am fairly new to java (and programming overall) and have received the task to implement a Sliding Window object from start to finish. The skeleton code is as follows:
我对java(以及整体编程)相当陌生,并且已经收到了从头到尾实现滑动窗口对象的任务。骨架代码如下:
import java.util.Scanner;
//implement class SlidingWindow
class Main {
// this is the test code for the judge, do not modify
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int windowSize = scanner.nextInt();
SlidingWindow window = new SlidingWindow(windowSize);
while (scanner.hasNextInt())
{
int value = scanner.nextInt();
window.Put(value);
System.out.println("[" + window.Min() + " " + window.Max() + "]");
}
scanner.close();
}
What needs to be done (and the ideas I have had so far towards solving it)
需要做什么(以及我迄今为止解决它的想法)
Create a sliding window w that can be instantiated with a window size n:
SlidingWindow w = New SlidingWindow(n) //This is what stumps me the most - is it a self-growing array? A linked list?
An input method Put that accepts integer numbers and has no return value:
public static void Put(int value){ // implementation will depend on SlidingWindow
A Max and Min method to return the min and max of the most recent inputs. This I will just do by looking for the min and max.
If the number of inputs exceeds the size of the window, the oldest element should be discarded.
创建一个可以用窗口大小 n 实例化的滑动窗口 w:
SlidingWindow w = New SlidingWindow(n) //这是最让我难受的——它是一个自增长的数组吗?一个链表?
接受整数且没有返回值的输入方法 Put :
public static void Put(int value){ // 实现将依赖于 SlidingWindow
Max and Min 方法返回最近输入的最小值和最大值。这我将通过寻找最小值和最大值来完成。
如果输入的数量超过窗口的大小,则应丢弃最旧的元素。
Sorry for the vagueness of the question - we never dealt with windows in class (I am a few weeks in to learning anything about coding) so I am truly stumped. I have looked around online for some resources but have not yet found anything suitable to help me.
很抱歉这个问题含糊不清——我们从来没有在课堂上处理过窗户(我有几周的时间来学习任何关于编码的知识)所以我真的很难过。我在网上四处寻找一些资源,但还没有找到任何适合我的东西。
If you have any ideas or advice to offer on how to create SlidingWindow w, I think that will get me on the right track!
如果您对如何创建 SlidingWindow w 有任何想法或建议,我认为这将使我走上正轨!
Thanks in advance!
提前致谢!
回答by Paul
For the sliding window, the simplest would possibly be a counter for the number of the insertion. Like this:
对于滑动窗口,最简单的可能是插入次数的计数器。像这样:
class Window{
int ct = 0;
int[] storage;
public Window(int size){
storage = new int[size];
}
public void put(int i){
storage[ct % storage.length] = i;
ct++;
}
}
This way, you can use a fixed size array and replace the oldest value by newer ones as soon as the array is filled, without shifting content.
这样,您可以使用固定大小的数组,并在数组填充后立即用较新的值替换最旧的值,而无需移动内容。
回答by Kaushik Basu
I am hereby providing an easy and relatively simple solution. I am giving code of two java files.
我在此提供一个简单且相对简单的解决方案。我给出了两个 java 文件的代码。
The Class:
班上:
public class Window {
public void getValue(int[] a, int w){
int t = 0;
for(int i=0;i<a.length;i++){
if(i==a.length-w+1){
break;
}else{
for(int j=i;j<i+w;j++){
if(t<a[j]){
t = a[j];
}
}
}
System.out.println(t);
t = 0;
}
}
}
The Main method:
主要方法:
public class MainMethod {
public static void main(String[] args) {
int[] a = new int[]{1, 3, -1, -3, 5, 3, 6, 7};
int w = 3;
Window ob = new Window();
ob.getValue(a, w);
}
}
Answer: 3 3 5 5 6 7
I only left checking of the length of the window should not be greater than the array.
我只剩下检查窗口的长度不应大于数组。