关闭

LeetCode:Longest Substring Without Repeating Characters

标签: LeetCodeLongest Substring Wi
3262人阅读 评论(0) 收藏 举报
分类:

Longest Substring Without Repeating Characters




Total Accepted: 152787 Total Submissions: 687012 Difficulty: Medium

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.

Subscribe to see which companies asked this question





















思路:

维护一个HashMap存储每个字符的位置;两个指针i,j,维护最长子串的范围。


java code:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        
        if(s==null || s.length()==0) return 0;
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        int max = 0;
        for(int i=0,j=0;i<s.length();i++) {
            char c = s.charAt(i);
            if(map.containsKey(c)) {
                j = Math.max(j, map.get(c)+1);
            }
            map.put(c, i);
            max = Math.max(max, i-j+1);
        }
        return max;
    }
}


0
0
查看评论

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUniqueSubstr // // Created by feliciafay on 11/21/13. /...
  • feliciafay
  • feliciafay
  • 2013-11-23 04:25
  • 16842

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在LeetCode中很常用的方法,对于字符串的题目非常有用。 首先brute force的...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 01:53
  • 18839

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a string, find the length of the longest substri...
  • j754379117
  • j754379117
  • 2016-06-20 20:36
  • 854

leetcode python - Longest Substring Without Repeating Characters

# Given a string, find the length of the longest substring without repeating characters. # # Examples: # # Given "abcabcbb", the answer is &...
  • AMDS123
  • AMDS123
  • 2017-04-01 15:07
  • 6337

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 f...
  • wangyaninglm
  • wangyaninglm
  • 2016-04-05 22:27
  • 2718

LeetCode 3 — Longest Substring Without Repeating Characters (C++ Java Python)

题目:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/] Given a string, find the length of the longest substring without r...
  • dragon_dream
  • dragon_dream
  • 2014-02-18 22:22
  • 4657

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-27 12:55
  • 5788

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is ...
  • banana1006034246
  • banana1006034246
  • 2017-07-15 17:35
  • 305

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 letter...
  • hu1020935219
  • hu1020935219
  • 2014-08-03 20:38
  • 7646

Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode

Method 1 (Simple) We can consider all substrings one by one and check for each substring whether it contains all unique characters or not. There will ...
  • hellobinfeng
  • hellobinfeng
  • 2014-01-05 01:56
  • 3311
    个人资料
    • 访问:391422次
    • 积分:6805
    • 等级:
    • 排名:第4127名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论