Comments (3)
这题还是有点难度的,一开始是打算用“双指针”来解的,后来感觉用正则表达式更优雅。
const noRepeatSubstrLen = str => {
let len = str ? str.length : 0;
// 空字符和1个字符的情况
if (len < 2) {
return len;
}
let arr = [
...new Set( // 去重
str.split(/(.+?)(\1)/g) // 贪婪模式匹配所有连续字符
)
].map(s => s.length); // 去重后统计每项的字符串长度
return Math.max.apply(null, arr);
}
console.log(noRepeatSubstrLen('')); // 0
console.log(noRepeatSubstrLen('a')); // 1
console.log(noRepeatSubstrLen('ab')); // 2
console.log(noRepeatSubstrLen('aa')); // 1
console.log(noRepeatSubstrLen('abcabcbb')); // 3
console.log(noRepeatSubstrLen('bbbbb')); // 1
console.log(noRepeatSubstrLen('pwwkew')); // 3
console.log(noRepeatSubstrLen('qqwewert')); // 2
from fe-practice-hard.
const get666Length = s => {
if (s.length < 2) return s.length;
let index,
i = 1,
discardLength = 0,
max = 0;
for (; i < s.length; i++) {
index = s.lastIndexOf(s[i], i - 1);
if (index !== -1) {
max = Math.max(max, i - discardLength);
discardLength = Math.max(discardLength, index + 1);
}
}
return Math.max(max, i - discardLength);
};
from fe-practice-hard.
我的怕是会超时⊙﹏⊙
// 求无重复字符的最长子串
const getMaxCount = (string) => {
// 计算最长不重复连续字符的个数
const getCount = (string, lastStr = '') => {
lastStr = lastStr ? lastStr : string[0];
for (let i = 1; i < string.length; i++) {
if (lastStr.includes(string[i])) {
// 有重复的,直接输出
return lastStr.length;
} else {
// 没有重复的,继续循环
lastStr += string[i];
getCount(string, lastStr);
}
}
return lastStr.length;
};
let max = 0;
for (let i = 0; i < string.length; i++) {
let count = getCount(string.slice(i));
if (count > max) {
max = count;
}
}
return max;
};
from fe-practice-hard.
Related Issues (20)
- 第 110 期(数据结构-数组):清空数组
- 第 111 期(W3C 标准-JavaScript-Date):五种获取指定日期毫秒数的方法
- 第 112 期(W3C 标准-HTML):table的colgroup和col
- 第 113 期(W3C 标准-JavaScript-异步):async 和 await
- 第 114 期(W3C标准-CSS-伪类&伪元素):focus-within 伪类
- 第 115 期(W3C标准-CSS-语法):CSS 单位
- 第 116 期(W3C 标准-JavaScript):atob & btoa —— base64 的解码编码
- 第 117 期(JavaScript-DOM):监听页面DOM元素的插入
- 第 118 期(算法-排序):数组乱序
- 第 119 期(JavaScript-DOM):DocumentFragment - 文档片段
- 第 120 期(W3C 标准-JavaScript-事件):pagehide 和 pageshow
- 第 121 期(W3C 标准-JavaScript-DOM):dataset
- 第 122 期(数据结构-哈希):哈希表
- 第 123 期(W3C标准-CSS-伪类&伪元素):表单验证伪类
- 第 124 期(W3C 标准-JavaScript):unicode码互转
- 第 125 期(算法-递归):统计子类目数量
- 第 126 期(W3C 标准-JavaScript):iframe父子通信
- 第 127 期(W3C标准-CSS-绘制):border-radius
- 第 128 期(技巧):关于logo显示的SEO小技巧
- 第 129 期(W3C标准-CSS-动画):跳动的心 for 10.24程序员节
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fe-practice-hard.