程序员面试金典17.05_go_字母与数字

题目给定一个放有字符和数字的数组 , 找到最长的子数组 , 且包含的字符和数字的个数相同 。
返回该子数组 , 若存在多个最长子数组 , 返回左端点最小的 。 若不存在这样的数组 , 返回一个空数组 。
示例 1:
输入: ["A","1","B","C","D","2","3","4","E","5","F","G","6","7","H","I","J","K","L","M"]
输出: ["A","1","B","C","D","2","3","4","E","5","F","G","6","7"]
【程序员面试金典17.05_go_字母与数字】示例 2:输入: ["A","A"]输出: []
提示:array.length <= 100000
解题思路分析1、前缀和;时间复杂度O(n) , 空间复杂度O(n)
程序员面试金典17.05_go_字母与数字文章插图
func findLongestSubarray(array []string) []string { m := make(map[int]int) m[0] = 0 res := 0 begin := 0 total := 0 for i := 0; i < len(array); i++ {if '0' <= array[i][0]ok {if i-index > res {res = i - indexbegin = index + 1}} else {m[total] = i} } return array[begin : begin+res]}总结Medium题目 , 使用前缀和进行统计 , 使用map记录第一次出现的位置