码迷,mamicode.com
首页 > 其他好文 > 详细

Longest Substring Without Repeating Characters

时间:2014-10-15 11:42:20      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   ar   java   for   sp   div   art   

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.

答案

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int result=0;
        Map<Character,Integer> map=new HashMap<Character,Integer>(500);
        int start;
        int end;
        for(start=0,end=0;end<s.length();end++)
        {
            Integer value=map.get(s.charAt(end));
            int pStart=(value==null?-1:value);
            for(;start<=pStart;start++)
            {
                map.remove(s.charAt(start));
            }
            map.put(s.charAt(end),end);
            result=Math.max(result,end-start+1);
        }
        return result;
    }
}


Longest Substring Without Repeating Characters

标签:style   color   io   ar   java   for   sp   div   art   

原文地址:http://blog.csdn.net/jiewuyou/article/details/40106843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!