本文部分参考Discuss: LeetCode. 步骤1. 选择数组的中间元素. 最大子序列有两种可能: 包含此元素/不包含. 步骤2. 步骤2.1 如果最大子序列不包含中间元素, 就对左右子序列进行步骤1. 步骤2.2 如果最大子序列包含, 则结果很简单, 就是左子列的最大后缀子列(即包含左子列最后一个元素--中间元素)加上右子列的最大前缀子列(即包含右子列第一个元素--中间元素) 步骤3. 返回三者中的最大值(左子列最大值, 右子列最大值, 二者拼接最大值). class Solution…
题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b]$ 之间的最大连续和. 要你计算出这个这个区间内最大连续和的区间 $[x,y](a \le x \le y \le b)$: 思路: 1.构造线段树,其中每个结点维护3个值,最大连续子列和 $max\_sub$,最大前缀和 $max\_prefix$,最大后缀和 $max\_suffix$. 具体来…
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 31021    Accepted Submission(s): 13922 Problem Description给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j &…
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ \ 3 2 1…
平民解法: 既然是找最小数组,那就得到一个排序好的数组,然后直接和初试数组比对,用一个left,right分别记录从最初开始不同,到最后不同的小标,最后左右做差再加一,就能得到长度. 其他解法: 双指针 + 线性扫描另外一个做法是,我们把整个数组分成三段处理. 起始时,先通过双指针 ii 和 jj 找到左右两次侧满足 单调递增 的分割点. 即此时 [0, i][0,i] 和 [j, n)[j,n) 满足升序要求,而中间部分 (i, j)(i,j) 不确保有序. 然后我们对中间部分 [i, j][…
题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L, R], 那么 nums[L] 一定大于区间内的最小值,而 nums[R] 一定大于区间内的最大值, 按照这个特性分别求出左右端点即可. 代码如下: const int inf = 0x7fffffff; class Solution { public: int findUnsortedSubar…
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. E…
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order, too. You need to find the shortest such subarray and output its length. E…
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. LeetCode581. Shortest Unsorted Continuous Subarray 示例 1: 输入: [2, 6, 4, 8, 10, 9, 15] 输出: 5 解释: 你只需要对 [6, 4, 8,…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetcode-cn.com/problems/check-if-all-1s-are-at-least-length-k-places-away/ 题目描述 给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 l…