[leetcode-525-Contiguous Array]

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

思路:

将0改为-1,将原题目改成求最大连续区间,区间内元素和为0。用map记录当前元素 j 和之前所有元素的和与下标,当map

中存在相同的sum时,说明之前i到j的区间元素和为0。

int findMaxLength(vector<int>& nums)
    {
        for (auto& a:nums) if (a == 0)a = -1;
        map<int, int>mp;
        mp[0] = -1;
        int sum = 0,ret =0;
        for (int i = 0; i < nums.size();i++)
        {
            sum += nums[i];
            if (mp.count(sum))ret = max(ret, i - mp[sum]);
            else mp[sum] = i;
        }
        return ret;
    }

参考:

http://www.cnblogs.com/liujinhong/p/6472580.html

时间: 2024-07-31 14:26:46

[leetcode-525-Contiguous Array]的相关文章

525. Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Outp

LeetCode 525: Continuous Array

Note: 1. Remember to intial (0, -1) since this kind of problems need a starting point. class Solution { public int findMaxLength(int[] nums) { if (nums.length < 2) { return 0; } for (int i = 0; i < nums.length; i++) { if (nums[i] == 0) { nums[i] = -

[LeetCode] Merge Sorted Array [22]

题目 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 ar

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

LeetCode: Convert Sorted Array to Binary Search Tree [108]

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树. [思路] 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点. 则使用二分法来解本题即可. [代码] /** * Definition for binary tree *

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

[LeetCode] Contiguous Array 邻近数组

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. Example 1: Input: [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. Example 2: Input: [0,1,0] Outp

[LeetCode]189.Rotate Array

题目 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

LeetCode——Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 原题链接:https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题目:给定一个升序排列元素的数组,将其转换成高度平衡的二叉树. 思路:已经排序.则从中间劈开,中间元素为树的根,左右递归构建. public

[LeetCode] Sort Transformed Array 变换数组排序

Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array. The returned array must be in sorted order. Expected time complexity: O(n) Example: nums = [-4, -2,