`
notlcry
  • 浏览: 1946 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

LeetCode-(2) Median of Two Sorted Arrays

阅读更多

问题描述:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

思路:
1.     在2个有序的数组中找中值,那么可以把2个数组合并,然后直接去中值就可以,时间复杂度为O(m+n)/2。因为题目没看清,以为是O(m+n)。所以写了个简单实现,结果还accpect了。
2.     如果要O(log(m+n)),直接的想法就是二分法。
下面上花了几天想,2个小时写出来的计算和证明过程。
 

 

 

 
总结:
1.     一般log(n)的方法,可以优先考虑二分法。
2.     一定要通过严格的数学证明,才能保证算法的正确性,在没有证明之前,通过脑子想(当然自己脑子差)会有很多错误,导致我提交了好多错误的答案,导致这道题的成功率极低。
3.     学会了反证法。用反证法来证明算法的正确性。
4.     通过归纳法来得到一般规律,并且得到递归到最小处理处理的情况。
5.     在需要列举情况的时候,需要用到排列组合,来对所有情况进行判断。(高中的数学啊,还是需要加强)
 
下面上代码:(流程还没有优化,可以进入不合并一些流程)
 
 
 

 

  • 大小: 652.1 KB
  • 大小: 881 KB
  • 大小: 1 MB
分享到:
评论

相关推荐

    LeetCode4 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java AC版本

    leetcode无法登录-MedianOfTwoSortedArrays:双排序数组的中位数

    leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 不能都为空。 ...

    leetcode接口-leetcodeHelper:leetcodeHelper

    leetcode 接口 该项目可帮助您使用首选的 IDE 或带有命令行界面 (CLI) 的编辑器来执行 leetcode。 入门 ...leetcode ...leetcode ...two-sum leetcode ...median-of-two-sorted-arrays 然后问题描述和启动代码将自动

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...

    leetcode-master.zip

    4. "004_median_of_two_sorted_arrays":这个题目是“寻找两个有序数组的中位数”。需要在两个已排序的数组中找到中位数,如果两个数组合并后的数组有偶数个元素,中位数就是中间两个数的平均值。这个问题通常用二分...

    leetcode4median-leetcode4-median-:leetcode4-中位数-

    在LeetCode上,题目4(Median of Two Sorted Arrays)是一个经典的算法问题,旨在寻找两个已排序数组的中位数。中位数是将一组数值从小到大排列后处于中间位置的数,在奇数个数的情况下是中间的那个数,而在偶数个数...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    lrucacheleetcode-leetcode-1:leetcode-1

    2. Add Two Numbers 链表求和,哨兵节点 3. Longest Substring Without Repeating Characters 最长没有重复字符的子序列 记录各字符最近一次出现的位置 4. Median of Two Sorted Arrays 求两有序数列的中位数,可...

    leetcode实现strstr-leetcode-js:js刷leetcode

    leetcode实现strstr leetcode-js 记录刷leetcode分析过程,希望一点点进步! leetcode地址 刷题顺序 按照顺序刷第一遍,记录实现思路,自己的优化方案,并研究高票大佬的思路。 已完成题目归档 序号 题目 题解 实现...

    程序员面试宝典LeetCode刷题手册

    4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...

    leetcode中国-LeetCode-Swift:LeetCode算法题Swift实现

    leetcode中国 LeetCode-Swift LeetCode算法题Swift实现 从2020年6月10日开始挑战每日刷算法,至少完成一道,让我们拭目以待! No. Title Chinese Difficulty Router 0001 Two Sum Easy 0002 Add Two Numbers Medium ...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    Median of Two Sorted Arrays 5 Longest Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove ...

    蓄水池算法leetcode-leetcode:Python中leetcode问题的解决方法

    蓄水池算法 leetcode Leetcode Solutions Continue updating... lt: leetcode jz: 剑指Offer Sort & Search # Name ...2 ...Two ...lt.2 ...Two ...Median of Two Sorted Arrays hard [python] lt.5 Longest Pa

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 详情 # Title Solution Time Space Difficulty 1 Two ...

    约瑟夫环leetcode-LeetCodeAlgorithm:leetCode的算法

    Median of Two Sorted Arrays 要求O(logN) 5 实现 strStr() kmp算法 30 串联所有单词的子串 不懂 36 有效的数独 不懂 37 解数独 不懂 109 要求使用OlogN复杂度 经典题 数学题 序列号 题目名称 题目难点 89 gray code...

    javalruleetcode-Leetcode:力码解决方案

    2 Add Two Numbers (两数相加) Medium Linked List 3 Longest Substring Without Repeating Characters (无重复字符的最长子串) Medium Dual Pointer (Sliding Window), Hash Table 4 Median of Two Sorted Arrays ...

    leetcodepython001-algorithm:leetcode问题(cpp、java、python),书籍破解_the_coding

    leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...

    leetcode分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

Global site tag (gtag.js) - Google Analytics