首页 > 代码库 > LeetCode——Longest Substring Without Repeating Characters
LeetCode——Longest Substring Without Repeating Characters
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.
给定一个字符串,找出最长的无重复子串。
public static int lengthOfLongestSubstring(String s) { if (s == null || s.length() == 0) return 0; int maxlen = 1; boolean[] flag = new boolean[256]; int start = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (flag[ch]) { maxlen = Math.max(maxlen, i - start); for (int j = start; j < i; j++) { if (s.charAt(j) == ch) { start = j + 1; break; } flag[s.charAt(j)] = false; } } else flag[ch] = true; } return Math.max(maxlen, s.length() - start); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。