3. Longest Substring Without Repeating Characters
来源:互联网 发布:杭州城西银泰mac在几楼 编辑:程序博客网 时间:2024/06/11 04:01
3. Longest Substring Without Repeating Characters
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.
Subscribe to see which companies asked this question
1.mine (16ms/40.58%)
int lengthOfLongestSubstring(char* s) { int j,ph,pt,n,max=1; if(s[0]=='\0'){ return 0; } if(s[0]=='\0'){ return 1; } ph=0;pt=0; while(s[pt+1]!='\0'){ pt++; for(j=ph;j<pt;j++){ if(s[pt]==s[j]){ ph=j+1; } } n=pt-ph+1; if(n>max){ max=n; } } return max;}
2.example(6ms/72.84%)
int lengthOfLongestSubstring(char* s) { int maxLen = 0, prevMaxLen = 0, prevIndexOf[128]; bzero(prevIndexOf, sizeof(prevIndexOf)); for (char *c = s; *c; c++) { int idx = c - s + 1; // Add 1 to the index since prevIndexOf is initialized with zeros int candidateLen = idx - prevIndexOf[*c]; int curMaxLen = (++prevMaxLen < candidateLen) ? prevMaxLen : candidateLen; prevIndexOf[*c] = idx; if (curMaxLen > maxLen) { maxLen = curMaxLen; } prevMaxLen = curMaxLen; } return maxLen;}
0 0
- [LeetCode]3.Longest Substring Without Repeating Characters
- LeetCode 3.Longest Substring Without Repeating Characters
- LeetCode --- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [Leetcode] 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters
- [leetcode] 3.Longest Substring Without Repeating Characters
- 3.Longest Substring Without Repeating Characters
- [leetcode] 3. Longest Substring Without Repeating Characters
- 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
- 运维笔记10 (Linux软件的安装与管理(rpm,yum))
- cookie和session
- 总结一下C/C++程序中几种常用的计时函数
- 全面理解Context
- 真正的建构主义教育是无比快乐的
- 3. Longest Substring Without Repeating Characters
- 微信公众平台开发中的WEUI
- SplashActivity 总结
- 适配器模式
- 类与对象的使用3
- Java编程里类的继承
- 51nod 1010 只包含因子2 3 5的数
- android圆形图片
- 第八周oj4