leetcode525. 连续数组 python
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组。
示例 1:
输入: [0,1]
输出: 2
说明: [0, 1] 是具有相同数量0和1的最长连续子数组。
示例 2:
输入: [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组 Python(超时)
class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
""" dict_nums = {0:0,1:0}
max_len = 0
len_nums = len(nums)
for j in range(len_nums):
for i in nums[j:]:
if i == 0:
dict_nums[0] += 1
elif i == 1:
dict_nums[1] += 1
if dict_nums[0] == dict_nums[1]:
max_len = max(max_len,dict_nums[0]+dict_nums[1])
dict_nums = {0: 0, 1: 0}
return (max_len)
时间复杂度改为O(n),通过
class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
sums = [0] * len(nums)
dmap = collections.defaultdict(int)
last = 0
for i, n in enumerate(nums):
last += 2 * nums[i] - 1
sums[i] = last
dmap[last] = max(dmap[last], i)
ans = 0
for i, m in enumerate(sums):
if m == 0:
ans = max(ans, i + 1)
else:
ans = max(ans, dmap[m] - i)
return ans
leetcode525. 连续数组 python的更多相关文章
- [Swift]LeetCode525. 连续数组 | Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- SQL Server 2008 R2——查找最小nIndex,nIndex存在而nIndex+1不存在 求最小连续数组中的最大值
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...
- Leetcode 525.连续数组
连续数组 给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组. 示例 1: 输入: [0,1] 输出: 2 说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2: ...
- Java实现 LeetCode 525 连续数组
525. 连续数组 给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组(的长度). 示例 1: 输入: [0,1] 输出: 2 说明: [0, 1] 是具有相同数量0和1的最长连续 ...
- 525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...
- 剑指offer-数字在排序数组中出现的次数-数组-python
题目描述 统计一个数字在排序数组中出现的次数. python 内置函数 count()一行就能搞定 解题思路 二分查找到给定的数字及其坐标.以该坐标为中点,向前向后找到这个数字的 始 – 终 ...
- 剑指offer-连续子数组的最大和-数组-python
题目描述 例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止). 给一个数组,返回它的最大连续子序列的和 思路:动态规划 # -*- coding:u ...
- LeetCode--128--最长连续序列(python)
给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2]输出: 4解释: 最长连续序列是 [1, 2, 3, ...
- LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array2 搜索旋转排序数组 中文: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0 ...
随机推荐
- 使用***客户端和Privoxy让所有CentOS 7命令行工具通过代理访问互联网(转载)
安装*** 首先安装pip: curl -LO "https://bootstrap.pypa.io/get-pip.py" python get-pip.py 通过pip安装** ...
- mysql进阶练习
一 . MySQL进阶练习 /*==========================创建班级表=============================*/ CREATE TABLE class ( ...
- Unicode编码,解释UCS、UTF、BMP、BOM等名词
(转载 谈谈Unicode编码,简要解释UCS.UTF.BMP.BOM等名词 这是一篇程序员写给程序员的趣味读物.所谓趣味是指可以比较轻松地了解一些原来不清楚的概念,增进知识,类似于打RPG游戏的升级 ...
- echart全局属性,自用,搜索比较快
// 全图默认背景 // backgroundColor: ‘rgba(0,0,0,0)’, // 默认色板 color: ['#ff7f50','#87cefa','#da70d6','#32cd ...
- <constant name="struts.devMode" value="true" />
<constant name="struts.devMode" value="true" /> 当vlaue为true,表示struts处于开发模式 ...
- hdu 2491 贪心
#include<stdio.h> #include<stdlib.h> #define N 110000 struct node { int u,v,len,time; }m ...
- [ASP.NET]asp.net动态加载用户控件
用户控件 // 用户控件源码 namespace wzjr.control { public partial class Topic : System.Web.UI.UserControl { pub ...
- Sereja and Bottles-水题有点坑爹
CodeForces - 315A Sereja and Bottles Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: ...
- hashmap的put方法源码分析
put主源码如下: public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = ha ...
- UVA 10859 - Placing Lampposts 树形DP、取双优值
Placing Lampposts As a part of the mission ‘Beautification of Dhaka City’, ...