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 ...
随机推荐
- Docker入门之 - 更换源为国内源,实现快速下载image
原文:Docker入门之 - 更换源为国内源,实现快速下载image 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012055638/artic ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Spark源代码阅读笔记之DiskStore
Spark源代码阅读笔记之DiskStore BlockManager底层通过BlockStore来对数据进行实际的存储.BlockStore是一个抽象类,有三种实现:DiskStore(磁盘级别的持 ...
- 利用PS把多张psd格式的图片转换为一张PDF格式
最近为公司做了一版电子样册,所有图片都是包含多图层高清晰的psd格式,要做成一个PDF文件的电子样册,发给客户看,面对这些零散的图片,本来打算利用在线合成:在线网址 https://smallpdf. ...
- Android 带清除功能的输入框控件EditTextWithDel
记录下一个非常有用的小控件EditTextWithDel.就是在Android系统的输入框右边增加一个小图标.点击小图标能够清除输入框里面的内容,由于Android原生EditText不具备此功能,所 ...
- 设置statusBar状态栏颜色
设置statusBar的[前景部分] 简单来说,就是设置显示电池电量.时间.网络部分标示的颜色, 这里只能设置两种颜色: 默认的黑色(UIStatusBarStyleDefault) 白色(UISta ...
- 【codeforces 750F】New Year and Finding Roots
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 777D】Cloud of Hashtags
[题目链接]:http://codeforces.com/contest/777/problem/D [题意] 让你通过删除字符串的末尾字符的方式; 使得所给的字符串以字典序升序排序; 不能交换字符串 ...
- Java反射xml数据类
我们做自己的自动化测试.遇到使用xml存储数据,然而,这些数据可以被封装成一个类.将数据传递.通过下面的实际例子,展示给大家.请欣赏. 第一步:xml数据存储将被使用 第二步:读取xml文件的方法 第 ...
- Android菜鸟的成长笔记(22)——Android进程间传递复杂数据(AIDL)
在上一篇中介绍了Andorid中的进程间的通信方式AIDL,本篇文章将介绍传递复杂数据的AIDL Service 下面通过一个示例说明: 本例子中用到了两个自定义类型:Person与Pet, 其中Pe ...