【leetcode】525. Contiguous Array】的更多相关文章

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetcode.com/problems/contiguous-array/description/ 题目描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1…
题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找到在0->i-1区间内差值也为count的最小下标j,那么j->i区间就是一个0和1数量相同的区间.遍历过程中记录最大的j->i长度即可得到答案. 代码如下: class Solution(object): def findMaxLength(self, nums): ""…
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/split-array-into-consecutive-subsequences/description/ 题目描述: You are g…
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partition-array-into-disjoint-intervals/description/ 题目描述: Given an array A, partition it into two (contiguous) subarrays left and right so that: Every elemen…
题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at most K.  After partitioning, each subarray has their values changed to become the maximum value of that subarray. Return the largest sum of the given arr…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/ 题目描述 Given an array A of integers, return true if and only if we can partition the array…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://leetcode.com/problems/circular-array-loop/ 题目描述 You are given a circular array nums of positive and negative integers. If a number k at an index is posi…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:https://leetcode.com/problems/beautiful-array/description/ 题目描述 For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2,…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: https://leetcode.com/problems/sort-array-by-parity-ii 题目描述 Given an array A of non-negative integers, half of the integers in A are odd, and half of…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leetcode.com/problems/rotate-array/description/ 题目描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array […
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 自定义sorted函数的cmp 日期 题目地址:https://leetcode.com/problems/sort-array-by-parity/description/ 题目描述: Given an array A of non-negative integers, return an array consisting of all the…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/monotonic-array/description/ 题目描述 An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone…
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B…
Product of Array Except Self Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2…
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.   每次把中间元素当成根节点,递归即可   /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * Tre…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路:对于树来说,自顶向下的递归是很好控制的,自底向上的递归就很容易让脑神经打结了.本来想仿照排序二叉树的中序遍历,逆向地由数组构造树,后来发现这样做需要先确定树的结构,不然会一直递归下去,不知道左树何时停止.代码: TreeNode *addNode(vector<int> &num, int…
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 思路: 归并的一部分,比Merge Tw…
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro…
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 题意要把两个有序的数组合并到他们…
这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n. 你可以假设 nums1 有足够的空间(空间大小大于或等于 m + n)来保存 nums2 中的元素. 示例: 输入: nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 输出: [1,2,2,…
题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbersReturn True if its possible otherwise return False. Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Ou…
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these msubarrays. Note:If n is the length of array, ass…
题目如下: Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with the given length.  Initially, each element equals 0. void set(index, val) sets the element at the given ind…
题目如下: Given an array A of integers, return true if and only if we can partition the array into three non-emptyparts with equal sums. Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2]…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 日期 题目地址:https://leetcode-cn.com/problems/split-array-with-equal-sum/ 题目描述 Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ 题目描述 Given a string S of digits, such as S = "123456579", we can split it into a Fib…
题目如下: Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing. In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do th…
题目如下: 解题思路:本题可以维护三个字典,dic_1保存没有组成序列的单元素,dic_2保存组成了包含两个元素的序列中的较大的元素,dic_3保存组成了包括三个或者三个以上元素的序列中的最大值.因为合法的序列至少要三个元素,解题的关键是要使得dic_2和dic_1的元素尽快满足条件.对于任意一个还没有加入字典的元素,加入字典的优先级从高到低排序分别是dic_2 > dic_1 > dic_3,即优先匹配dic_2,接下来匹配dic_1,再就是dic_3,如果三个都不满足,说明在目前条件下市落…