# 字符串出现的不重复最长长度
# 昨日回顾
# 1. 归并排序
// 1. reduce
function a1(arr){
return arr.reduce((acc, cur)=>{
return Array.isArray(cur)? [...acc, ...a1(cur))]:[...acc, cur];
},[])
}
// 2. concat
function a2(arr){
let arrCopy = [...arr];
while(arrCopy.some(item=>Array.isArray(item))){
arrCopy = [].concat([...arrCopy]);
}
return arrCopy;
}
// 3. flat Infinity
function a3(arr){
return arr.flat(Infinity).sort((a-b)=>a-b);
}
// 4. merge mergeSort reduce
function a4(arr){
function flat(aa){
return aa.reduce((acc, cur)=>{
return Array.isArray(cur)? [...acc, ...flat(cur)]:[...acc, cur];
},[]);
}
function mergeSort(arrS){
if(arrS.length<2) return arrS;
let middle = arrS>>1;
let left = arrS.slice(0, middle);
let right = arrS.slice(middle);
return merge(mergeSort(left), mergeSort(right));
}
function merge(left, right){
let arrC;
while(left.length && right.length){
if(left.shift()<right.shift()){
arrC.push(left.shift());
} else{
arrC.push(right.shift());
}
}
while(left.length){
accC.push(left.shift())
}
while(right.length){
accC.push(right.shift())
}
return accC;
}
return mergeSort(flat(arr));
}
# 2. 斐波那契
function fibonacci1(n, v1 = 1, v2 = 1) {
if (n == 1) return v1;
if (n == 2) return v2;
return fibonacci1(n - 1, v2, v1 + v2);
}
function fibonacci2(n) {
let pre = 1;
let cur =1;
let data;
if(n==1 || n==2) return 1;
for(let k =3; k<=n; k++){
data = pre+cur;
pre=cur;
cur=data;
}
return data;
}
function
# 今日解题
let str = "fdsfsdzffdfdddfsdsds";
let arr = [];
const s = str.split("");
let total = 0; // 长度
let maxStr = ""; // 最长不重复长度的字符串
for (let i = 0; i < s.length; i++) {
const ele = s[i];
const idx = arr.indexOf(ele);
if (idx > -1) {
arr = arr.slice(idx + 1);
console.log(arr);
}
arr.push(ele);
if (arr.length > total) {
maxStr = arr.join("");
total = arr.length;
}
}
console.log(total, maxStr);