网站建设注册名有规范吗,本地wordpress怎么创建2个网址,托管机构,房产信息网显示限售查找不重复字符的最长子字符串#xff08;编程面试中常见题-用8种编程语言来回答#xff09;给定一个字符串str#xff0c;找到不重复字符的最长子字符串。比如我们有 “ABDEFGABEF”, 最长的字符串是 “BDEFGA” 和 “DEFGAB”, 长度为6.再如 “BBBB” 最长字符串是 “B”,… 查找不重复字符的最长子字符串编程面试中常见题-用8种编程语言来回答给定一个字符串str找到不重复字符的最长子字符串。比如我们有 “ABDEFGABEF”, 最长的字符串是 “BDEFGA” 和 “DEFGAB”, 长度为6.再如 “BBBB” 最长字符串是 “B”, 长度为1.再比如 “neatcoding” 最长字符串是“neatcodi”, 长度为8.所需的时间复杂度为On其中n是字符串的长度。我们将逐个字符地遍历该字符串检查此字符是否在当前子字符串中如果是则将当前子字符串保存到子字符串集合中使用此字符作为起始值创建一个新的子字符串 如果否则将此字符添加到当前子字符串中至此循环结束检查当前子字符串是否为空如果是则不执行任何操作 如果否请将其添加到子字符串集合我们设置一个对象来存储最长的子字符串现在让我们遍历子字符串集合找到最长的一个 检查当前子字符串是否更长如果是则替换最长的字符串 如果没有什么也不做最后我们有最长的子字符串。让我们编码。In Javascript:returnLongestNonRepeatSubString(s){ if(!s){ return ; } let subCollection []; let currentSub ; for(let i 0; i s.length; i ) { let c s[i]; if(currentSub.indexOf(c) -1) { currentSub c; } else { subCollection.push(currentSub); currentSub c; } } if(currentSub.length 0){ subCollection.push(currentSub); } let longest ; for(let i 0 ; i subCollection.length ; i ) { let sub subCollection[i]; if(sub.length longest.length) { longest sub; } } return longest; }In C#: string returnLongestNonRepeatSubString(string s) { if (string.IsNullOrEmpty(s)) { return ; } Liststring subCollection new Liststring(); string currentSub ; for (int i 0; i s.Length; i) { char c s[i]; if (currentSub.IndexOf(c) -1) { currentSub c; } else { subCollection.Add(currentSub); currentSub c; } } if (currentSub.Length 0) { subCollection.Add(currentSub); } string longest ; for (int i 0; i subCollection.Count; i) { string sub subCollection[i]; if (sub.Length longest.Length) { longest sub; } } return longest; }In Java:String returnLongestNonRepeatSubString(String s){ if (s null || s.length() 0) { return ; } ListString subCollection new ArrayListString(); String currentSub ; for (int i 0; i s.length(); i) { char c s.charAt(i); if (currentSub.indexOf(c) -1) { currentSub c; } else { subCollection.add(currentSub); currentSub c; } } if (currentSub.length() 0) { subCollection.add(currentSub); } String longest ; for (int i 0; i subCollection.size(); i) { String sub subCollection.get(i); if (sub.length() longest.length()) { longest sub; } } return longest;}In Kotlin:fun returnLongestNonRepeatSubString(s: String?): String { if (s null || s.length 0) { return } val subCollection ArrayListString() var currentSub for (i in 0 until s.length) { val c s[i] if (currentSub.indexOf(c) -1) { currentSub c } else { subCollection.add(currentSub) currentSub c } } if (currentSub.length 0) { subCollection.add(currentSub) } var longest for (i in subCollection.indices) { val sub subCollection[i] if (sub.length longest.length) { longest sub } } return longest}In Golang:func returnLongestNonRepeatSubString(s string) string { if len(s) 0 { return } subCollection : make([]string, 1) currentSub : for i : 0; i len(s); i { var c s[i] if !strings.Contains(currentSub, string(c)) { currentSub string(c) } else { subCollection append(subCollection, currentSub) currentSub string(c) } } if len(currentSub) 0 { subCollection append(subCollection, currentSub) } var longest for _, sub : range subCollection { if len(sub) len(longest) { longest sub } } return longest}In c:stringreturnLongestNonRepeatSubString (const string s){ if (s.empty ()) { return ; } std::vector string subCollection; string currentSub; for (int i 0; i s.length (); i) { char c s[i]; if (currentSub.find (c) -1) { currentSub c; } else { subCollection.push_back (currentSub); currentSub string (1, c); } } if(!currentSub.empty()) { subCollection.push_back (currentSub); } string longest ; for (int i 0; i subCollection.size(); i) { string sub subCollection.at(i); if (sub.length() longest.length()) { longest sub; } } return longest;}In Python:def returnLongestNonRepeatSubString(s): if not s: return subCollection [] currentSub for c in s: if (currentSub.find(c) -1): currentSub c else: subCollection.append(currentSub) currentSub c if currentSub: subCollection.append(currentSub) longest for sub in subCollection: if (len(sub) len(longest)): longest sub return longestIn Swift:func returnLongestNonRepeatSubString (s: String) - String { if (s nil || s.isEmpty) { return } var subCollection [String]() var currentSub for c in s { let i currentSub.firstIndex(of: c) if(i nil) { currentSub currentSub String(c) } else { subCollection.append(currentSub); currentSub String(c) } } var longest for sub in subCollection { if sub.length longest.length { longest sub } } return longest}