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 ...
随机推荐
- 【t070】二进制
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 求所有可以只用1和00拼成的长度为N的二进制数的个数除以15746的余数. 比如当N=4的时候,有5个 ...
- window对象属性alert、confirm、prompt怎么使用?
window对象属性alert.confirm.prompt怎么使用? 一.总结 1.参数依次复杂,返回值依次复杂,但是感觉都是一一继承的,所以很好想也很好写. 二.window对象alert.con ...
- js匿名自执行函数
匿名自执行函数:没有方法名的函数闭包:闭包是指有权访问另一个函数作用域变量的函数: 通过一个实例来解释: 从网上找到了一个案例,使用了for循环.匿名自执行函数.setTimeout. 案例1: va ...
- Android View框架的measure机制
概述 Android中View框架的工作机制中,主要有三个过程: 1.View树的測量(measure)Android View框架的measure机制 2.View树的布局(layout) Andr ...
- Atom编辑器折腾记_(13)JS代码智能提示补全(插件:atom-ternjs)
题外话 官方正式版尽管内置了.autocomplete-plus;最为明显的一个功能就是记忆你已经输入过的名称进行匹配; 可是针对于某些语言来说,还是有些不足的-.当中JS的补全上就明显不足了-所以须 ...
- 【codeforces 765B】Code obfuscation
[题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...
- X Window 简单的新手教程
1.X Window 系统的窗体显示原理: http://www.ibm.com/developerworks/cn/linux/l-cn-xwin/ 2.X Window 程式设计入门--第二章 X ...
- spark 基于key排序的wordcount
java /** * 根据单词次数排序的wordcount * @author Tele * */ public class SortWordCount { private static SparkC ...
- Power control within a coherent multi-processing system
Within a multi-processing system including a plurality of processor cores 4, 6operating in accordanc ...
- 深入浅出MFC:对话框消息路由
[appmodul.cpp] extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstanc ...