首页 > 其他 > 详细

Leetcode(3)-Longest Substring Without Repeating Characters

时间:2016-02-03 06:38:28      阅读:137      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!