Longest Substring Without Repeating Characters (not optimized)

xiaoxiao2021-02-28  24

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", 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.

class Solution { public: int lengthOfLongestSubstring(string s) { int len = s.length(); // cout << len << endl; int sum = 0; vector<int> index; for(int i=0;i<len;i++){ index.push_back(i); for(int j=i+1;j<len;j++){ if(checkContains(index,s,s[j])){ break; } index.push_back(j); } //注意:int和unsigned int比较会将int -->unsigned int,-1 > 100 if(sum <= index.size()){ sum = index.size(); } // cout << "index size: " << index.size() << endl; // cout << "sum: " << sum << endl; index.clear(); } return sum; } bool checkContains(vector<int> &idx,string &s,char &c){ int len = idx.size(); for(int i=0;i<len;i++){ if(s[idx[i]] == c){ return true; } } return false; } };

转载请注明原文地址: https://www.6miu.com/read-2624817.html

最新回复(0)