【LeetCode】 3. Longest Substring Without Repeating Characters

来源:互联网 发布:使命召唤4mac迅雷下载 编辑:程序博客网 时间:2024/06/10 05:57

Problem:

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.

==============================================================================================================

 Thinking:

1.用了一个vector容器。

2.只要和容器内没有重复的就把字符串挨个扔进去,同时记录大小。

3.遇到重复了,丢掉容器里重复的那个字母及其前面的字母,此时最大子串长度记下。

.4继续挨个扔进去,重复3步骤。

5.输出最大子串长度。

经过上述过程,字符串pwwkew被分成过:pw、wke、kew,最长为3

==============================================================================================================

Solution:

class Solution {public:    int lengthOfLongestSubstring(string s) {        vector<char> s1;        int mnum=0;        if(s.empty()!=true){        s1.push_back(s[0]);        mnum=1;        for(int i=1;i<s.size();i++){            if(find(s1.begin(),s1.end(),s[i])==s1.end()){                s1.push_back(s[i]);                if(mnum<s1.size())                mnum=s1.size();            }            else {                if(mnum<s1.size())                mnum=s1.size();                s1.erase(s1.begin(),find(s1.begin(),s1.end(),s[i])+1);                s1.push_back(s[i]);            }                           }        }    return mnum;   }};

=============================================================================================================
Knowledge:

1.vector的声明

 

   vector<ElemType> c;   创建一个空的vector

 

   vector<ElemType> c1(c2); 创建一个vector c1,并用c2去初始化c1

 

   vector<ElemType> c(n) ; 创建一个含有n个ElemType类型数据的vector;

 

   vector<ElemType> c(n,elem); 创建一个含有n个ElemType类型数据的vector,并全部初始化为elem;

 

   c.~vector<ElemType>(); 销毁所有数据,释放资源;

 

2.vector容器中常用的函数。(c为一个容器对象)

 

    c.push_back(elem);   在容器最后位置添加一个元素elem

 

    c.pop_back();            删除容器最后位置处的元素

 

    c.at(index);                返回指定index位置处的元素

 

    c.begin();                   返回指向容器最开始位置数据的指针

 

    c.end();                      返回指向容器最后一个数据单元的指针+1

 

    c.front();                     返回容器最开始单元数据的引用

 

    c.back();                     返回容器最后一个数据的引用

 

    c.max_size();              返回容器的最大容量

 

    c.size();                      返回当前容器中实际存放元素的个数

 

    c.capacity();               同c.size()

 

     c.resize();                   重新设置vector的容量

 

    c.reserve();                同c.resize()

 

    c.erase(p);               删除指针p指向位置的数据,返回下指向下一个数据位置的指针(迭代器)

 

    c.erase(begin,end)     删除begin,end区间的数据,返回指向下一个数据位置的指针(迭代器)

 

    c.clear();                    清除所有数据

 

    c.rbegin();                  将vector反转后的开始指针返回(其实就是原来的end-1)

 

    c.rend();                     将vector反转后的结束指针返回(其实就是原来的begin-1)

 

    c.empty();                   判断容器是否为空,若为空返回true,否则返回false

 

    c1.swap(c2);               交换两个容器中的数据

 

    c.insert(p,elem);          在指针p指向的位置插入数据elem,返回指向elem位置的指针      

 

    c.insert(p,n,elem);      在位置p插入n个elem数据,无返回值

 

    c.insert(p,begin,end) 在位置p插入在区间[begin,end)的数据,无返回值

 

3.vector中的操作

 

    operator[] 如: c.[i];

 

    同at()函数的作用相同,即取容器中的数据。

   Vector容器的清空

在使用容器时,有时候我们还需要清空容器,这样清空既可以清空容器内的变量,也可以清空容器占用的空间,此时的代码为:

vector<RadarOutParaCache>().swap(RedRadarParaOutVector);

<>内为容器的类型,swap()内为容器的名称。




阅读全文
1 0
原创粉丝点击