Skip to content
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

2018/08/14 利用Set结构做的一道题 #26

Open
ChenPt opened this issue Aug 14, 2018 · 0 comments
Open

2018/08/14 利用Set结构做的一道题 #26

ChenPt opened this issue Aug 14, 2018 · 0 comments
Labels
笔试题 校招遇到做错的笔试题或者有趣的笔试题

Comments

@ChenPt
Copy link
Owner

ChenPt commented Aug 14, 2018

原题:

题目的意思,给定一个字符串,返回不包含相同字符的最长子串的长度
采用滑动窗口的形式

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    var res = 0, i = 0,  j = 0
    var length = s.length
    var set = new Set()
    while(i < length && j < length) {
        if(!set.has(s.charAt(j))) {
            set.add(s.charAt(j++))
            res = Math.max(res, j - i)
        } else {
            set.delete(s.charAt(i++))
        }
    }
    return res
};

lengthOfLongestSubstring("abcabcbb")

image

@ChenPt ChenPt added the 笔试题 校招遇到做错的笔试题或者有趣的笔试题 label Aug 14, 2018
@ChenPt ChenPt changed the title 利用Set结构做的一道题 2018/08/14 利用Set结构做的一道题 Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
笔试题 校招遇到做错的笔试题或者有趣的笔试题
Projects
None yet
Development

No branches or pull requests

1 participant