题目描述
数组元素的目标和
思想
双指针
java 代码
import java.util.*;
import java.io.*;
class Main{
static int N = 100010;
static int[] a = new int[N], b = new int[N];
public static void main(String args[])throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int[] s = Arrays.asList(in.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
int n = s[0];
int m = s[1];
int x = s[2];
a = Arrays.asList(in.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
b = Arrays.asList(in.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
//a 数组 指针 从头开始
//b 数组 指针 从尾开始
for (int i = 0, j = m-1; i < n; i++){
while(j>0 && a[i]+b[j] > x ){
j--;
}
if(j>0 && a[i] + b[j] == x){
System.out.print(i + " " + j);
}
}
}
}