zoukankan      html  css  js  c++  java
  • LeetCode 3

    Longest Substring Without Repeating Characters

    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.

     1 /*************************************************************************
     2     > File Name: LeetCode003.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Tue 17 May 2016 17:10:02 PM CST
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9     
    10     Longest Substring Without Repeating Characters
    11     
    12     Given a string, find the length of the longest substring 
    13     without repeating characters.
    14 
    15     Examples:
    16 
    17     Given "abcabcbb", the answer is "abc", which the length is 3.
    18 
    19     Given "bbbbb", the answer is "b", with the length of 1.
    20 
    21     Given "pwwkew", the answer is "wke", with the length of 3. 
    22     Note that the answer must be a substring, 
    23     "pwke" is a subsequence and not a substring.
    24 
    25  ************************************************************************/
    26 
    27 #include "stdio.h"
    28 
    29 /* 8ms */
    30 int lengthOfLongestSubstring(char* s)
    31 {
    32     int n = strlen(s);
    33     
    34     if( n <= 1 )
    35     {
    36         return n;
    37     }
    38     
    39     int i = 0, j = 0;
    40     int maxLen = 0;
    41     int exist[256] = { 0 };
    42     while( j < n )
    43     {
    44         if( exist[s[j]] )
    45         {
    46             maxLen = maxLen>(j-i) ? maxLen : (j-i);
    47             while( s[i] != s[j] )
    48             {
    49                 exist[s[i]] = 0;
    50                 i++;
    51             }
    52             i++;
    53             j++;
    54         }
    55         else
    56         {
    57             exist[s[j]] = 1;
    58             j++;
    59         }
    60     }
    61     maxLen = maxLen>(n-i) ? maxLen : (n-i);
    62     return maxLen;
    63 }
    64 
    65 /* 36ms */
    66 int lengthOfLongestSubstring2(char* s)
    67 {
    68     int len = 0;
    69     int p=0 , q=0,  i; 
    70 
    71     for( q=0; q<strlen(s); q++ )
    72     {
    73         for( i=p; i<q; i++ )
    74         {
    75             if( s[q] == s[i] )
    76             {
    77                 if( q-p > len ) len = q-p;
    78                 p = i+1;
    79             }
    80         }
    81     }
    82     len = len>q-p?len:q-p;
    83     return len;
    84 }
    85 
    86 int main()
    87 {
    88     char* s = "abccab";
    89     int ret = lengthOfLongestSubstring(s);
    90     printf("%d
    ", ret);
    91     
    92     return 0;
    93 }
  • 相关阅读:
    c语言小项目---通讯录2.0
    小练习 打地鼠游戏
    4-20 这是一次失败的小项目 (单链表应用--通讯录)
    4-19 易错点 链表的插入
    小练习 简易双色球模拟器
    【2019.8.22】测试题目订正
    【模板】【P1182】数列分段II——二分答案
    【P2634】聪聪可可——点分治
    【模板】【P3605】【USACO17JAN】Promotion Counting 晋升者计数——动态开点和线段树合并(树状数组/主席树)
    【AcWing 113】【交互】特殊排序——二分
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5511662.html
Copyright © 2011-2022 走看看