当前位置:   article > 正文

leetcode python - Longest Substring Without Repeating Characters_longest substring python

longest substring python
  1. # Given a string, find the length of the longest substring without repeating characters.
  2. #
  3. # Examples:
  4. #
  5. # Given "abcabcbb", the answer is "abc", which the length is 3.
  6. #
  7. # Given "bbbbb", the answer is "b", with the length of 1.
  8. #
  9. # Given "pwwkew", the answer is "wke", with the length of 3.
  10. # Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
  11. #
  12. class Solution(object):
  13. def lengthOfLongestSubstring(self, s):
  14. """
  15. :type s: str
  16. :rtype: int
  17. """
  18. i,j,k = 0,0,0
  19. map = {}
  20. while i < len(s) and j < len(s):
  21. if map.has_key(s[j]):
  22. i = max(i, map[s[j]] + 1)
  23. map[s[j]] = j
  24. k = max(k, j - i + 1)
  25. j += 1
  26. return k


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/507027
推荐阅读
相关标签
  

闽ICP备14008679号