三刀流.索隆 发表于 2021-12-2 13:07:28

无重复字符的最长子串

本帖最后由 三刀流.索隆 于 2021-12-2 13:11 编辑

leetcode的题目,不知道哪里有问题,抱错也看不懂



int lengthOfLongestSubstring(char * s){
        int i = 0,j = 0,count = 0,temp = 0;
       
        for (i = 0;s != '\0';++i)
        {
                for (j = i + 1;s != s;++j)
                        temp++;
               
                count = temp > count ? temp:count;
        }
       
        return count;
}

链接放不起,就直接打出来了:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

jackz007 发表于 2021-12-2 15:32:54

本帖最后由 jackz007 于 2021-12-5 14:44 编辑

#include <stdio.h>

int foo(char s[])
{
      int a , b , c , d , i , j , k                                          ;
      for(i = k = 0 ; i < 50 ; i ++) for(j = 0 ; j < 2 ; j ++) d = 0            ;
      if(s) {
                for(c = 0 , i = 0 ; s ; i ++) {
                        for(j = i + 1 ; s && s != s ; j ++)                  ;
                        for(a = i ; a < j - 1 ;) {
                              for(b = a + 1 ; b < j && s != s ; b ++)         ;
                              if(b < j) break                                       ;
                              else a ++                                             ;
                        }
                        if(a == j - 1) {
                              d = i                                             ;
                              d = j - i                                       ;
                              c ++                                                    ;
                              i = j - 1                                             ;
                        }
                }
                for(k = 0 , i = 1 ; i < c ; i ++) if(d > d) k = i         ;
/*
                for(i = 0 ; i < c ; i ++) {
                        if(d == d) {
                              for(j = 0 ; j < d ; j ++) putchar(s + j]) ;
                              putchar('\n')                                           ;
                        }
                }
*/
      }
      return d                                                                  ;
}

int main(void)
{
      char s                                                                     ;
      gets(s)                                                                         ;
      printf("%d\n" , foo(s))                                                         ;
}
      编译、运行实况
D:\0002.Exercise\C>g++ -o x x.c

D:\0002.Exercise\C>x
bbbbbb
1

D:\0002.Exercise\C>x
pwwkew
3

D:\0002.Exercise\C>x
abcabcbb
3

D:\0002.Exercise\C>

三刀流.索隆 发表于 2021-12-5 13:16:32

bool ifOk(int i,int j,char *s);

int lengthOfLongestSubstring(char * s){
    if (NULL == s)
      return 0;

      int i = 0,j = 0,count = 0,temp = 1;
      
      for (i = 0;s != '\0';++i)
      {
            for (j = i+1;s != '\0' && s != s;++j)
            {
                if (ifOk(i,j,s) == 1)
                  temp++;
            }
            count = temp > count ? temp:count;
            temp = 1;
      }
      //for ()
      
      return count;
}

bool ifOk(int i,int j,char *s)
{
    int a = i,b = i+1;
    if (j-i < 2)
      return 1;
    else
    {
      for (;b <= j;++b)
      {
            for (a = i;a != b;++a)
            {
                if (s != s)
                  continue;
                else
                {
                  return 0;
                  break;
                }
            }
      }
    }

    return 1;
}

三刀流.索隆 发表于 2021-12-5 13:21:59

jackz007 发表于 2021-12-2 15:32
编译、运行实况

如果输入s为""的话,你的代码有bug{:10_257:}

jackz007 发表于 2021-12-5 14:45:53

三刀流.索隆 发表于 2021-12-5 13:21
如果输入s为""的话,你的代码有bug

         你提醒的很对,我的代码确实忽略了空串,2 楼的代码已经修正,欢迎再测。
页: [1]
查看完整版本: 无重复字符的最长子串