题目

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. php对浮点数小数取整,php除法取整数

    如果我们使用" / "操作符进行除法运算时,如果遇到无法除尽的情况,会得到小数值.如果我只希望得到整数部分,怎么办呢? 1.round — 对浮点数进行四舍五入 float rou ...

  2. Tomcat结合Apache、Nginx实现高性能的web服务器

    一.Tomcat为什么需要与apache.nginx一起结合使用? Tomcat虽然是一个servlet和jsp容器,但是它也是一个轻量级的web服务器.它既可以处理动态内容,也可以处理静态内容.不过 ...

  3. Android java.lang.IllegalStateException: Already logged in to server.

    今晚在搞openfire时,无意中发现了这样的一个问题:问题描述: java.lang.IllegalStateException: Already logged in to server.原因:wh ...

  4. hibernate框架简单步骤

    Demo.java package com.itheima.test; import org.hibernate.Session; import org.hibernate.SessionFactor ...

  5. &quot;Swift&quot;编程语言

    来自英文文档.百度翻译以及自己没过4级的渣渣英语功底,为了自己以后看起来方便 About Swift 关于"海燕" IMPORTANT 重要 This is a prelimina ...

  6. Oracle成长点点滴滴(2)— 权限管理

    权限管理中权限包含系统权限以及对象权限.在解说权限管理之前我们先来了解用户的创建以及授权这些前提. 1.      创建用户以及授权 Ø  默认用户 既然提到了创建用户,首先必须先把用户的知识攻克了. ...

  7. 一入Python深似海--print

    先给大家来个干货^~^,学习Python的一个好站点,http://learnpythonthehardway.org/book/ 经典样例 以下是几个老经典的样例喽,刚接触Python的能够敲一敲, ...

  8. easyexcel 读写测试

    <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId> ...

  9. rac下一个/tmp/bootstrap权限问题

    原创作品,离 "深蓝blog" 博客,欢迎转载,请务必注明转载如下源,否则追究其版权责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...

  10. 【38.46%】【codeforces 615E】Hexagons

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...