c++ 查找 list中最长的字符串_查找不重复字符的最长子字符串(编程面试中常见题-用8种编程语言来回答)...
查找不重復(fù)字符的最長子字符串(編程面試中常見題-用8種編程語言來回答)
給定一個(gè)字符串str,找到不重復(fù)字符的最長子字符串。
比如我們有 “ABDEFGABEF”, 最長的字符串是 “BDEFGA” 和 “DEFGAB”, 長度為6.
再如 “BBBB” 最長字符串是 “B”, 長度為1.
再比如 “neatcoding” 最長字符串是“neatcodi”, 長度為8.
所需的時(shí)間復(fù)雜度為O(n),其中n是字符串的長度。
我們將逐個(gè)字符地遍歷該字符串
檢查此字符是否在當(dāng)前子字符串中,如果是,則將當(dāng)前子字符串保存到子字符串集合中,使用此字符作為起始值創(chuàng)建一個(gè)新的子字符串;
如果否,則將此字符添加到當(dāng)前子字符串中;
至此,循環(huán)結(jié)束,檢查當(dāng)前子字符串是否為空,如果是,則不執(zhí)行任何操作
如果否,請將其添加到子字符串集合
我們設(shè)置一個(gè)對象來存儲(chǔ)最長的子字符串,
現(xiàn)在,讓我們遍歷子字符串集合,找到最長的一個(gè)
檢查當(dāng)前子字符串是否更長,如果是,則替換最長的字符串
如果沒有,什么也不做
最后,我們有最長的子字符串。
讓我們編碼。
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 "";
}
List<string> subCollection = new List<string>();
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 "";
}
List<String> subCollection = new ArrayList<String>();
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 = ArrayList<String>()
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++:
string
returnLongestNonRepeatSubString (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 longest
In 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
}
總結(jié)
以上是生活随笔為你收集整理的c++ 查找 list中最长的字符串_查找不重复字符的最长子字符串(编程面试中常见题-用8种编程语言来回答)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推广的euclid_欧几里德(Eucli
- 下一篇: C++error:找不到指定文件