Leetcode 3 Longest Substring Without Repeating Characters
2015-06-11 16:05
477 查看
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中之间的字符直至保持该监测到的字符唯一,并维护最大值。
使用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
相关文章推荐
- [python]python三元表达式另类实现方式
- Linux使用 tar命令-g参数进行增量+差异备份、还原文件
- 华为内部的WEB安全原则
- EL表达式
- 重装Win7后找回Ubuntu启动项并在Ubuntu中修复引导
- memcached 之 哈希一致性 和 虚拟节点 分析
- VMware虚拟机配置Ubuntu桥接方式(Bridged)使虚拟机和宿主机能互相ping通
- Android官网开发文档API
- Android.mk 参数函数查询
- 15回文相关问题
- GCC 命令行具体解释
- 白衣倾相
- 二叉树遍历方法总结
- 5-3图层的层次关系
- 试用Redis
- linux下安装配置cacti监控
- linux下安装配置cacti监控
- ssl协议
- mysql 异常
- Hadoop配置文件参数整理(4)---yarn-site.xml