Leetcode 3 Longest Substring Without Repeating Characters

xiaoxiao2021-02-28  33

LeetCode 3

Longest Substring Without Repeating Characters

Problem Description: Given a string, find the length of the longest substring without repeating characters.

Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

Solution:

class Solution { public: int lengthOfLongestSubstring(string s) { bool temp[256]; int begin = 0; int maxlength = 0; int i, j; for (i = 0; i < s.length(); i++) { memset(temp, false, sizeof(hash)); temp[s[i]] = true; for (j = i+1; j < s.length(); j++) { if (!temp[s[j]]) { temp[s[j]] = true; } else { break; } } if (j-i>maxlength) { maxlength = j-i; begin = i; } } return maxlength; } };
转载请注明原文地址: https://www.6miu.com/read-2623730.html

最新回复(0)