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

最长不重复子串算法-初版 #21

Open
hstarorg opened this issue Jul 19, 2018 · 0 comments
Open

最长不重复子串算法-初版 #21

hstarorg opened this issue Jul 19, 2018 · 0 comments

Comments

@hstarorg
Copy link
Owner

function lengthOfLongestSubstring(text) {
  function hasRepeatChar(str) {
    var strArr = str.split('');
    var obj = {};
    strArr.forEach(x => (obj[x] = true));
    return strArr.length != Object.keys(obj).length;
  }

  var longStr = '';
  var len = text.length;
  var tempStr = '';
  for (var i = 0; i < len - longStr.length; i++) {
    tempStr = text[i];
    for (var j = i + 1; j < len; j++) {
      if (hasRepeatChar(tempStr + text[j])) {
        if (longStr.length < tempStr.length) {
          longStr = tempStr;
        }
        break;
      } else {
        tempStr += text[j];
      }
    }
    if (!hasRepeatChar(tempStr)) {
      if (longStr.length < tempStr.length) {
        longStr = tempStr;
      }
    }
  }
  // console.log(longStr);
  return longStr.length;
}
var len;
len = lengthOfLongestSubstring('abcdefghijklmn');
console.log(len, len === 14);
len = lengthOfLongestSubstring('cbca');
console.log(len, len === 3);
len = lengthOfLongestSubstring('c');
console.log(len, len === 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant