LeetCode Longest Substring Without Repeating Characters

来源:互联网 发布:ubuntu 输入法只有五笔 编辑:程序博客网 时间:2024/05/18 03:36

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

class Solution {public:int lengthOfLongestSubstring(string s) {int n = s.length();int lengest = 0, count = 0, start = 0;if (n == 0)return count;for (int i = 0; i < n; i++) {int j = start;while (j < i) {if (s[j] == s[i])break;elsej++;}if (j < i) {if (count > lengest)lengest = count;count -= j - start;start = j + 1;continue;}else {count++;if (i == n - 1 && count > lengest)lengest = count;}}return lengest;}};



0 0
原创粉丝点击