Java 由于超时而终止

Java 由于超时而终止,java,Java,当我提交我的代码时,我得到了一些成功的测试用例,但是对于其他一些测试用例,我由于超时而被终止,请问有什么帮助吗?我知道它必须更优化,但我尽了最大努力进行优化,我仍然有同样的问题,这是我的代码: public class Solution{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int T = scan.nextInt();// lo

当我提交我的代码时,我得到了一些成功的测试用例,但是对于其他一些测试用例,我由于超时而被终止,请问有什么帮助吗?我知道它必须更优化,但我尽了最大努力进行优化,我仍然有同样的问题,这是我的代码:

     public class Solution{
     public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int T = scan.nextInt();//
    long tab[] = new long[T];

    String s;
    int n;
    int j=0;

    while(T-->0){
    s=scan.next();
    n=scan.nextInt();
    if(s.equals("add"))
    {
        tab[j]=n;
        j++;
    }
    if(s.equals("del"))
    {

        int i=0;
        boolean e=false;
        while(!e)
        {
            if(tab[i]==n)
            {
                e=true;
                tab[i]=-1;
            }
            else
            {
                i++;
            }
        }
    }
    if(s.equals("cnt"))
    {

        int count=0;
        int k=0;
            while(k<j)

        {
            if((tab[k]!=-1)&&(n&tab[k])==tab[k])
            {
                count++;
            }
            k++;
        }
        System.out.println(count);
    }}

}
公共类解决方案{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int T=scan.nextInt()//
长制表符[]=新长制表符[T];
字符串s;
int n;
int j=0;
而(T-->0){
s=scan.next();
n=scan.nextInt();
如果(s.等于(“加”))
{
tab[j]=n;
j++;
}
如果(s.equals(“del”))
{
int i=0;
布尔e=false;
而(!e)
{
如果(制表符[i]==n)
{
e=真;
表[i]=-1;
}
其他的
{
i++;
}
}
}
如果(s.equals(“cnt”))
{
整数计数=0;
int k=0;

而(k如果测试用例的数量很高,那么Scanner可能比BufferedReader慢。我使用一个自定义类,因为Scanner可能对您有所帮助

private static final class FastScanner {
    private final InputStream mIs;
    private final byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner() {
        this(System.in);
    }

    public FastScanner(final InputStream is) {
        this.mIs = is;
    }

    public int read() {
        if (this.numChars == -1) {
            throw new InputMismatchException();
        }
        if (this.curChar >= this.numChars) {
            this.curChar = 0;
            try {
                this.numChars = this.mIs.read(this.buf);
            } catch (final IOException e) {
                throw new InputMismatchException();
            }
            if (this.numChars <= 0) {
                return -1;
            }
        }
        return this.buf[this.curChar++];
    }

    public String nextLine() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        final StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isEndOfLine(c));
        return res.toString();
    }

    public String nextString() {
        int c;
        while (isSpaceChar(c = read())) {
        }
        final StringBuilder res = new StringBuilder();
        do {
            res.appendCodePoint(c);
            c = read();
        } while (!isSpaceChar(c));
        return res.toString();
    }

    public long nextLong() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        long res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public int nextInt() {
        int c = read();
        while (isSpaceChar(c)) {
            c = read();
        }
        int sgn = 1;
        if (c == '-') {
            sgn = -1;
            c = read();
        }
        int res = 0;
        do {
            if (c < '0' || c > '9') {
                throw new InputMismatchException();
            }
            res *= 10;
            res += c - '0';
            c = read();
        } while (!isSpaceChar(c));
        return res * sgn;
    }

    public int[] nextArray(final int n) {
        final int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextInt();
        }
        return a;
    }

    public long[] nextLongArray(final int n) {
        final long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = nextLong();
        }
        return a;
    }

    public char[] nextCharArray(final int n) {
        final char[] buf = new char[n];
        int b, p = 0;
        while (isSpaceChar(b = read())) {
        }
        while (p < n && !isSpaceChar(b)) {
            buf[p++] = (char) b;
            b = read();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    public char[][] nextMatrix(final int n, final int m) {
        final char[][] map = new char[n][];
        for (int i = 0; i < n; i++) {
            map[i] = nextCharArray(m);
        }
        return map;
    }

    public int[][] nextIntMatrix(final int n, final int m) {
        final int[][] map = new int[n][];
        for (int i = 0; i < n; i++) {
            map[i] = nextArray(m);
        }
        return map;
    }

    public long[][] nextLongMatrix(final int n, final int m) {
        final long[][] map = new long[n][];
        for (int i = 0; i < n; i++) {
            map[i] = nextLongArray(m);
        }
        return map;
    }

    public boolean isSpaceChar(final int c) {
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public boolean isEndOfLine(final int c) {
        return c == '\n' || c == '\r' || c == -1;
    }
}
私有静态最终类FastScanner{
私有最终输入流管理信息系统;
私有最终字节[]buf=新字节[1024];
私有int curChar;
私有国际货币基金组织;
公共快速扫描程序(){
这(系统中);
}
公共快速扫描仪(最终输入流为){
this.mIs=is;
}
公共int read(){
if(this.numChars==-1){
抛出新输入失配异常();
}
如果(this.curChar>=this.numChars){
this.curChar=0;
试一试{
this.numChars=this.mIs.read(this.buf);
}捕获(最终IOE例外){
抛出新输入失配异常();
}
if(this.numChars'9'){
抛出新输入失配异常();
}
res*=10;
res+=c-‘0’;
c=读取();
}而(!isSpaceChar(c));
返回res*sgn;
}
public int nextInt(){
int c=read();
while(isSpaceChar(c)){
c=读取();
}
int-sgn=1;
如果(c=='-'){
sgn=-1;
c=读取();
}
int res=0;
做{
如果(c<'0'| c>'9'){
抛出新输入失配异常();
}
res*=10;
res+=c-‘0’;
c=读取();
}而(!isSpaceChar(c));
返回res*sgn;
}
公共整数[]次整数(最终整数n){
最终整数[]a=新整数[n];
对于(int i=0;i
您能为我们展示失败的特定测试的代码吗?您面临的问题是什么?当我提交代码时,我会收到“由于超时而终止”,我试图优化尽可能多的代码,但我仍然遇到同样的问题,如何解决这个问题?您共享了代码,但没有测试失败。我猜您的类无法处理某些输入,只能等待来自
系统的更多输入。
。(例如,如果第一个整数误报了参数的数量)为了确保这一点,我们需要看看测试是什么。