package com.xxx;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArgsTest {
public int lengthOfLongestSubstring(String s) {
//对传入参数进行校验
if (s == null || s.length() <= 0) {
return 0;
}
if (s.length() == 1) {
return 1;
}
//将结果放进一个可排序的集合中
List list = new ArrayList<Integer>();
String expectResult = "";
char[] array = s.toCharArray();
//循环,将不重复的子串的长度全部放入集合中
for (int i=0; i<array.length; i++) {
for (int j=i; j<array.length; j++) {
if (!expectResult.contains(String.valueOf(array[j]))) {
expectResult += array[j];
} else {
list.add(expectResult.length());
expectResult = "";
break;
}
}
}
//对集合进行排序(升序)
Collections.sort(list);
//返回集合中的最后一个元素
return (Integer) list.get(list.size() - 1);
}
[@Test](https://my.oschina.net/azibug)
public void myTest() {
String params = "abcadcdb";
int value = lengthOfLongestSubstring(params);
System.out.println(value);
}
}
转载于:https://my.oschina.net/maliang1989/blog/2996171