New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第 4 题:字符串出现的不重复最长长度 #10
Comments
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
const arr = [...s]
let res = 1;
let result = arr.reduce((total, cur, i, arr) => {
if (i == 0) {
return cur;
} else {
if (total.indexOf(cur) < 0) {
return total + cur
} else if (res < total.length) {
res = total.length
return total.slice(total.indexOf(cur) + 1, total.length) + cur
} else {
return total.slice(total.indexOf(cur) + 1, total.length) + cur
}
}
}, "")
if (res < result.length) {
res = result.length
}
return res
};
console.log(lengthOfLongestSubstring("loddktdji"))
console.log(lengthOfLongestSubstring("dvdf"))
console.log(lengthOfLongestSubstring("adfafwefffdasdcx")) |
/**
* 题目:字符串出现的不重复最长长度
* 整体思路:
* 用一个滑动窗口装没有重复的字符,枚举字符记录最大值即可
* 对于遇到重复字符如何收缩窗口大小?
* 我们可以用 map 维护字符的索引,遇到相同的字符,把左边界移动过去即可
* 挪动的过程中记录最大长度
*/
var lengthOfLongestSubstring = function (s) {
let map = new Map();
let i = -1
let res = 0
let n = s.length
for (let j = 0; j < n; j++) {
if (map.has(s[j])) {
i = Math.max(i, map.get(s[j]))
}
res = Math.max(res, j - i)
map.set(s[j], j)
}
return res
}; |
|
const lengthOfLongestSubString = (s: string) => {
let left = 0,
right = 0,
res = 0,
map = new Map();
while (right < s.length) {
if (map.has(s[right])) {
left = Math.max(left, right);
}
res = Math.max(res, right - left + 1);
map.set(s[right], right);
right++;
}
return res;
};
console.assert(lengthOfLongestSubString("") === 0);
console.assert(lengthOfLongestSubString("aa") === 1);
console.assert(lengthOfLongestSubString("aabc") === 3); |
function lenSetStr (str) {
return [...new Set(str)].length
}
|
不是这样吧 |
麻烦把题目表述清楚行吗?装什么呢,中文没学好? |
@wangyingjie123 楼上的,写的对不对自己没数吗?能不能自己充分验证了再发出来? |
function lengthOfStr (str) {
let res = ""
let result = [...str].reduce((acc, v, i) => {
if (i === 0) {
return v;
} else {
if (acc.indexOf(v) < 0) {
return acc + v
} else {
res = res.length < acc.length ? acc : res
return acc.slice(acc.indexOf(v) + 1, acc.length) + v
}
}
}, "")
console.log(res.length > result.length ? res : result)
return Math.max(res.length, result.length)
} |
你这何止是不会调格式,你这是不会读题啊! |
var str = 'abacvdtrgasgdoijmiqunvlhsdanwqp'; |
/*
* @Author: codytang
* @Date: 2020-07-18 17:54:20
* @LastEditTime: 2020-07-18 18:01:55
* @LastEditors: codytang
* @Description: 第 4 题:字符串出现的不重复最长长度
*/
function longNoRepeatStr(str) {
let max = -Infinity;
let count = 0;
let pre = null;
for (let index = 0; index < str.length; index += 1) {
const cur = str[index];
if (cur !== pre) {
count += 1;
} else {
count = 1;
}
pre = cur;
if (count > max) max = count;
}
return max;
}
console.log(longNoRepeatStr("aabccddabcde")); //6 |
var lengthOfLongestSubstring = function (s = '') { |
|
遍历的同时,进行截取比较 let str = 'fdsfsdzffdfdddfsdsds'
let arr = []
const s = str.split('')
let total= 0; // 长度
let maxStr = '' // 最长不重复长度的字符串
for(var i = 0;i <s.length;i++){
const ele = s[i]
const idx = arr.indexOf(ele)
if(idx>-1){
arr = arr.slice(idx+1)
}
arr.push(ele)
if(arr.length>total){
maxStr = arr.join('')
total = arr.length
}
}
console.log(total)
console.log(maxStr) |
const twoSum = function(nums, target) { |
function fn(s) {
let temps = '',max=0
for(let i=0;i<s.length;i++){
let current = s[i]
if(temps.indexOf(s[i]) !== -1){
temps = temps.substring(temps.indexOf(s[i])+1)
}
temps += current
max = Math.max(temps.length, max)
}
return max
} |
|
function longNoRepeatStr(str) {
if (Object.prototype.toString.call(str) !== '[object String]') throw 'unvalid string'
let arr = str.split('')
return Array.from(new Set(arr)).length
}
console.log(longNoRepeatStr('ssdfsd')) |
/**
* @param str {string}
* 使用快慢双指针;对比快慢指针所指的值,如果不相等则计数加1,否则计数重置
*/
function maxNoRepeatStr (str) {
// 为空时,直接返回0
if (!str) {
return 0;
}
// 慢指针指向0
let slow = 0;
// 快指针指向1
let fast = 1;
// 计数默认为1 - 至少一个不重复
let count = 1;
// 当快指针走到结尾时停止
while (str.length > fast) {
// 不相等,计数加1
if (str[slow] !== str[fast]) {
count++;
} else {
// 重置为1
count = 1;
}
// 快慢指针同时走
slow++;
fast++;
}
return count;
}
console.assert(maxNoRepeatStr("loddktdji") === 6);
console.assert(maxNoRepeatStr("dvdf") === 4);
console.assert(maxNoRepeatStr("adfafwefffdasdcx") === 7); |
function strNorepeatMaxLength (str) { |
|
str.length? |
|
/**
* @param {string} s
* @return {number}
*/
function lengthOfLongestSubstring (str) {
if (str.length === 0 || str.length === 1) {
return str.length;
}
let start = 0;
let end = 0;
let maxStr = str[0];
let location = {
[str[0]]: 0
}
for (let index = 1; index < str.length; index++) {
const char = str[index];
if (typeof location[char] !== 'undefined') {
if (location[char] >= start) {
start = location[char] + 1;
}
}
location[char] = index;
end++;
if ((end - start + 1) > maxStr.length) {
maxStr = str.substring(start, end + 1);
}
}
return maxStr.length;
} |
说下思路,每个字符比较,如果有相同就保存当前length,没有就++,最后对比。 |
// 第 4 题:字符串出现的不重复最长长度
function lengthOfLongestSubstring(s) {
let max = 0; // 最长长度
let dict = {}; // 纪录当前字符串的下标位置
let i = 0; // 遍历 字符串s 的指针
let cur = 0; // 当前无重复字符串的指针
while (i < s.length) {
if (!dict[s[i]]) {
dict[s[i]] = String(i); // 再次遍历, i 在 字符串s 的位置
i++;
cur++;
} else {
i = dict[s[i]] + 1;
cur = 0;
dict = {};
}
if (cur > max) {
max = cur;
}
}
return max;
}
console.log(lengthOfLongestSubstring("loddktdji"));
console.log(lengthOfLongestSubstring("dvdf"));
console.log(lengthOfLongestSubstring("adfafwefffdasdcx")); |
const getLongestStrLen = (s) => {
const len = s.length;
let str = '';
let res = 0;
let char, index;
for(let i=0; i<len; i++){
char = s.charAt(i);
index = str.indexOf(char);
if(index === -1){
str += char;
res = res < str.length ? str.length : res;
} else{
str = str.substr(index + 1) + char;
}
}
return res;
} |
滑动窗口
|
console.assert(maxNoRepeatStr("asyncDataloddktdji") === 6); // true |
审题不对 |
// 'a' // 判重
} findMaxDiffSub(str) |
// 快慢指针 function maxNoRepeatStr(str) { // 扫描整个字符串 // test |
滑动窗 /**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let
finalLength = 0,
tempLength = 0,
// window的结构为key(字母表示):value(字母下标)
window = {},
left = 0,
right = 0;
while (left < s.length && right < s.length) {
// 如果记录表中没有,或记录的位置不在滑动窗口内,则继续扩张
if (window[s[right]] === undefined || window[s[right]] < left) {
window[s[right]] = right;
tempLength = right - left + 1;
finalLength = finalLength >= tempLength ? finalLength : tempLength;
}
// 否则,收缩窗口至重复的下一个
else {
left = window[s[right]] + 1;
window[s[right]] = right;
tempLength = right - left + 1;
}
right++;
}
return finalLength;
}; |
function strLen(str) {
} |
const noRepeatStrLen = (s) => {
let tmpStr = '';
for (let i = 0; i < s.length; i++) {
if (tmpStr.indexOf(s[i]) === -1) {
tmpStr += s[i];
} else {
return tmpStr;
}
}
return tmpStr;
}
console.log(noRepeatStrLen('abcabc'));
console.log(noRepeatStrLen('abcd123'));
console.log(noRepeatStrLen('aa24')); |
题目竟然没读懂。。。 |
function deStr(str) {
let tmpStr = '';
let maxStr = '';
for(let i = 0; i < str.length; i++) {
const index = tmpStr.indexOf(str[i]);
if(index != -1) { // 原来的不重复字符串,找到重复的字符
if(maxStr.length <= tmpStr.length) {
maxStr = tmpStr;
}
tmpStr += str[i];
tmpStr = tmpStr.slice(index + 1);
}else {
tmpStr += str[i];
}
}
return maxStr ? maxStr : str;
}
deStr('abacbefkb') |
function strLength (str) {
let index = 1
let stack = ''
let maxStr = 0
let same = 0
while (index <= str.length) {
let text = str.substring(index, 0)
if(stack.substr(stack.length - 1, 1) === text.substr(text.length - 1, 1)) {
same = text.length - 1
if(stack.length > maxStr) {
maxStr = stack.length
}
} else {
stack = text.substring(same)
}
index ++
}
return stack.length > maxStr ? stack.length : maxStr
} 把自己的拿上来,看看跟大佬的差距有多少 |
function maxRepeat(s){
let list=Array.from(s);
let map=new Map();
let max=0;
for(let i=0;i<list.length;i++){
if(map.has(list[i])){
map.set(list[i],map.get(list[i])+1);
}else{
map.set(list[i],1);
}
if(map.get(list[i])>max){
max=map.get(list[i]);
}
}
return max;
}
console.log(maxRepeat("aaabbbbccccc")); |
|
maxLength('abbba1234567')就不行了 |
|
const lengthOfLongestSubstring = function (s) {
let map = new Map();
let i = -1
let res = 0
let n = s.length
for (let j = 0; j < n; j++) {
if (map.has(s[j])) {
i = Math.max(i, map.get(s[j]))
}
res = Math.max(res, j - i)
map.set(s[j], j)
}
return res
};
console.log(lengthOfLongestSubstring("abcabcdeabcd")) |
function lengthOfLongestSubstring(s){
let i = 0
let j = 0
let n = s.length
let set = new Set()
let max = 0
while(j < n){
while(set.has(s[j])){
set.delete(s[i++])
}
set.add(s[j])
j++
max = Math.max(max, j - i)
}
return max
} |
|
function lengthOfLongestSubString (str){ |
function maxSubString(str) { |
function Fn(str) { |
/**
* 最长无重复子串
* 移动窗口法
* @param {*} str
*/
function maxLength(str) {
const set = new Set();
let max = ""; // 最大子串
let left = 0; // 左指针left,右指针i
for (let i = 0; i < str.length; i++) {
const char = str[i];
// 有重复,左指针移到右指针,清空set
if (set.has(char)) {
left = i;
set.clear();
} else {
// 如果窗口扩大了,就记录新的子串
if (i - left + 1 > max.length) {
max = str.slice(left, i + 1);
}
}
set.add(char);
}
console.log(max);
}
maxLength("abcabcdagaiuytaabb"); // 输出aiuyt |
`var lengthOfLongestSubstring = function (s) { };` |
function findMaxSizeDiffOfStr(strInput){ // findMaxSizeDiffOfStr("abccccfedggggggg"); |
function fn(s) {
let res = ''
let max = 0
for (let c of s) {
let index = res.indexOf(c)
if (index === -1) {
res += c
} else {
res = res.slice(index + 1) + c
}
max = Math.max(res.length, max)
}
return max
} |
|
利用双指针 迭代过程中更新最大长度 function fn(str) {
let left = 0, right = 0
let maxLen = 0
while(left <= right && right < str.length) {
const index = str.slice(left, right + 1).indexOf(str[right + 1])
maxLen = Math.max(maxLen, str.slice(left, right + 1).length)
if (index > -1) {
right++
left = right
} else {
right++
}
}
return maxLen;
} |
应该是力扣上这个题吧:https://leetcode.cn/problems/longest-substring-without-repeating-characters/description/
|
欢迎在下方发表您的优质见解
The text was updated successfully, but these errors were encountered: