algorithms icon indicating copy to clipboard operation
algorithms copied to clipboard

问题3,最长公共不重复字符串貌似有更简单的解法

Open consoles opened this issue 7 years ago • 1 comments

function solve(str) {
	var maxStr = '',
	    tmpStr = '';

	var hashed = {};
	for (const c of str) {
		if (!hashed[c]) {
			hashed[c] = true;
			tmpStr += c;
		} else {
			if (tmpStr.length > maxStr.length) {
				maxStr = tmpStr;
				tmpStr = c;
			}
		}
	}

	return maxStr;
}

consoles avatar Feb 01 '18 04:02 consoles

@consoles 感谢指明! 每道题都应该会有其最优的解法,因为毕竟很多题我只写出了自己第一时间想到的方法。。而且随着题目序数的增大,我的题解应该会有所进步吧,毕竟自己是按照顺序从第一题开始刷的。。🌚

ecmadao avatar Feb 01 '18 04:02 ecmadao