LeetCode - 4 - Longest Substring Without Repeating Characters
题目
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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- 解决Eclipse 启动后总是Building WorkSpace(sleeping)
打开eclipse后eclipse总是在Building WorkSpace(sleeping),我的解决方案是,Project -> Building AutoMatically关闭就好了,以 ...
- Java8获取参数名及Idea/Eclipse/Maven配置
在Java8之前,代码编译为class文件后,方法参数的类型固定,但是方法名称会丢失,方法名称会变成arg0.arg1.....而现在,在Java8开始可以在class文件中保留参数名,这就给反射带来 ...
- MySQL建立双向主备复制server配置方法
1.环境描写叙述 serverA(主) 192.85.1.175 serverB(从) 192.85.1.176 Mysql版本号:5.1.61 系统版本号:System OS:ubuntu 10.1 ...
- [02]基于webservice权限系统
面前,我们已经介绍过.如何使用cxf建立webservice. 那我们要介绍的这款许可制度表结构. 此权限管理系统分为部门管理.员工管理.角色管理,权限管理,授权的人才和企业管理(这里不涉及) 角色管 ...
- 图解linux启动过程
概述 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvWUVZVUFOR0VO/font/5a6L5L2T/fontsize/400/fill/I0JBQkF ...
- 卷积神经网络(CNN)的细节问题(滤波器的大小选择)
0. 滤波器的大小选择 大部分卷积神经网络都会采用逐层递增(1⇒ 3 ⇒ 5 ⇒ 7)的方式. 每经过一次池化层,卷积层过滤器的深度都会乘以 2: 1. 权值共享:减轻过拟合 & 降低计算量 ...
- Spring处理跨域请求
[nio-8080-exec-8] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from s ...
- Linux的设备文件名与硬盘分区已经挂载点的关系
以CentOS6.3为例. 选择的硬盘设备名是/dev/sda,即第一块STAT硬盘,然后在该硬盘分了3个主分区和1个扩展分区,设备名分别是/dev/sda1,/dev/sda2,/dev/sda3, ...
- imageNamed 与 imageWithContentsOfFile差额
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 普通情况下 ...
- vue props 传输数值或boolean
字面量语法 vs 动态语法 初学者常犯的一个错误是使用字面量语法传递数值: <!-- 传递了一个字符串"1" --> <comp some-prop=" ...