版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/shiliang97/article/details/101487694
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:
Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3:
Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
int lengthOfLongestSubstring(char * s){
int prior = 0; //上次状态下最长子串的长度
int left = 0;
int dict[256] = {0}; //映射ASCII码
int right = 1; //表示字符串中第right个字符
int i;
while(*s != '\0'){
i = *s-0; //字符转换为整数
if(dict[i] > left) left = dict[i];
dict[i] = right;
prior = (prior>right-left)?prior:right-left; //right的值比对应的数组下标大1
s++;
right++;
}
return prior;
}