lc 无重复字符的最长子串
链接:https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1012/
代码:
 
#include<algorithm> class Solution { public: int lengthOfLongestSubstring(string s) { int res = 1; int len = s.size(); if(len == 0) return 0; if(len == 1) return 1; int count[len]; for(int i = 0; i < len; i++) { count[i] = 1; } // cout << "----" << endl; for(int i = 1; i < len; i++) { // cout << "====" << endl; for(int j = i-1, temp = count[i-1]; temp > 0 && j >= 0; temp--, j--) { // cout << s[j] << " " << s[i] << " " << count[i] << endl; if(s[j] != s[i]) count[i]++; else break; } if(res < count[i]) res = count[i]; } return res; } };
思路:初始化每个 count都是 1,由于是子串,最大长度自然是 count[i-1] +1, 将当前位置字母之前的 count[i-1]长度的字符比较,若相等 count[i]++,否则 break 掉,先下笔,再敲键盘。
posted on 2020-05-18 00:21 FriskyPuppy 阅读(160) 评论(0) 收藏 举报
 
                    
                     
                    
                 
                    
                 
 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号