标签:003 unmap map int tempLength 字符 Substring Without maxLength
题目:
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.
代码(C++实现):
1 class Solution { 2 public: 3 int lengthOfLongestSubstring(string s) 4 { 5 // 定义一个map用来存放整个字符串s 6 unordered_map<char, int> unmap; 7 8 // tempLength记录每次扫描位置开始的一次统计的最长字符串的长度 9 int tempLength = 0; 10 // 众多tempLength中的最大值 11 int maxLength = 0; 12 13 // 第一层循环:分别以s中的每个字符为基准,进行遍历 14 for (int j = 0; j < s.size(); j++) 15 { 16 // 第二层循环:以当前第一层循环中当前的字符为基准,进行遍历,统计以此字符为基准的tempLength 17 for (int i = j; i < s.size(); i++) 18 { 19 // 是否tempLength继续增加的条件是,map中没有出现过当前指向的字符 20 if (unmap.count(s[i]) == 0) 21 { 22 pair<char, int> myshopping(s[i], i); 23 // 如果当前的map中无此字符,将当前字符插入到map中 24 unmap.insert(myshopping); 25 tempLength++; 26 maxLength = maxLength > tempLength ? maxLength : tempLength; 27 } 28 // 当前字符已经在map中了,直接break,并将本次使用的map进行清除操作 29 else 30 { 31 32 tempLength = 0; 33 unmap.clear(); 34 break; 35 } 36 37 } 38 } 39 40 return maxLength; 41 } 42 };
标签:003,unmap,map,int,tempLength,字符,Substring,Without,maxLength 来源: https://www.cnblogs.com/xuelisheng/p/10771848.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。