Skip to main content

800.数组元素目标和

思路

info

双指针什么时候从一头一尾开始遍历,什么时候同时从起点开始遍历?

代码

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int n, m, x;
int a[N], b[N];

int main() {

cin >> n >> m >> x;

for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < m; i++) cin >> b[i];

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) cout << i << " " << j << endl;
}

return 0;
}