leetcode-题5--优化
#include <iostream> #include <vector> #include <algorithm> using namespace std; //判断两个有序数组的第k个大的数 int dfs(int *a, int m, int *b, int n, int k) { if (m > n)return dfs(b, n, a, m, k); if (m == 0)return b[k - 1]; if (k == 1)return min(a[0], b[0]); int ai = min(k / 2, m), bi = k - ai; if (a[ai-1] < b[bi-1]) { return dfs(a + ai, m - ai, b, n, k - ai); } if (a[ai-1] > b[bi-1]) { return dfs(a, m, b + bi, n - bi, k - bi); } if (a[ai] == b[bi]) return a[ai]; } int main() { int a[5] = { 1,3,5,6,7 }; int b[5] = { 2,3,4,6,7 }; int res = dfs(a, 5, b, 5, 5); cout << res << endl; system("pause"); return 0; }