首页 > 学院 > 开发设计 > 正文

lengthOfLongestSubstring

2019-11-09 17:35:08
字体:
来源:转载
供稿:网友

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. * * 题目大意: * 给定一个字符串,找字符中的最大非重复子串 * Longest Substring Without Repeating Characters * 解题思路:当遇到字符串中相同的字符时,先计算runner和walker的差值,该差值就是当前不重复的字符的个runner跑的比较,快将walker移到前一个相同字符的位置并加1,下次再查找相同的字符时,就从当前的walker的位置开始查找。依次查找下去。

public int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) return 0; HashSet<Character> set = new HashSet<Character>(); int max = 0; int walker = 0; int runner = 0; while (runner < s.length()) { if (set.contains(s.charAt(runner))) { if (max < runner - walker) { max = runner - walker; } while (s.charAt(walker) != s.charAt(runner)) { set.remove(s.charAt(walker));** //我觉得这句话没有啥作用,因为他并没有正确的保存不重复的字符串, 所以删除也不影响结果的输出,如果有人觉得这句话有用,麻烦跟小弟说一下,谢谢** walker++; } walker++; } else { set.add(s.charAt(runner)); } runner++; } for (Character character : set) { System.out.PRintln(">>>>>>>>>>>>"+character); } max = Math.max(max, runner - walker); return max; } **这个代码时我看的一个大牛的博客,代码就是抄的人家的,我自己当做一个笔记,小弟将要大四毕业了,有没有要android开发的毕业生,求推荐(最好在北京)**
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表