如何解决我的Java代码中超出“内存限制”的问题?

时间:2019-04-24 02:21:18

标签: java

我正在尝试在lightoj.com平台上提交问题。 问题的链接:https://vjudge.net/problem/LightOJ-1088

我正在尝试针对此问题提交Java解决方案。 我做错了什么,因为它使用了比预期多的内存? 我不明白为什么会给出“超出内存限制”的结论。

这是我的JAVA代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;


public class Main {

    static class FastReader { 
        BufferedReader br; 
        StringTokenizer st; 

        public FastReader() 
        { 
            br = new BufferedReader(new
                    InputStreamReader(System.in)); 
        } 

        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 

        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 

        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 

        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 

        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    }

    static int l,r,mid,i1,i2,now;

    static int get(int a, int b, int[] arr, int n) {
        if(arr[n-1]<a || arr[0]>b) return 0;
        l = 0;
        r = n-1;
        while(l<=r) {
            mid = (l+r)/2;
            if(arr[mid]==a) {
                now=mid;
                break;
            }
            if(arr[mid]<a) l = mid+1;
            else {
                r = mid-1;
                now = mid;
            }
        }
        i1 = now;
        l=0;
        r=n-1;
        while(l<=r) {
            mid = (l+r)/2;
            if(arr[mid]==b) {
                now = mid;
                break;
            }
            if(arr[mid]<b) {
                now = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        i2=now;
        return i2-i1+1;
    }

    public static void main(String[] args) {

        FastReader sc = new FastReader();
        OutputStream outputstream = System.out;
        PrintWriter out = new PrintWriter(outputstream);

        int t = sc.nextInt();
        int n,q;
        int a,b;
        int[] arr = new int[100000];
        long ans;
        int cas = 0;
        while(t--!=0) {
            out.println("Case " + ++cas + ":");
            n = sc.nextInt();
            q = sc.nextInt();
            for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
            while(q--!=0) {
                a = sc.nextInt();
                b = sc.nextInt();
                ans = get(a,b,arr,n);
                out.println(ans);
            }
        }
        out.close();

    }

}

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。对我来说,使用以下过程被接受了。

  1. 在for或while循环内分配测试用例的所有变量
  2. 打印数据后,使这些变量指向空
  3. 致电System.gc(); 这样,多余的内存将被Java垃圾回收器清除。

对于您的问题,请在下面执行以下操作。

public static void main(String[] args) {

    FastReader sc = new FastReader();
    OutputStream outputstream = System.out;
    PrintWriter out = new PrintWriter(outputstream);

    int t = sc.nextInt();
    int n,q;
    int a,b;
    long ans;
    int cas = 0;
    while(t--!=0) {
            out.println("Case " + ++cas + ":");
            n = sc.nextInt();
            q = sc.nextInt();
            int[] arr = new int[n];
            for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
            while(q--!=0) {
                a = sc.nextInt();
                b = sc.nextInt();
                ans = get(a,b,arr,n);
                out.println(ans);
            }
            arr=null;
            System.gc();
        }
        out.close();
}