题解 | 双指针解法

最长不含重复字符的子字符串

http://www.nowcoder.com/practice/48d2ff79b8564c40a50fa79f9d5fa9c7

双指针解法 通过所有算例,耗时短,供参考

    def lengthOfLongestSubstring(self , s: str) -> int:
        cnt = 1
        i = 0
        k = 1
        
        while i < len(s)-1 and k < len(s):
            if s[k] not in s[i: k]:
                cnt = max(cnt, len(s[i: k+1]))
                k += 1
            elif s[k] in s[i: k]:
                i += 1
                k = i+1
        
        return cnt
全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务