3 无重复字符的最长

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:

输入: “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:

输入: “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


int isContainRepeatChar(char *s, unsigned int start, unsigned int end, char c)
{
	while (start < end) {
		if (s[start] == c) {
			return start;
		}
		start++;
	}

	return -1;
}

int lengthOfLongestSubstring(char * s) {
	int iRetLength = 0;
	int i = 0;
	int iCurLength = 0;
	int start = 0, end = 0, repeat = 0;

	while (s[i] != NULL) {
		end = i;
		repeat = isContainRepeatChar(s, start, end, s[i]);
		if (repeat == -1) {
		}
		else {
			start = repeat + 1;
		}
		iCurLength = end - start + 1;
		if (iRetLength < iCurLength) {
			iRetLength = iCurLength;
		}
		i++;
	}

	return iRetLength;

}
无重复字符最长子串问题指的是,给定一个字符串`s`,找出其中不含有重复字符最长子串的长度,该问题可使用滑动窗口和哈希表解决,这一组合能解决一系列字符串子串问题,如最小覆盖子串、找到字符串中所有字母异位词、最长重复子串等[^3]。 ### 算法思路 使用滑动窗口结合哈希表的方法。定义一个哈希表来存储每个字符字符串中出现的位置,同时定义双指针`left`和`right`,其中`left`指向滑动窗口的左端,而`right`则向右移动。在移动`right`指针的同时不断更新哈希表,以及记录当前无重复字符最长子串的长度。当遇到重复字符时,利用哈希表中记录的信息来更新左指针,并重新计算当前的最长子串[^1]。 ### 代码实现 #### Python实现 ```python def lengthOfLongestSubstring(s): # 哈希表,记录字符及其最近一次出现的位置 char_index_map = {} # 滑动窗口的左边界 left = 0 # 最长无重复子串的长度 max_length = 0 for right in range(len(s)): current_char = s[right] # 如果当前字符已经存在于窗口中,更新左边界 if current_char in char_index_map: left = max(left, char_index_map[current_char] + 1) # 更新哈希表,记录当前字符的位置 char_index_map[current_char] = right # 计算当前窗口的长度,并更新最大长度 max_length = max(max_length, right - left + 1) return max_length ``` #### Java实现 ```java import java.util.HashMap; import java.util.Map; class Solution { public int lengthOfLongestSubstring(String s) { int n = s.length(); // 字符长度 int maxLength = 0; // 最长无重复子串的长度 Map<Character, Integer> map = new HashMap<>(); // 哈希表,记录字符及其最近一次出现的位置 // 滑动窗口的左右边界分别为 i 和 j for (int j = 0, i = 0; j < n; j++) { char currentChar = s.charAt(j); // 当前字符 // 如果当前字符已经存在于窗口中,更新左边界 i if (map.containsKey(currentChar)) { i = Math.max(map.get(currentChar) + 1, i); } // 更新哈希表,记录当前字符的位置 map.put(currentChar, j); // 计算当前窗口的长度,并更新最大长度 maxLength = Math.max(maxLength, j - i + 1); } return maxLength; // 返回最长无重复子串的长度 } } ``` #### C++实现 ```cpp #include <iostream> #include <string> #include <unordered_map> #include <algorithm> class Solution { public: int lengthOfLongestSubstring(std::string s) { int n = s.size(); int maxLength = 0; std::unordered_map<char, int> charIndexMap; for (int right = 0, left = 0; right < n; right++) { char currentChar = s[right]; if (charIndexMap.find(currentChar) != charIndexMap.end()) { left = std::max(left, charIndexMap[currentChar] + 1); } charIndexMap[currentChar] = right; maxLength = std::max(maxLength, right - left + 1); } return maxLength; } }; ``` ### 复杂度分析 - **时间复杂度**:$O(n)$,其中`n`是字符串的长度。因为只需要遍历一次字符串。 - **空间复杂度**:$O(min(n, m))$,其中`m`是字符集的大小(ASCII 字符集大小为 128),哈希表最多存储所有唯一字符 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值