现在的位置: 首页 > 综合 > 正文

Longest Substring Without Repeating Characters — leetcode

2018年10月19日 ⁄ 综合 ⁄ 共 1958字 ⁄ 字号 评论关闭

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) {
        const char *hash[256] = {};
        const char *str = s.c_str();
        const char *head = str;
        int max_len = 0;
        while (*str) {
                if (hash[*str] >= head) {
                        if (str - head > max_len)
                                max_len = str - head;

                        head = hash[*str] + 1;
                }
                hash[*str] = str;
                ++str;
        }

        if (str - head > max_len)
                max_len = str - head;

        return max_len;
    }
};

在leetcode上运行用时64ms。

算法参考了博客

http://www.geeksforgeeks.org/length-of-the-longest-substring-without-repeating-characters/

附上该博客的代码,供比较。是不是我的代码要短小的多。

int longestUniqueSubsttr(char *str)
{
    int n = strlen(str);
    int cur_len = 1;  // To store the lenght of current substring
    int max_len = 1;  // To store the result
    int prev_index;  // To store the previous index
    int i;
    int *visited = (int *)malloc(sizeof(int)*NO_OF_CHARS);
 
    /* Initialize the visited array as -1, -1 is used to indicate that
       character has not been visited yet. */
    for (i = 0; i < NO_OF_CHARS;  i++)
        visited[i] = -1;
 
    /* Mark first character as visited by storing the index of first
       character in visited array. */
    visited[str[0]] = 0;
 
    /* Start from the second character. First character is already processed
       (cur_len and max_len are initialized as 1, and visited[str[0]] is set */
    for (i = 1; i < n; i++)
    {
        prev_index =  visited[str[i]];
 
        /* If the currentt character is not present in the already processed
           substring or it is not part of the current NRCS, then do cur_len++ */
        if (prev_index == -1 || i - cur_len > prev_index)
            cur_len++;
 
        /* If the current character is present in currently considered NRCS,
           then update NRCS to start from the next character of previous instance. */
        else
        {
            /* Also, when we are changing the NRCS, we should also check whether
              length of the previous NRCS was greater than max_len or not.*/
            if (cur_len > max_len)
                max_len = cur_len;
 
            cur_len = i - prev_index;
        }
 
        visited[str[i]] = i; // update the index of current character
    }
 
    // Compare the length of last NRCS with max_len and update max_len if needed
    if (cur_len > max_len)
        max_len = cur_len;
 
 
    free(visited); // free memory allocated for visited
 
    return max_len;
}

抱歉!评论已关闭.