#include <iostream>
#include <vector>using namespace std;// 不是一直在查找中值,而是一直查找前k个值,每次去掉一部分值,k也相应减小,直到减到1为止,这样就可以算出结果
// time complexity O(log(m+n))  space complexity O(log(m+n))
class Solution
{
public:double findMedianSortedArrays(const vector<int>& A, const vector<int>& B){const int m=A.size();const int n=B.size();int total = m+n;if (total & 0x1)          // 按位与, 判断奇偶    total为奇数return find_kth(A.begin(), m, B.begin(), n, total/2+1);          // 奇数找到中间值就行else                      // total 为偶数return (find_kth(A.begin(), m, B.begin(), n, total/2)+find_kth(A.begin(), m, B.begin(), n, total/2+1))/2.0;   //  偶数找到中间的两个值/2.0}private:static int find_kth(std::vector<int>::const_iterator A, int m, std::vector<int>::const_iterator B, int n, int k)  //调用这个函数不会访问或者修改任何对象(非static)数据成员{//always assume that m is equal or smaller than nif (m>n)return find_kth(B, n, A, m, k);if (m==0)return *(B+k-1);      // B的medianif (k==1)return min(*A, *B);   // 找最小值, 这个排序从小往大排序,第一位就是最小值 已经能定位中值了//divide k into two partsint ia=min(k/2, m), ib=k-ia;   // 在这可以看出为啥假设m is equal or smaller than n   A中取k/2或是全取, 然后B中取补齐k个元素if (*(A+ia-1)<*(B+ib-1))       // 比较所取的最大值, 如果最大值A<B,则将A的前ia个数抛弃, A>B,则抛弃B的前ib个值,如果两个值相等,则找到了第k个值return find_kth(A+ia, m-ia, B, n, k-ia);else if(*(A+ia-1) > *(B+ib-1))return find_kth(A, m, B+ib, n-ib, k-ib);elsereturn A[ia-1];}
};int main()
{vector<int> a = {1,3};vector<int> b = {2};Solution s;double result;result = s.findMedianSortedArrays(a, b);cout << result << endl;
//    cout << (1&0x1) << endl;
//    cout << (2&0x1) << endl;
}

转载于:https://www.cnblogs.com/o-v-o/p/9979922.html

leetcode find kth相关推荐

  1. 剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  2. LeetCode 230. Kth Smallest Element in a BST--C++,Python解法--面试真题--找二叉树中第K小的元素

    题目地址:Kth Smallest Element in a BST - LeetCode Given a binary search tree, write a function kthSmalle ...

  3. LeetCode 215. Kth Largest Element in an Array--数字第K大的元素--最大堆或优先队列--C++,Python解法

    题目地址:Kth Largest Element in an Array - LeetCode Find the kth largest element in an unsorted array. N ...

  4. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  5. leetcode 230. Kth Smallest Element in a BST | 230. 二叉搜索树中第K小的元素(Java)

    题目 https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 题解 方法1:中序遍历+剪枝 import java.util.Arra ...

  6. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array...

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  7. Leetcode - 230. Kth Smallest Element in a BST (BST)

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. LeetCode 215 Kth Largest Element in an Array

    思路: 跟找第kth小的数相反,数组逆向排序.从前找第k - 1个数. 1 public class Solution { 2 public int findKthLargest(int[] arra ...

  9. leetcode 779. K-th Symbol in Grammar | 779. 第K个语法符号(Java)

    题目 https://leetcode.com/problems/k-th-symbol-in-grammar/ 题解 思考 15 分钟,编码 1 分钟. 其实目标位置在哪一行不重要,因为每一行都有相 ...

  10. leetcode 668. Kth Smallest Number in Multiplication Table | 668. 乘法表中第k小的数(二分查找)

    题目 https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/ 题解 看了答案 Approach #3: B ...

最新文章

  1. 线程池的介绍及简单实现
  2. tomcat会自动解压webapps目录下的war包
  3. Zabbix 2.4.5 自定义 key 监控 apache
  4. Python中*args 和**kwargs的用法
  5. 搜狗浏览器缓冲区溢出漏洞EXP
  6. python爬虫JS逆向之人口流动态势大数据
  7. Graph Convolutional Networks 图卷积网络
  8. matlab abs()le8,《MATLAB_语言与数学运算》练习答案
  9. 中国移动苏州研发中心前端笔试(2)
  10. 十天征服单片机百度云_郭天祥十天征服单片机视频下载地址
  11. TOGAF 10 正式发布
  12. 【数字IC】深入浅出理解UART协议
  13. ACL2021_Lexicon Enhanced Chinese Sequence Labelling Using BERT Adapter
  14. mysqldump参数之-A -B
  15. cf446 div2
  16. 大学生html5实训报告,大学生实训心得体会范文(精选5篇)
  17. 计网实验总结一:路由器配置
  18. 1062: [NOI2008]糖果雨 - BZOJ
  19. 使用云开发数据库构建更生动的小程序
  20. GD25Qxxx使用笔记

热门文章

  1. 吊炸天!程序员定制版高效摸鱼神器...
  2. java bean prototype_Spring原型bean-prototype不能销毁? 转载
  3. 10月区块链领域投融资事件盘点
  4. 计算机图形学浙江大学第一章图形学概述
  5. 史上最通俗易懂的ASM教程
  6. 嵌入式开发 面试问答
  7. STM32F767/429-CAN通信实验
  8. centos 安装 redis
  9. 深度学习课程资源整理
  10. CSS 设置垂直居中