力扣题目
无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
示例 1:
输入: “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
示例 2:
输入: “bbbbb”
输出: 1
解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
示例 3:
输入: “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
package leetcode.meituan;
import java.util.*;
public class 无重复字符的最长子串 {
//思路新数字每次加都记录,为负删除,取最大值
public int lengthOfLongestSubstring(String s) {
int a=0,b=0,c=0;
boolean e=false;
ArrayList<Object> d =new ArrayList<>(10);
String str = s;
for (int i = 0; i < str.length(); i++) {
if(d.contains(str.charAt(i))==false) {//如果第一位为负,就比较到正的之前为最大
d.add(str.charAt(i));
if (a <= c + 1) {
a = c + 1;
}
c++;
}
else{
d.add(str.charAt(i));
b=d.indexOf(str.charAt(i));
for(int m=0;m<=b;m++)
{
d.remove(0);
}
c=c-b;
}
}
return a;
}
}
//测试用例
class Main{
public static void main(String[] args) {
String s="ohomm";
无重复字符的最长子串 a=new 无重复字符的最长子串();
System.out.println(a.lengthOfLongestSubstring(s));
}
}