Longest Substring Without Repeating Characters

原创 2015年07月10日 16:34:32
题目:

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存储字符和index信息,map中不存在字符,则put进来,存在

字符则统计map的size并和存储的最大长度比较,然后清空map。但当测试用例是一个

很长的String时,就会超时。

public static int lengthOfLongestSubstring(String s) {
		if(s==null)
			return 0;
		s=s.trim();
		int count=0;
		Map<Character,Integer> map=new HashMap<Character,Integer>();
		for(int i=0;i<s.length();i++){
			if(!map.containsKey(s.charAt(i)))
				map.put(s.charAt(i), i);
			else{
				count=Math.max(count, map.size());
				i=map.get(s.charAt(i));
				map.clear();
			}
		}
		return Math.max(count, map.size());
	}

改进的方法是用boolean数组存储字符信息,默认为false,数组中存在字符是,置为true,

否则,记录当前子字符串和count的最大值,将数组置为false,更新子字符串起始index。

public static int lengthOfLongestSubstring(String s){
		if(s==null||s.length()<=0)
			return 0;
		boolean[] ch=new boolean[128];
        int start = 0;
        int count = 0;
        for(int i=0;i<s.length();i++){
        	if(ch[s.charAt(i)]){//如果数组中不存在字符
        		count=Math.max(count, i-start);//比较子字符串长度和之前的长度记录
        		for (int k = start; k < i; k++) {
    				if (s.charAt(k)== s.charAt(i)) {
    					start = k + 1; //更新子字符串的起始位置
    					break;
    				}
    				ch[s.charAt(k)] = false;//将数组置为false
    			}
        	}else
        		ch[s.charAt(i)]=true;//将数组中字符对应的位置置为true
        }
        return Math.max(count, s.length()-start);
    }


【LeetCode-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】

【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】给定一个字符串,找字符中的最大非重复子串用start记录当前处理的开始位置...

【LeetCode】Longest Substring Without Repeating Characters

问题描述Given a string, find the length of the longest substring without repeating characters.Examples:G...

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

Longest Substring Without Repeating Characters-----LeetCode

题目Given a string, find the length of the longest substring without repeating characters.Examples:Giv...

leetcode003 Longest Substring Without Repeating Characters

题目3. Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

leetcode: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

(leetcode) Longest Substring Without Repeating Characters

Question : Given a string, find the length of the longest substring without repeating characters. ...

LeetCode- 3. Longest Substring Without Repeating Characters

Description Given a string, find the length of the longest substring without repeating characters.E...

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Longest Substring Without Repeating Characters
举报原因:
原因补充:

(最多只允许输入30个字)