欢迎来到IT嘟嘟的博客

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。
扩大
缩小

3. 无重复字符的最长子串

#include<iostream>
#include<stdio.h>
#include<vector>
#include <algorithm>
#include <functional>
#include<string>
#include<fstream>
#include <sstream>
#include <assert.h>
using namespace std;
class Solution {
public:
	int lengthOfLongestSubstring(string s)
	{
		if (s.length() == 0)//如果字符串的长度为0,则返回空即为0
			return 0;
		int size, i = 0, j, k, max = 0;//初始化一些变量
		size = s.length();//获得字符串的长度
		for (j = 0; j < size; j++)//遍历字符串
		{
			for (k = i; k < j;k++)//下个字符串是否和当前字符相同,若相同跳出当前遍历
			if (s[k] == s[j]){
				i = k + 1;
				break;
			}
			if (j - i + 1>max)//求取不相同的字符串的大小
				max = j - i + 1;
		}
		return max;
	}
};

string stringToString(string input) {
	assert(input.length() >= 2);
	string result;
	for (int i = 1; i < input.length() - 1; i++)
	{
		char currentChar = input[i];
		if (input[i] == '\\') {
			char nextChar = input[i + 1];
			switch (nextChar) {
			case '\"': result.push_back('\"'); break;
			case '/': result.push_back('/'); break;
			case '\\': result.push_back('\\'); break;
			case 'b': result.push_back('\b'); break;
			case 'f': result.push_back('\f'); break;
			case 'r': result.push_back('\r'); break;
			case 'n': result.push_back('\n'); break;
			case 't': result.push_back('\t'); break;
			default: break;
			}
			i++;
		}
		else {
			result.push_back(currentChar);
		}
	}
	return result;
}

int main() {
	string line;
	while (getline(cin, line)) {
		string s = stringToString(line);

		int ret = Solution().lengthOfLongestSubstring(s);

		string out = to_string(ret);
		cout << out << endl;
	}
	return 0;
}

  

 

posted on 2019-04-24 12:55  IT嘟嘟  阅读(112)  评论(0编辑  收藏  举报

导航