|
| 1 | +package com.leecode.week02; |
| 2 | + |
| 3 | +import com.sun.org.apache.xpath.internal.operations.Bool; |
| 4 | + |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ |
| 12 | + * 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 |
| 13 | + */ |
| 14 | +public class LeetCode_03_35 { |
| 15 | + public void printAllSubstring(String s){ |
| 16 | + char[] chs=s.toCharArray(); |
| 17 | + for(int i=0;i<s.length();i++){ |
| 18 | + for(int j=i+1;j<s.length();j++){ |
| 19 | + System.out.println(s.substring(i,j)); |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | + public static void main(String[] args) { |
| 24 | + LeetCode_03_35 lc=new LeetCode_03_35(); |
| 25 | + System.out.println(lc.lengthOfLongestSubstring("abcabcbb")); |
| 26 | + System.out.println(lc.lengthOfLongestSubstring("abc abcbb")); |
| 27 | + System.out.println(lc.lengthOfLongestSubstring("bb bbb")); |
| 28 | + System.out.println(lc.lengthOfLongestSubstring(" ")); |
| 29 | + System.out.println(lc.lengthOfLongestSubstring("au")); |
| 30 | + System.out.println(lc.lengthOfLongestSubstring("abcabcbb")); |
| 31 | + System.out.println(lc.lengthOfLongestSubstring("abbbcd")); |
| 32 | + |
| 33 | + //1.给定一个字符串,先输出看子串都有哪些 |
| 34 | + //暴力枚举 |
| 35 | + lc.printAllSubstring("abcabcbb"); |
| 36 | + |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + public int lengthOfLongestSubstring(String s){ |
| 42 | + if(s.isEmpty()){ |
| 43 | + return 0; |
| 44 | + } |
| 45 | + |
| 46 | + if(s.length()==1){ |
| 47 | + return 1; |
| 48 | + } |
| 49 | + |
| 50 | + int longest=0; |
| 51 | + |
| 52 | + char[] chs=s.toCharArray(); |
| 53 | + Set<Character> set=new HashSet<>(); |
| 54 | + for(int i=0;i<s.length();i++){ |
| 55 | + set.add(chs[i]); |
| 56 | + int subLen=1; |
| 57 | + for(int j=i+1;j<s.length();j++){ |
| 58 | + |
| 59 | + if(set.contains(chs[j])){ //有重复字符,则循环终止 |
| 60 | + set.clear(); |
| 61 | + break; |
| 62 | + }else{ |
| 63 | + set.add(chs[j]); |
| 64 | + } |
| 65 | + subLen++; |
| 66 | + } |
| 67 | + longest=Math.max(longest,subLen); |
| 68 | + |
| 69 | + set.clear(); |
| 70 | + |
| 71 | + } |
| 72 | + return longest; |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + public int lengthOfLongestSubstring_v1(String s) { |
| 77 | + if(s.isEmpty()){ |
| 78 | + return 0; |
| 79 | + } |
| 80 | + |
| 81 | + if(s.length()==1){ |
| 82 | + return 1; |
| 83 | + } |
| 84 | + |
| 85 | + int longest=0; |
| 86 | + |
| 87 | + char[] chs=s.toCharArray(); |
| 88 | + Set<Character> set=new HashSet<>(); |
| 89 | + for(int i=0;i<s.length();i++){ |
| 90 | + set.add(chs[i]); |
| 91 | + int subLen=1; |
| 92 | + for(int j=i+1;j<s.length();j++){ |
| 93 | + |
| 94 | + if(set.contains(chs[j])){ //有重复字符,则循环终止 |
| 95 | + set.clear(); |
| 96 | + break; |
| 97 | + }else{ |
| 98 | + set.add(chs[j]); |
| 99 | + } |
| 100 | + subLen++; |
| 101 | + } |
| 102 | + longest=Math.max(longest,subLen); |
| 103 | + |
| 104 | + set.clear(); |
| 105 | + |
| 106 | + } |
| 107 | + return longest; |
| 108 | + } |
| 109 | + |
| 110 | + /*Map<String, Boolean> cache=new HashMap<>(); |
| 111 | + public boolean hasRepeatingChar(String s){ |
| 112 | +
|
| 113 | + if(cache.containsKey(s)){ |
| 114 | + return cache.get(s); |
| 115 | + } |
| 116 | +
|
| 117 | + char[] chs=s.toCharArray(); |
| 118 | + Set<Character> characters=new HashSet<>(); |
| 119 | + for (char ch:chs) { |
| 120 | + if(characters.contains(ch)){ |
| 121 | + cache.put(s,true); |
| 122 | + return true; |
| 123 | + }else { |
| 124 | + characters.add(ch); |
| 125 | + } |
| 126 | +
|
| 127 | + } |
| 128 | + cache.put(s,false); |
| 129 | + return false; |
| 130 | + }*/ |
| 131 | +} |
0 commit comments