标签:span 暴力 nbsp main lse list system str tin
暴力法:
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.junit.Test; public class App { public static void main(String[] args) { String str="pwwkew"; // new App().lengthOfLongestSubstring(str); System.out.println(new App().lengthOfLongestSubstring(str)); } public int lengthOfLongestSubstring(String s) { List<String> list1=new ArrayList<String>();// 所有字符串组合 List<Integer> list2=new ArrayList<Integer>(); List<String> list3=new ArrayList<String>();// 去重后的组合 for(int i=0;i<s.length();i++) { for(int j=i;j<s.length();j++) { // System.out.println(s.substring(i,j+1)); list1.add(s.substring(i,j+1)); } } for(String str:list1) { // System.out.println(str); boolean flag=false; Set hs1=new HashSet<Character>(); for(int m=0;m<str.length();m++) { if(hs1.contains(str.charAt(m))) { flag=true; break; } else { hs1.add(str.charAt(m)); } } if(flag==false) { list2.add(str.length()); } // System.out.println(str.length()); // list2.add(str.length()); } // return 0; Collections.sort(list2); return list2.get(list2.size()-1); } @Test public void Test() { String string="abca"; Set hs1=new HashSet<Character>(); for(int m=0;m<string.length();m++) { if(hs1.contains(string.charAt(m))) { return; } else { hs1.add(string.charAt(m)); } } System.out.println(string); } }
NO.3 longest substring without repeating characters
标签:span 暴力 nbsp main lse list system str tin
原文地址:https://www.cnblogs.com/Maskisland/p/10025741.html