【LeetCode】3. Longest Substring Without Repeating Characters

Myth丶恋晨 2022-03-15 12:26 261阅读 0赞

Introduce

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

  1. Input: "abcabcbb"
  2. Output: 3
  3. Explanation: The answer is "abc", with the length of 3.

Example 2:

  1. Input: "bbbbb"
  2. Output: 1
  3. Explanation: The answer is "b", with the length of 1.

Example 3:

  1. Input: "pwwkew"
  2. Output: 3
  3. Explanation: The answer is "wke", with the length of 3.
  4. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solution

  1. class Solution(object):
  2. def lengthOfLongestSubstring(self, s):
  3. """ :type s: str :rtype: int """
  4. max_value = 0
  5. if len(s) <= 1:
  6. return len(s)
  7. else:
  8. for i in range(len(s)):
  9. res_list = [s[i]]
  10. for j in range(i+1, len(s)):
  11. if s[j] not in res_list:
  12. res_list.append(s[j])
  13. max_value = max(max_value, len(res_list))
  14. else:
  15. max_value = max(max_value, len(res_list))
  16. break
  17. return max_value

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

Code

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

  • 更新时间:2019-03-02

发表评论

表情:
评论列表 (有 0 条评论,261人围观)

还没有评论,来说两句吧...

相关阅读