The solution refers to the official solution on leetcode.
class Solution {
public int lengthOfLongestSubstring(String s) {
/ / a hash collection that records whether each character has occurred
Set<Character> occ = new HashSet<Character>();
int n = s.length();
/ / the right pointer, whose initial value is - 1, is equivalent to that we are on the left side of the left boundary of the string and have not started moving
int rk = -1, ans = 0;
for (int i = 0; i < n; ++i) {
if (i != 0) {
/ / move the left pointer one space to the right to remove a character
occ.remove(s.charAt(i - 1));
}
while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
/ / keep moving the right pointer
occ.add(s.charAt(rk + 1));
++rk;
}
/ / characters i to rk are a very long non repeating character string
ans = Math.max(ans, rk - i + 1);
}
return ans;
}
}
Slide the window and store the strings in the window into the hash set one by one. When the next character does not exist in the hash set, move the right boundary of the window one bit to the right and judge again. If the character exists in the hash set, move the left boundary to the right and judge again. After each move of the right boundary to the right, judge whether the string length in the window is greater than the maximum value of the tag, Greater than, replace storage. When the window sliding is completed, the maximum value of the tag is returned.
Take this opportunity to record relevant knowledge such as hash table. Hash table, also known as hash table, is a data structure accessed according to the key. The hash function converts the key into a hash value to locate the location of data storage. It is usually a key value pair structure, that is, the hash value calculated by the key is used to locate the storage location, and the content stored in the location is value. Hash set (hashset) is also a data structure that locates the storage location through the hash value, but it is not a key value pair structure, but stores the key itself, which is equivalent to only a hash table The key part of (hashtable), that is, the hash value calculated by the key is used to locate the storage location, and the storage content in this location is the key itself. In short, a hash set is a data structure (set) that cannot store duplicate elements, while a hash table is a storage key value pair, in which the key key cannot be repeated. A hash map is a hash table (hashtable) a similar data structure is also a key value pair storage, but the hash mapping is thread safe, while the hash table is non thread safe. The so-called thread safety is to ensure that only one thread can access the same data at the same time when multiple threads operate data at the same time (that is, it locks data operations); if you can't ensure this, it's non thread safe -- refer to a comment named qybao.
Use of hash sets
/*Add element: hashset.add(E e): Returns boolean type. If the set does not contain the specified element, the specified element will be added; If this set already contains the element, the call does not change the set and returns false. Delete element: hashset.clear(): Remove all elements from this set. hashset.remove(Object o): If the specified element exists in this set, it is removed. hashset.isEmpty(): Returns true if the set does not contain any elements. hashset.contains(Object o): Returns true if this set contains the specified element. hashset.size(): Returns the number of elements in this set (the capacity of the set)*/ HashSet hashset=new HashSet();Creation of hash collection towards hashset Add a string to the hashset.add("abc"); //Add an integer to the hashset hashset.add(1); //Add a character to the hashset hashset.add('a'); //Add an array to the hashset int[] abc={10,11,12}; hashset.add(abc); //Add a custom object to the hashset Cat cat1=new Cat("asd", 2); hashset.add(cat1);//Add an object to the hashset //Traverse HashSet Iterator it = hashset.iterator(); while(it.hasNext()) { Object obj = it.next(); if(obj instanceof Integer) { System.out.println("Integer:"+obj); } if(obj instanceof String) { System.out.println("String:"+obj); } if(obj instanceof Character) { System.out.println("Character:"+obj); } if(obj instanceof int[]) { System.out.print("int[]:"); for(int i=0;i<abc.length;i++) { System.out.print(abc[i]+" "); } } }
——Turn from https://blog.csdn.net/tingzhiyi/article/details/52152487