码迷,mamicode.com
首页 > 其他好文 > 详细

Latched 3 Longest Substring Without Repeating Characters

时间:2015-06-11 18:25:40      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

使用Hash存储已经读入的字符,一个指针用于读入字符并存入Hash,发现Hash中存在相同字符后,另一个指针向前移动并删除Hash中之间的字符直至保持该监测到的字符唯一,并维护最大值。

def length_of_longest_substring(s)
    h = Hash.new
    max, i = 0, 0
    s.length.times do |j|
        while h[s[j]]
            h.delete(s[i])
            i += 1
        end
        h[s[j]] = 1
        max = j-i+1 > max ? j-i+1 : max
    end
    max
end

 

Latched 3 Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4569351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!