Leetcode练习 #3Longest Substring Without Repeating Characters
来源:互联网 发布:yy会员签到软件 编辑:程序博客网 时间:2024/06/18 09:24
3. Longest Substring Without Repeating Characters
Given a string,find the length of thelongest 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 asubsequence and not a substring.
题目简析:
题目要求比较简单,就是在给出的字符串中找出最长无重复字母串并返回长度,初步思路是采取动态规划,一方面记录当前得到的最长无重复字母串maxString,另一方面保持currentString的可增长性。
从具体操作来看,从母字符串s的首字母开始,每次依次选取后一个字母,添加到持续增长的字符串currentString之中,对于字母重复的不同情况,采取以下策略:
1.待添加字母与currentString无重复
无重复时,直接把字母添加到currentString末端, 实现字符串增长。
2.待添加字母与currentString重复,重复位置在currentString首位
重复位置在字符串首位时,舍弃字符串currentString首位的重复字母,并把新字母添加到currentString末端,消除了重复的情况,此时保持了字符串最长长度又保持了增长性。
3.待添加字母与currentString重复,重复位置在currentString末位
在字符串中,若有两个相邻的重复字母,必然产生断隔,因此当前的currentString将不再具有增长性。为了获取增长性,需要重新初始化一个currentString,currentString字符串重新开始的位置由该重复字母的位置开始。同时为了不丢失已有的最长字符串记录,这里字符串maxString需要及时记录原有的currentString。
4.待添加字母与currentString重复,重复位置在currentString中间(非首尾位置)
与情况3类似,当重复字母位于currentString中间时,失去了可增长性。此时,我们重新初始化currentString,currentString字符串起始位置也是在中间的重复字母的位置。与3相同的是,此时应记录的最长字符串同样是原本整个currentString。
以下插入代码(C++代码)
class Solution {public: int lengthOfLongestSubstring(string s) { string maxString=""; string currtString; int start=0; int pos=0; int size=s.length(); if(size==1) return 1; for(int i=1;i<size;i++){ currtString.assign(s,start,i-start); pos=currtString.find(s[i]); if(pos==-1) currtString.assign(s,start,i-start+1); else if(pos==0){ start++; } else if(pos==i-start-1){ if(maxString.length()<currtString.length()) maxString=currtString; start=i; } else if(pos>0 && pos <i-start-1){ if(maxString.length()<currtString.length()) maxString=currtString; start=start+end+1; } } if(maxString.length()>currtString.length()) return maxString.length(); else return currtString.length(); }};
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
以上思路纯属个人看法,如有更好的思路,望不吝赐教。
- [编程练习][Longest Substring Without Repeating Characters]<LeetCode-3>
- 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 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
- 移动端车牌识别技术功能应用
- Gdb install faild: Unpacking gdb-arm-none-eabi (7.6.50.20131218-0ubuntu1+1) ... dpkg: error processi
- 最简单的搭建及使用本地服务器
- Java中的集合框架
- 137. Single Number II
- Leetcode练习 #3Longest Substring Without Repeating Characters
- 笔记:Web安全(更新:2017.09.07)
- URAL-1103. Pencils and Circles
- SQL Server 数据模型和数据规范
- RTB 广告投放流程详解
- iOS平台下闪退原因汇总(一):"Ran out of trampolines of type 0/1/2" 运行时间错误
- 类型转换函数 & 转换构造函数
- Groovy中对xml的操作补充
- laravel5.5 常用 artisan 命令