c++ 11 vs 98

在求最长子字符串中题中要遍历个上万字符数据

1.使用c++11代码

for (auto ch : s) {

		auto ss = vsi[ch];

		vsi[ch].insert(i);

		i++;

	}

2.使用c++98代码

	for (int i = 0; i < s.length(); i++) {

		ch = s[i];

		vsi[ch].insert(i);

	}

 这两段代码时间比较

1.c++ 11

c++ 11 vs 98

2.c++ 98

c++ 11 vs 98

下面是longest-substring-without-repeating-characters AC代码

int lengthOfLongestSubstring(string s) 

{

    if (s.length() <= 1) return s.length();

    vector<set<int> > vsi(256);

    char ch;

    for (int i = 0; i < s.length(); i++) {

        ch = s[i];

        vsi[ch].insert(i);

    }

    int count = 0;

    int max = 0;

    for (int i = 0; i < s.size() - 1; i++) {

        count = 0;

        int endPos = -1;

        for (int j = i; j < s.size(); j++) {

            if ((endPos != -1 && j >= endPos)) {

                break;

            }

            auto pos1 = vsi[s[j]].find(j);

            pos1++;

            count++;

            if (pos1 != vsi[s[j]].end()) {

                int tmp = (*pos1) - i;

                if (tmp <= max) break;

                if (endPos > (*pos1) || endPos == -1) {

                    endPos = (*pos1);

                }

            }

        }

        if (max < count) {

            max = count;

        }

    }

    return max;

}

 

你可能感兴趣的:(C++)