LeetCode-3 Longest Substring Without Repeating Characters
来源:互联网 发布:u3d java工资大概多少 编辑:程序博客网 时间:2024/05/01 01:21
题目:给一个字符串,返回该字符串中最长的不含重复字符的字符串长度
思路:1、暴力搜索,判断每个子字符串是否符合条件
2、定义一个窗口,含左右边界,使用一个map保存窗口中的字符,每次右边界向右移动时,判断新字符是否在map中,如果不在,则更新右边界,同时更新最长子字符串长度。如果在,则移动左边界到map中相同字符所在位置右侧,同时更新map及窗口大小。依次直到遍历完字符串,时间复杂度O(N)
class Solution {public: //:时间复杂度:O(n) //:空间复杂度:O(1) //:思路:滑动窗口法,维护一个窗口,确保窗口内的子串为无重复子串,当右边界向右加一时,若该字符已经存在,则左边界移到该字符之前出现位置的右边,相应的count减去移动的数目。如果该字符第一次出现,则count+1,继续移动,同时更新最大计数值。这样一次循环即可实现。 int lengthOfLongestSubstring(string s) { int a[256]={0}; int length1=s.length(); if (length1==0)return 0; int coun=1; int te_count=1; int i=0; for(int k=0;k<256;k++)a[k]=-1; int temp=s[0]; a[temp]=1; for(int k=1;k<length1;k++){ temp=s[k]; if(a[temp]==1){ int m=i; while(s[m]!=s[k]){ te_count--; a[s[m]]=-1; m++; } i=m+1; if(te_count>coun){ coun=te_count; } } else{ a[temp]=1; te_count++; if(te_count>coun){ coun=te_count; } } } return coun; }};
0 0
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode 3: Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- vs2015/mfc动态创建菜单
- Web页面实时刷新技术探讨
- freemarker中获取当前日期并比较
- 报错为未启用当前数据库的 SQL Server Service Broker,请为此数据库启用 Service Broker
- 求最大子序列
- LeetCode-3 Longest Substring Without Repeating Characters
- curl,fsocketopen,socket 三种函数抓取html页面
- android多媒体系统分析-AudioPolicyService
- 如何对报表的参数控件赋值
- 高跟鞋
- UINavigationController和UITabBarController的属性之间的一些继承关系
- safe_mode 开启后linux下影响
- 二位小数
- 称砝码