* The algorithm often asked in interview or written examination import java.util.ArrayList; import java.util.Collections;
import java.util.Comparator; import org.apache.commons.lang.StringUtils; /* *
【Author】 Programmers who love breakfast * 【Time】2020 year 11 month 23 day afternoon 2:28:17 * 【Function】 Find the longest string that does not repeat */
public class Test5 { public static void main(String[] args) { String string =
"suhdfuisehwerqiowo"; String findNotDupLong = findNotDupLong(string); System.err
.println(" Don't repeat the longest string :"+findNotDupLong); } private static String findNotDupLong(
String string) { if (StringUtils.isBlank(string)) { return " Can't find "; } ArrayList<
String> arrayList = new ArrayList<String>();// Used to store qualified strings String[] split =
string.split(""); for (int i = 0; i < split.length; i++) { String begin = split[
i]; for (int j = i; j < split.length; j++) { if (begin.equals(split[j])) { if (
StringUtils.isNotBlank(string.substring(i, j))) { arrayList.add(string.substring
(i, j)); } } } } Collections.sort(arrayList, new Comparator<String>() {
@Override public int compare(String o1, String o2) { if (o1.length()>o2.length()
) { return -1; }else { return 1; } } }); return arrayList.size()>0?arrayList.get
(0):" Can't find "; } }
* Do you have any good methods
* Later, there was a test error Method updated private static String findNotDupLong(String string) { if (
StringUtils.isBlank(string)) { return " Can't find "; } ArrayList<String> arrayList =
new ArrayList<String>();// Used to store qualified strings String[] split = string.split(""); for (
int i = 0; i < split.length; i++) { for (int j = i; j < string.length(); j++) {
if (string.substring(i,j).contains(string.charAt(j)+"")) { System.err.println(
string.substring(i,j)+"==="+string.charAt(j)+""); arrayList.add(string.substring
(i,j)); i++; j=i; } } } Collections.sort(arrayList, new Comparator<String>() {
@Override public int compare(String o1, String o2) { if (o1.length()>o2.length()
) { return -1; }else if (o1.length()< o2.length()) { return 1; }else { return 0;
} } }); return arrayList.size()>0?arrayList.get(0):" Can't find "; }
The second method It's complicated It's hard to understand You can study it .
private static String findNotDupLong3(String dupString) { long
currentTimeMillis= System.currentTimeMillis(); char[] chars = dupString.
toCharArray(); int[] data = new int[1024]; for (int i = 0; i < data.length; i++)
{ data[i] = -1; } Stack<Character> stack = new Stack<Character>(); Stack<
Character> temp = new Stack<Character>(); int maxLength = 0; int index = 0; for
(char aChar : chars) { if (data[aChar] == -1) { data[aChar] = index++; // Simultaneous operation
If data If there is no data in the array Respectively Data in data and stack in stack.push(aChar); } else { if (stack
.size() > maxLength) { // If data There is data in the array It is necessary to stack Put the temporary data in stack in maxLength =
stack.size(); // The current maximum length is maxLength temp = (Stack<Character>)stack.clone(); }
stack.push(aChar); // Place the new repeating element in the stack in int popSize = stack.size() - index +
data[aChar]; // Find one size do { data[stack.get(0)] = -1; // data Existing in
stack The first element of is set to -1 stack.remove(0); // delete stack First element in }while (--popSize !=0); data
[aChar] = index++; // When popSize != 0 When data The location of this element in is index ++ } } if (stack.
size() > maxLength) { temp = stack; } String result = ""; int size = temp.size()
; for (int i = 0; i < size; i++) { result += temp.get(i); } System.out.println(
" method 3 time consuming :"+(System.currentTimeMillis()-currentTimeMillis) +" millisecond "); return result; }

Technology
©2019-2020 Toolsou All rights reserved,
switch Statement programming : Give a percentage score , Output grade required A,B,C,D,E.Jetson Nano read GPS data (TTL turn USB)yolov3 Detailed explanation - model structure Android Mobile application foundation learning —— Chapter 1 basic introduction Four years of programming , Or is it inferior Database design AWS Elastic Load Balancing (Amazon ELB) Study notes el-select Parameter problem From outsourcing testing to Alibaba , A girl of three books C++STL Container summary