当前位置:网站首页>Interview questions 17.05. Letters and numbers
Interview questions 17.05. Letters and numbers
2022-08-08 14:31:00 【Xiuqiang】
面试题 17.05. 字母与数字
#前缀和 #哈希表
给定一个放有字母和数字的数组,找到最长的子数组,且包含的字母和数字的个数相同.
返回该子数组,若存在多个最长子数组,返回左端点下标值最小的子数组.若不存在这样的数组,返回一个空数组.
示例 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”]
示例 2:
输入: [“A”,“A”]
输出: []
提示:
array.length <= 100000
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/find-longest-subarray-lcci
解法1
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** * 思路: * digital as-1,See the letters as1,再计算前缀和. * The hash table records the subscript of the first occurrence of the prefix sum. * If the prefix sum is the same, the difference of the subscripts is calculated. */
public class Solution {
public String[] findLongestSubarray(String[] array) {
// 前缀和
int s = 0;
// Save the first time
Map<Integer, Integer> m = new HashMap<>();
// 前缀和数组,Save the prefix and the first occurrence,数组的下标
int[] sum = new int[array.length];
// The length of the longest subarray with the same number of letters and numbers
int max = 0;
// Subarray start index
int start = 0;
// Only the same number of numbers and letters are required in the question,It doesn't matter how many numbers and letters are there.So think of the numbers as -1,See the letters as 1,Similar to the idea of discretization in line segment trees.
for (int i = 0; i < array.length; i++) {
char c = array[i].charAt(0);
if (c >= '0' && c <= '9') {
// 数字 -1
s--;
} else {
// 字母 + 1
s++;
}
// 前缀和等于0的情况,[0,i] The sum of all elements in the interval is 0,That is, the number of numbers and letters are the same.区间长度为 i+1
if (s == 0 && i + 1 > max) {
start = 0;
max = i + 1;
}
// Fill prefix and array
sum[i] = s;
// The prefix and the first occurrence of the subscript
Integer prefixFist = m.get(s);
if (prefixFist != null) {
// 当前下标 i Subtract the subscript of the first occurrence of the prefix sum prefixFist,Calculate the interval length
if (i - prefixFist > max) {
// 举个例子 前缀和为 1 0 1,下标分别为 0 1 2,The first occurrence of the prefix sum is 1 的下标为 0,The subscript of the second occurrence of the prefix sum is 2.区间 0 1 的长度为 2 - 0 = 2
start = prefixFist + 1;
max = i - prefixFist;
}
} else {
m.put(s, i);
}
}
// Returns the longest subarray
String[] copy = new String[max];
System.arraycopy(array, start, copy, 0, max);
return copy;
}
}
作者:xiu-qiang-jiang
链接:https://leetcode.cn/problems/find-longest-subarray-lcci/solution/by-xiu-qiang-jiang-g8j9/
来源:力扣(LeetCode)
边栏推荐
- H5不同屏幕大小显示不同的文字大小图片大小
- KD-SCFNet: More Accurate and Efficient Salient Object Detection Through Knowledge Distillation (ECCV2022)
- 【LeetCode】761. Special binary sequence
- 华为云弹性云服务器ECS使用【华为云至简致远】
- 【控制】动力学建模举例 --> 拉格朗日法
- 「复盘」面试BAMT回来整理398道高频面试题,助你拿高薪offer
- synchronized修饰类的注意事项
- qtwebapp库的编译及简单使用
- 彻底了解什么是POE交换机!!!
- egg.js框架的基本设置 及 使用
猜你喜欢
随机推荐
【os.path】的相关用法(持更)
Full of dry goods, Yu Jingxin class of the Institute of Information Technology, Chinese Academy of Sciences will help you get academic research and thesis writing skills
【小码匠自习室】ABC180-C: 马虎是小孩的天性吗?
2022-08-07 第五小组 顾祥全 学习笔记 day31-集合-Map集合
京东三面惨遭被虐,关于redis,高并发,分布式,问懵了
基于ModelArts的StyleGAN3生成高清图丨【华为云至简致远】
KMP Media Group South Africa implemented a DMS (Document Management System) to digitize the process, employees can again focus on their actual tasks, providing efficiency
PC端实用软件推荐
手把手教你设计一个全局异常处理器
【电路基础2】电容
AT2382-[AGC015D]A or...or B Problem
shell regular expression, Three Musketeers grep command
【Rust—LeetCode题解】1.两数之和
vijos1212 Way Selection
Review: What is the pre-approval of autumn recruitment?What is an ordinary autumn move?It's all recruitment, why do you need to set these two recruitment time periods?
JS-Bom-while(计算闰年)
【Rust—LeetCode题解】1408.数组中的字符串匹配
vsomeip环境搭建及helloworld测试例跑通
华为云会议初体验【华为云至简致远】
idea中项目呈现树形结构









