📜  如何摆脱Java TLE问题

📅  最后修改于: 2021-06-26 21:19:05             🧑  作者: Mango

您多次编写正确的Java代码,并根据约束进行了必要的优化,这经常发生。但是,您会获得TLE😢。
发生这种情况的原因是Java使用Scanner类进行输入和输出输出所花费的时间比BufferedReader和StringBuffer类要慢。在此处详细阅读有关扫描仪类的信息。

看一些技巧来摆脱这个TLE问题(当您的逻辑明显正确时)?

让我们从GeeksforGeeks练习中解决一个问题,并解决TLE问题:
问题:将0、1、2和2的数组隔离
简而言之,问题在于给定一个0、1和2的数组。我们必须隔离数组开头的所有0,数组中间的所有1和数组末尾的所有2。
例子:

Input : 1 1 2 0 0 2 1
Output : 0 0 1 1 1 2 2

方法:分离0、1和2的数组

下面是上述方法的实现:

// Program to segragate the
// array of 0s, 1s and 2s
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        // Using Scanner class to take input
        Scanner sc = new Scanner(System.in);
  
        // Number of testcase input
        int t = sc.nextInt();
  
        // Iterating through all the testcases
        while (t-- > 0) {
  
            // Input n, i.e. size of array
            int n = sc.nextInt();
  
            int arr[] = new int[n];
  
            // Taking input of array elements
            for (int i = 0; i < n; i++)
                arr[i] = sc.nextInt();
  
            // Calling function to segragate
            // input array
            segragateArr(arr, n);
  
            // printing the modified array
            for (int i = 0; i < n; i++) {
                System.out.print(arr[i] + " ");
            }
  
            System.out.println();
        }
        sc.close();
    }
  
    // Function to segragate 0s, 1s and 2s
    public static void segragateArr(int arr[], int n)
    {
        /*
        low : to keep left index
        high : to keep right index
        mid : to get middle element
        */
        int low = 0, high = n - 1, mid = 0;
  
        // Iterating through the array and
        // segregating elements
        while (mid <= high) {
  
            // If element at mid is 0
            // move it to left
            if (arr[mid] == 0) {
                int temp = arr[low];
                arr[low] = arr[mid];
                arr[mid] = temp;
                low++;
                mid++;
            }
  
            // If element at mid is 1
            // nothing to do
            else if (arr[mid] == 1) {
                mid++;
            }
  
            // If element at mid is 2
            // move it to last
            else {
                int temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
            }
        }
    }
}

根据我们的期望,它应该通过所有测试用例,并在GeeksforGeeks实践中被接受。但是,当我们在GeeksforGeeks IDE上提交此代码时,它将显示TLE。

这表示我们已经超过了预期的时间限制。没问题,让我们使用上面给出的提示。

  1. 使用BufferedReader进行输入。
  2. 使用StringBuffer保存和打印输出。

方法:分离0、1和2的数组

以下是用于分隔0、1和2的Java代码的实现

// Java program to segragate
// array of 0s, 1s and 2s
import java.io.*;
import java.util.*;
  
class GFG {
    // Driver Code
    public static void main(String[] args) throws IOException
    {
  
        // Using BufferedReader class to take input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  
        // taking input of number of testcase
        int t = Integer.parseInt(br.readLine());
  
        while (t-- > 0) {
            // n : size of array
            int n = Integer.parseInt(br.readLine());
  
            // Declaring array
            int arr[] = new int[n];
  
            // to read multiple integers line
            String line = br.readLine();
            String[] strs = line.trim().split("\\s+");
  
            // array elements input
            for (int i = 0; i < n; i++)
                arr[i] = Integer.parseInt(strs[i]);
  
            // Calling Functions to segregate Array elements
            segragateArr(arr, n);
  
            // Using string buffer to append each output in a string
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < n; i++)
                sb.append(arr[i] + " ");
  
            // finally printing the string
            System.out.println(sb);
        }
        br.close();
    }
  
    // Function to segragate 0s, 1s and 2s
    public static void segragateArr(int arr[], int n)
    {
        /*
        low : to keep left index
        high : to keep right index
        mid : to get middle element
        */
        int low = 0, high = n - 1, mid = 0;
  
        // Iterating through the array and
        // segregating elements
        while (mid <= high) {
  
            // If element at mid is 0
            // move it to left
            if (arr[mid] == 0) {
                int temp = arr[low];
                arr[low] = arr[mid];
                arr[mid] = temp;
                low++;
                mid++;
            }
  
            // If element at mid is 1
            // nothing to do
            else if (arr[mid] == 1) {
                mid++;
            }
  
            // If element at mid is 2
            // move it to last
            else {
                int temp = arr[mid];
                arr[mid] = arr[high];
                arr[high] = temp;
                high--;
            }
        }
    }
}


伟大的!您已经升级。
Java TLE问题?似乎非常简单:)。您现在可以尝试。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。