【LeetCode】3. Longest Substring Without Repeating Characters

Introduce

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
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: 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.

Solution

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        max_value = 0
        if len(s) <= 1:
            return len(s)
        else:
            for i in range(len(s)):
                res_list = [s[i]]
                for j in range(i+1, len(s)):
                    if s[j] not in res_list:
                        res_list.append(s[j])
                        max_value = max(max_value, len(res_list))
                    else:
                        max_value = max(max_value, len(res_list))
                        break
            return max_value

https://leetcode.com/submissions/detail/211711430/

Code

GitHub:https://github.com/roguesir/LeetCode-Algorithm

  • 更新時間:2019-03-02
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章