import java.util.Scanner;
class d478{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
while(total-- > 0){
int existCount = 0;
int numberList[] = new int[totalOfNumber];
for(int index = 0;index < totalOfNumber;index++){
numberList[index] = scanner.nextInt();
}
for(int time = 0;time < totalOfNumber;time++){
int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
while(low <= high){
int middle = (low + high) / 2;
if(numberList[middle] == number){
++existCount;
break;
}else if(numberList[middle] > number){
high = middle - 1;
}else{
low = middle + 1;
}
}
}
System.out.println(existCount);
}
}
}
請問如何優化
import java.util.Scanner;
class d478{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
while(total-- > 0){
int existCount = 0;
int numberList[] = new int[totalOfNumber];
for(int index = 0;index < totalOfNumber;index++){
numberList[index] = scanner.nextInt();
}
for(int time = 0;time < totalOfNumber;time++){
int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
while(low <= high){
int middle = (low + high) / 2;
if(numberList[middle] == number){
++existCount;
break;
}else if(numberList[middle] > number){
high = middle - 1;
}else{
low = middle + 1;
}
}
}
System.out.println(existCount);
}
}
}
請問如何優化
己改變了思考方法,還是TLE。
是不是利用JAVA 問題。。。。。。
import java.util.Scanner;
class d478{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
while(total-- > 0){
int existCount = 0;
int numberList[] = new int[totalOfNumber];
for(int index = 0;index < totalOfNumber;index++){
numberList[index] = scanner.nextInt();
}
for(int time = 0;time < totalOfNumber;time++){
int number = scanner.nextInt(),low = 0,high = totalOfNumber - 1;
while(low <= high){
int middle = (low + high) / 2;
if(numberList[middle] == number){
++existCount;
break;
}else if(numberList[middle] > number){
high = middle - 1;
}else{
low = middle + 1;
}
}
}
System.out.println(existCount);
}
}
}
己改變了思考方法,還是TLE。
是不是利用JAVA 問題。。。。。。
import java.util.Scanner;
class d478{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
while(total-- > 0){
int existCount = 0;
int numberList[] = new int[totalOfNumber];
for(int index = 0;index < totalOfNumber;index++){
numberList[index] = scanner.nextInt();
}
for(int time = 0,pointer = 0;time < totalOfNumber;time++){
int number = scanner.nextInt();
for(int index = pointer;index < totalOfNumber;index++){
if(number == numberList[index]){
existCount++;
pointer++;
break;
}else if(number > numberList[index]){
pointer++;
continue;
}
}
}
System.out.println(existCount);
}
}
}
己改變了思考方法,還是TLE。
是不是利用JAVA 問題。。。。。。
對不起,這才是最新的CODE
import java.util.Scanner;
class d478{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int total = scanner.nextInt(),totalOfNumber = scanner.nextInt();
while(total-- > 0){
int existCount = 0;
int numberList[] = new int[totalOfNumber];
for(int index = 0;index < totalOfNumber;index++){
numberList[index] = scanner.nextInt();
}
for(int time = 0,pointer = 0;time < totalOfNumber;time++){
int number = scanner.nextInt();
for(int index = pointer;index < totalOfNumber;index++){
if(number == numberList[index]){
existCount++;
pointer++;
break;
}else if(number > numberList[index]){
pointer++;
continue;
}else{
break;
}
}
}
System.out.println(existCount);
}
}
}