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

Leetcode(3)-Longest Substring Without Repeating Characters

时间:2016-02-03 06:39:32      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

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.

 

经典Hashtable + two pointer的题目。时间复杂度O(n), 空间复杂度O(n)

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         int res = 0, start = 0;
 4         Map<Character, Integer> map = new HashMap<>();
 5         for(int i = 0; i < s.length(); i++){
 6             char c = s.charAt(i);
 7             if(map.containsKey(c)){
 8                 start = Math.max(start, map.get(c)+1);
 9             }
10             map.put(c, i);
11             res = Math.max(res, i-start+1);
12         }
13         return res;
14     }
15 }

 

Leetcode(3)-Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/xinhuan23/p/5178880.html

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