题目:max length in leetcode
来源:互联网 发布:棋牌游戏源码可控制 编辑:程序博客网 时间:2024/04/29 01:02
题目
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
使用MAP来改善查找是否有重复,ldx 与 rdx 分别表示这个sub string 的左边的下标和右边的下标
class Solution {
public:int lengthOfLongestSubstring(string s) {
if (s.size()==0) return 0;
int max_length = 1;
map<char,int> imap;
int ldx = 0;
int rdx = 0;
for(;rdx<s.size();++rdx){
if(imap.find(s[rdx])!=imap.end()){
int mdx = imap[s[rdx]];
if(mdx>=ldx){ //考虑这些重复是否有用
int len = rdx-ldx;
max_length = (len>max_length?len:max_length);
ldx = mdx + 1;
}
}
imap[s[rdx]]=rdx;//更新最新的index
}
max_length = ((rdx-ldx)>max_length)?(rdx-ldx):max_length;
return max_length;
}
};
0 0
- 题目:max length in leetcode
- leetcode题目解答--Max Points on a Line
- [LeetCode] Text Justification (put words in lines with same length)
- DB2 的Max Key Length
- leetcode题目:reverse words in a string
- LeetCode All in One 题目讲解汇总
- LeetCode算法题目:Largest Rectangle in Histogram
- leetcode做题总结,题目Length of Last Word 2012/03/27
- Datatype length in mysql
- The max length of a byte array
- max key length is 767 bytes
- 【LeetCode】 Length Of LastWord
- Start from length & length() in Java
- 【九度】题目1548:平面上的点 && 【LeetCode】Max Points on a Line
- leetcode题目--Reverse Words in a String解答
- leetcode -- Reverse Nodes in k-Group -- 经典题目,要重写
- 33.leetcode题目237: Delete Node in a Linked List
- LeetCode Swap Nodes in Pairs题目的一种简洁解法
- 第三周 项目2 三角形类 2
- 录音频率图
- linux find命令-exec参数的使用说明(笔记)
- 1-7. Database Administrator
- JSTL
- 题目:max length in leetcode
- UVa 207 - PGA Tour Prize Money(模拟)
- linux系统调用工作过程
- 1-8. Web Designer
- 第二周项目四---考了语文数学的学生
- 1-9. Technical Support
- 1-10. Senior Software Engineer
- Machine Learning by Andrew Ng --- K-means
- 测试员,敢问路在何方?来自微软工程师