我正在尝试解决一个问题,但得到内存限制我认为这是在输出之前缓冲的原因我有整数,我怎样才能以最少的内存使用量输出它们?现在我正在使用 PrintWriter。还有其他更好的方法吗?
import java.io.*;
import java.util.*;
class stack_ {
public char index;
public int value;
public stack_(char ind, int val) {
index = ind;
value = val;
}
}
public class timous {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer( new BufferedReader (new InputStreamReader(System.in)) );
Vector<stack_> numbers = new Vector<stack_>();
PrintWriter out = new PrintWriter(System.out);
in.nextToken();
char num = (char)in.nval;
for (char i = 0, k; i<num; i++) {
in.nextToken();
if (in.sval.equals("POP")) {
k = (char)(numbers.size()-1);
in.nextToken();
while (numbers.elementAt(k).index!=(char) in.nval)
k--;
out.println(numbers.elementAt(k).value);
numbers.removeElementAt(k);
}
else {
in.nextToken();
k = (char)in.nval;
in.nextToken();
numbers.add(new stack_(k, (int) in.nval));
}
}
out.flush();
}
}
内存限制测试3;时间 0.078; 内存 834 KB
import java.io.*;
import java.util.*;
public class timous {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer( new BufferedReader (new InputStreamReader(System.in)) );
int[] numbers_int = new int[20000];
char[] numbers_char = new char[20000];
PrintWriter out = new PrintWriter(System.out,true);
in.nextToken();
char num = (char)in.nval;
for (char i = 0, k, cur=0; i<num; i++) {
in.nextToken();
if (in.sval.charAt(1)=='O') {
k = cur;
k--;
in.nextToken();
while (numbers_char[k]!=(char) in.nval)
k--;
out.println(numbers_int[k]);
for (; k<19999; k++){
numbers_int[k] = numbers_int[k+1];
numbers_char[k] = numbers_char[k+1];
}
cur--;
}
else {
in.nextToken();
numbers_char[cur] = (char)in.nval;
in.nextToken();
numbers_int[cur] = (int)in.nval;
cur++;
}
}
}
}