525:连续数组

This commit is contained in:
huangge1199 2021-06-03 10:52:34 +08:00
parent 7efb0980b6
commit c21ceac93f
3 changed files with 95 additions and 1 deletions

View File

@ -0,0 +1,67 @@
//给定一个二进制数组 nums , 找到含有相同数量的 0 1 的最长连续子数组并返回该子数组的长度
//
//
//
// 示例 1:
//
//
//输入: nums = [0,1]
//输出: 2
//说明: [0, 1] 是具有相同数量0和1的最长连续子数组
//
// 示例 2:
//
//
//输入: nums = [0,1,0]
//输出: 2
//说明: [0, 1] ( [1, 0]) 是具有相同数量0和1的最长连续子数组
//
//
//
// 提示
//
//
// 1 <= nums.length <= 105
// nums[i] 不是 0 就是 1
//
// Related Topics 哈希表
// 👍 300 👎 0
package leetcode.editor.cn;
import java.util.HashMap;
import java.util.Map;
//525:连续数组
public class ContiguousArray {
public static void main(String[] args) {
//测试代码
Solution solution = new ContiguousArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int findMaxLength(int[] nums) {
int max = 0;
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int count = 0;
map.put(count, -1);
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
count++;
} else {
count--;
}
if (map.containsKey(count)) {
max = Math.max(max, i - map.get(count));
} else {
map.put(count, i);
}
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,27 @@
<p>给定一个二进制数组 <code>nums</code> , 找到含有相同数量的 <code>0</code><code>1</code> 的最长连续子数组,并返回该子数组的长度。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> nums = [0,1]
<strong>输出:</strong> 2
<strong>说明:</strong> [0, 1] 是具有相同数量0和1的最长连续子数组。</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> nums = [0,1,0]
<strong>输出:</strong> 2
<strong>说明:</strong> [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> 不是 <code>0</code> 就是 <code>1</code></li>
</ul>
<div><div>Related Topics</div><div><li>哈希表</li></div></div>\n<div><li>👍 300</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long