LeetCode·每日一题·1624.两个相同字符之间的最长子字符串·模拟
2023-09-27 14:26:29 时间
链接:https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/by-xun-ge-v-h1e6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目![](https://img-blog.csdnimg.cn/6b1a9ccb815041beae872b601a8423c1.png)
示例![](https://img-blog.csdnimg.cn/f3138d4f8cc44d5aa9c7988aadd5b39b.png)
思路
思路
直接枚举数组中每一个元素,记录元素第一次出现的下标,当在次出现时,保存中间量
小问题
int hash[26] = {-1};
为什么数组初始化之后不为-1???
代码
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int maxLengthBetweenEqualCharacters(char * s){
int max = -1;
int len = strlen(s);
int hash[26];//记录第一次下标
memset(hash, -1, sizeof(int) * 26);
for(int i = 0; i < len; i++)//枚举所有元素
{
if(hash[s[i] - 'a'] == -1)//未出现过,记录第一次下标
{
hash[s[i] - 'a'] = i;
}
else//出现过了,保存最大中间量
{
max = MAX(max, i - hash[s[i] - 'a']-1);
}
}
return max;
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/largest-substring-between-two-equal-characters/solution/by-xun-ge-v-h1e6/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章
- LeetCode-3.无重复字符的最长子串
- LeetCode高频题:int数字x被拆为p1,p2,p3,输入N必须满足N=p1+p2/p3,p2/p3整除,N有多少种分数形式
- 后缀数组的应用:[Leetcode] 321.拼接最大数(困难)
- JS leetcode 宝石与石头 题解分析,正则字符组也有妙用
- [LeetCode] First Missing Positive
- [LeetCode]面试题 01.01. 判定字符是否唯一
- [LeetCode]121. 买卖股票的最佳时机
- 90、【树与二叉树】leetcode ——104. 二叉树的最大深度:层次遍历+DFS+子问题分解(C++版本)
- 【数据结构/二叉树】leetcode刷题路线(持续更新)
- 【LeetCode】241. Different Ways to Add Parentheses
- 【LeetCode】167. Two Sum II - Input array is sorted
- 【LeetCode】169. Majority Element
- 【LeetCode】30. Substring with Concatenation of All Words
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 1297. Maximum Number of Occurrences of a Substring 子串的最大出现次数
- [LeetCode] 1247. Minimum Swaps to Make Strings Equal 交换字符使得字符串相同
- [LeetCode] 1081. Smallest Subsequence of Distinct Characters 不同字符的最小子序列
- [LeetCode] 926. Flip String to Monotone Increasing 翻转字符串到单调递增
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
- leetcode 53. Maximum Subarray 最大子数组和(中等)
- LeetCode 3. 无重复字符的最长子串