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

Longest Substring Without Repeating Characters

时间:2014-04-29 13:47:20      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:leetcode   java   

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) {
		if (s == null || s.isEmpty()) {
			return 0;
		}
		
		int pre = 1; 
		int length = s.length();
		int longest = 0;
		if(length == 1) return 1;  
		for (int i = 1; i < length; i++) {
			char temp = s.charAt(i);
			int j = i - 1;
			int count = 1;
			while (j >= 0 && temp != s.charAt(j)) {
				j--;
				count++;
			}
            if(count >= pre+1) count = pre+1;  
            if(count > longest) longest = count;  
            pre = count;  
		}
		return longest;
    }
}


Longest Substring Without Repeating Characters

标签:leetcode   java   

原文地址:http://blog.csdn.net/jason_wang1989/article/details/24700407

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