大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
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); }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/117555.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...