Longest Substring Without Repeating Character…
2018-06-18 04:20:14来源:未知 阅读 ()
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.
题目地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/
题目意思:给出一个字符串,输出最长的子串的长度,子串不能有重复的字符。(子串是连续的。)
解题思路: 依次读取字符串的每一个字符,如果第一次出现则当前子串长度+1,否则:首先判断当前长度是否大于最大长度,是则替换最大长度。然后
查找重复的字符是在原字符串哪里出现的。
代码如下:
1 int lengthOfLongestSubstring(char* s) { 2 int maxlen = 0,currlen = 0; 3 int table[128], i, j, start = 0; 4 memset(table, 0, sizeof(table)); 5 for (i = 0; s[i] != '\0'; ++i){ 6 if( (++table[s[i]]) == 2 ){ 7 if (currlen > maxlen){ 8 maxlen = currlen; 9 } 10 for(j = start; j < i; ++j){ //start记录重复的字符后一个位置 11 if (s[j] == s[i]){ 12 table[s[j]] = 1; 13 start = j+1; 14 break; 15 }else{ 16 --currlen; 17 table[s[j]] = 0; 18 } 19 } 20 }else{ 21 ++currlen; 22 } 23 } 24 if (currlen > maxlen){ 25 maxlen = currlen; 26 } 27 return maxlen; 28 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Longest Ordered Subsequence 2020-02-09
- 最长上升子序列(LIS: Longest Increasing Subsequence) 2019-09-08
- The Xor-longest Path(trie树) 2019-08-16
- Longest Substring Without Repeating Characters 2019-08-16
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀 2018-06-29
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash