003

来源:互联网 发布:安装人工智能计算器 编辑:程序博客网 时间:2024/05/18 03:44

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.

Subscribe to see which companies asked this question.


        //思路:a:第一个指针位置,目前开始判断,b:第二个位置,探测最长的长度
        //i:指针到b的时候,从a到b开始检测,是否有i=(a,b)之间相同,
        //如果出现不同或者i到了l-1的时候,开始进行判断:是否这个长度>max
   

实际问题:

对于菜鸡来说

要写一个逻辑

中间会有很多问题

然后再不断修bug

怎么样做快一点呢

第一个 背一种思路

缺点:太迂腐

第二种 有ide来编译

节约时间效率高

第三种 高手写的时候便能大致模拟,考虑各种bug的问题

最不推荐,瞎jb想,瞎调bug,并且走神,ruin 掉你的耐心

总而言之,

水平不够的情况下

已经没有精力去看其他的算法和语言了

-

这次运气还不错

错误一大堆


瞎搞得时候很快


准备下个xcode来调试

-----------------------------------------------------------------------------------------------------------------------


class Solution {

public:
    int lengthOfLongestSubstring(string s) {
        int l=s.length();
        int a,b,max=0,count=1;
        int c,d;//to mark
        if(l==1){
        return 1;


        }
        int be,ed;
        int i;//for move 
        a=0;
        while(a<l){
        for(b=a+1;b<l;b++){
       
        bool flag=true;
        for(i=a;i<b;i++){
        //cout<<"i:"<<i<<endl; 
        if(s[i]==s[b]){
        flag=false;
                        break;
        }
        }
        if(!flag||i==l-1){
       
        if(i==l-1&&flag)//last


        count++;
        if(count>max){
                        be=a;
                        if(flag)
                            ed=b;
                        else 
                            ed=b-1;
        max=count;
        c=a;
        d=b-1;
        }
        break;


        }
        count++;
        if(!flag)
        break;




        }
        
        //count end
        if(b==l-1||b==l)
        break;
        if(a==l-1)
            break;
        //cout<<i<<endl;
        a=i+1;//move to i+1
        count=1;
    }
return max;

    }




};
0 0