很多时候,您已经编写了正确的Java代码,并根据约束条件进行了尽可能多的优化。但是,你得到了很多????。 这是因为Java使用Scanner类获取输入和写入输出所花费的时间,与BufferedReader和StringBuffer类相比,Scanner类的速度较慢。详细阅读Scanner类 在这里 . 看看一些技巧来解决这个问题(当你的逻辑显然是正确的时候)?
提示1: 避免使用 扫描器类 并尝试使用 BufferedReader类 . 提示2: 尝试使用 StringBuffer类 以防需要打印大量数据。
让我们来回答一个问题 Geeksforgek的实践 并解决这个问题: 问题: 分隔0、1和2的数组 简而言之,问题是,给定一个0、1和2的数组。我们必须将数组开头的所有0、数组中间的所有1和数组最后的所有2分开。 例如:
Input : 1 1 2 0 0 2 1Output : 0 0 1 1 1 2 2
方法: 分隔0、1和2的数组 以下是上述方法的实施情况:
JAVA
// 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--; } } } } |
根据我们的期望,它应该通过所有测试用例,并在 Geeksforgek的实践 .但是,当我们在Geeksforgeks IDE上提交这段代码时,它显示了TLE。
这意味着我们已经超出了预期的时间限制。这不是问题,让我们使用提示 如上所述 .
- 使用BufferedReader获取输入。
- 使用StringBuffer保存和打印输出。
方法: 分隔0、1和2的数组 下面是隔离0、1和2的Java代码的实现
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问题?看起来很简单:)。 你现在可以试试 .