NO.3 longest substring without repeating characters
Posted maskisland
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NO.3 longest substring without repeating characters相关的知识,希望对你有一定的参考价值。
暴力法:
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的主要内容,如果未能解决你的问题,请参考以下文章
Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters