【leetcode刷题笔记】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.



题解:贪心算法。

利用一个hashmap存放当前最长的无重substring所包含的元素,变量leftbound存放当前的最左边界。则当前遍历的字符s[i]有两种情况:

  1. s[i]所指的元素已经存在在map中了,那么就左移leftbound直到leftbound到i这一段不再包含s[i],并且在map中清除leftbound扫过的元素;
  2. s[i]所指的元素不在map中,保持leftbound不动,查看是否需要更新当前最长substring的长度。

代码如下:

 1 public class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         if(s == null || s.length() == 0)
 4             return 0;
 5
 6         HashMap<Character, Integer> map = new HashMap<Character, Integer>();
 7         int leftebound = 0;
 8         int answer = 0;
 9
10         for(int i = 0;i < s.length();i++){
11             char current = s.charAt(i);
12             if(map.containsKey(s.charAt(i))){
13                 while(s.charAt(leftebound) != current && leftebound < i){
14                     map.remove(s.charAt(leftebound));
15                     leftebound++;
16                 }
17                 leftebound++;
18             }
19             else {
20                 map.put(current, 0);
21                 answer = Math.max(i-leftebound+1, answer);
22             }
23         }
24
25         return answer;
26     }
27 }       

【leetcode刷题笔记】Longest Substring Without Repeating Characters

时间: 2024-10-31 21:21:10

【leetcode刷题笔记】Longest Substring Without Repeating Characters的相关文章

刷题3. Longest Substring Without Repeating Characters

一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug free有点难度,主要是边界的问题. 二.这个题目,我自己实现,没有参考代码 提交了5次: 第1次: Wrong Answer,主要是" "这个空串不对 第2次.第3次:Runtime Error,"au" out of Range 第4次:Wrong Answer,

LeetCode 第三题,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 s

【leetcode刷题笔记】Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Exp

【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

Given a string, find the length of the longest substring without repeating characters. Example 1:           Input: "abcabcbb"                              Output: 3                           Explanation: The answer is "abc", with the l

Leetcode经典试题:Longest Substring Without Repeating Characters解析

题目如下: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 E

LeetCode(3)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 s

lintcode 中等题:longest substring without repeating characters 最长无重复字符的子串

题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于,"bbbbb",其无重复字符的最长子字符串为"b",长度为1. 解题 利用HashMap,map中不存在就一直加入,存在的时候,找到相同字符的位置,情况map,更改下标 public class Solution { /** * @param s: a s

第三题:Longest Substring Without Repeating Characters

题目链接:题目链接 找到最长不重复子串,使用hash方法来做是极好的!遍历string,找到一个字符,然后会判断这个字符是不是已经在当前的这个子串中,这个使用hash方法,因为可见字符最多256个,那么使用hash[256]就可以!初始化为-1,保存的是相应的字符在string中的位置.如果遇到在前面存在的字符,那么需要将这个子串的start位置移动到已经存在的位置后面一位,然后继续遍历,直到遍历结束. 具体看下面的代码: class Solution { public: int lengthO