题目

URL:https://leetcode.com/problems/median-of-two-sorted-arrays/

解法

二分法。

总的思想是将 2 个数组用 2 个指针“整体”二分。具体来说调整 2 个二分指针的位置,达到:

  • 左半部分 size = 右半部分 size (2 个数组大小和为奇数可以相差 1)
  • 划分左面的数值 < 划分右面的数值,由于两个数组都为有序数组,只需要保证划分边界的值左下角小于右上角以及左上角小于右下角。

在这个过程中,注意:

  • 划分数组的长度,通常后一个数组长,方便处理。
  • 注意划分边界,到了划分边界,说明划分已完成(不能再划分了),接下来只需要处理值。

划分完成后取划分值,注意划分取值:

  • 若划分左面值不存在,忽略即可。
  • 奇数,只需要去划分左面的最大值。
  • 偶数,取划分左面最大值和划分右面最小值。
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums1.length > nums2.length) return findMedianSortedArrays(nums2, nums1); int imin = 0, imax = nums1.length, i = 0, j = 0;
while (imin <= imax) {
i = (imin + imax) / 2;
j = (nums1.length + nums2.length + 1) / 2 - i;
if (i > 0 && nums1[i - 1] > nums2[j]) {
imax = i - 1;
} else if (i < nums1.length && nums1[i] < nums2[j - 1]) {
imin = i + 1;
} else {
break;
}
} int maxLeft;
if (i == 0) {
maxLeft = nums2[j - 1];
} else if (j == 0) {
maxLeft = nums1[i - 1];
} else {
maxLeft = Math.max(nums1[i - 1], nums2[j - 1]);
}
if ((nums1.length + nums2.length) % 2 != 0) return maxLeft; int maxRight;
if (i == nums1.length) {
maxRight = nums2[j];
} else if (j == nums2.length) {
maxRight = nums1[i];
} else {
maxRight = Math.min(nums1[i], nums2[j]);
} return (double) (maxLeft + maxRight) / 2;
}

二分法,时间复杂度O(log2(m+n)),运行时间约为 70 ms。

总结

很难很经典,对于二分查找领域来说,又产生了一个新的高度:不再是一个数组的二分查找,而是多个数组的二分查找。

LeetCode - 4 - Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  8. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  10. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. poj2151之概率DP

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4403   ...

  2. [Angular] Adding keyboard events to our control value accessor component

    One of the most important thing when building custom form component is adding accessbility support. ...

  3. iOS 取消多余tableView的横线的写法

    - (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...

  4. 51nod1673 树有几多愁 - 贪心策略 + 虚树 + 状压dp

    传送门 题目大意: 给一颗重新编号,叶子节点的值定义为他到根节点编号的最小值,求所有叶子节点值的乘积的最大值. 题目分析: 为什么我觉得这道题最难的是贪心啊..首先要想到 在一条链上,深度大的编号要小 ...

  5. TensorFlow 需注意的细节问题

    1. 数据类型 不带小数点的数默认为 int32,带小数点的数默认为 float32:

  6. [MFC]SDI在图片背景上实现文本跟随鼠标移动

    SDI是单文档接口应用程序的简称.本文要实现的是在视图区域显示一张图片,然后在图片表层显示文字,并且文字跟随鼠标移动.思考一下,可以判断这个问题一共分为以下几个部分:1.显示图片:2.找到鼠标的位置: ...

  7. VS版本 与 .NET版本以及C#版本之间的关系

    版本 .NET Framework版本 Visual Studio版本 发布日期 特性 C# 1.0 .NET Framework 1.0 Visual Studio .NET 2002 2002.1 ...

  8. 利用jQuery传送json格式的字符串,后端用ashx文件来接收

    在Default.aspx里面,我们会透过javascript建立两个物件,分别有Name和Age的属性,再透过Array的方式,将这两个物件塞到Array里面去.使用Ajax内建的$.ajax AP ...

  9. PS 滤镜算法— — 表面模糊

    图像的表面模糊处理,其作用是在保留图像边缘的情况下,对图像的表面进行模糊处理.在对人物皮肤处理上,比高斯模糊更有效.因为高斯模糊在使人物皮肤光洁的同时,也将一些边缘特征如脸部的眉毛.嘴唇等给模糊了,不 ...

  10. QT调用VC DLL的例子(所有源码)

    http://blog.csdn.net/zhuce0001/article/details/20651025 http://blog.csdn.net/zhuce0001/article/detai ...