终止到期超时

时间:2015-08-02 12:35:37

标签: java

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

     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);
    }}

}

}

1 个答案:

答案 0 :(得分:0)

如果测试用例的数量很高,那么与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;
    }
}