leetcode第三题

题目描述:

找出一串字符串中不含重复字母的最长字串

 解答:

 mounted() {
    let result = this.findLongestString("afahdkjuhaivs")
    console.log("result = " + result);
  },
  methods: {
    /*
    * 使用滑动窗口,将队列的左边的元素移出,一直维持这样的队列,找出队列出现最长的长度时候
    * */
    findLongestString(s) {
      //ES6中的set函数本身是一个构造函数
      let res = 0, left = 0, set = new Set();
      //遍历字符串
      for (let i = 0; i < s.length; i++) {
        left = Math.max(left, set[s[i]] || 0);
        set[s[i]] = i + 1;
        res = Math.max(res, i - left + 1);
      }
      return res;
    }
原文地址:https://www.cnblogs.com/lilililiwang/p/14780585.html