zoukankan      html  css  js  c++  java
  • 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(''); break;
    			case 'f': result.push_back('f'); break;
    			case 'r': result.push_back('
    '); break;
    			case 'n': result.push_back('
    '); break;
    			case 't': result.push_back('	'); 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;
    }
    

      

  • 相关阅读:
    mysql 官网下载,以及安装配置
    SQL 完美解决用逗号分隔存放在一个字段数据
    centos7 开机自动执行shell脚本
    ansible 通过shell脚本执行MySQL语句
    查找让mysql cpu达到100%的罪魁祸首
    查看mysql数据库容量大小
    docker安装zabbix-proxy
    docker zabbix 环境变量
    zabbix 时区不对-误差5个小时--持续更新
    zabbix钉钉告警
  • 原文地址:https://www.cnblogs.com/277223178dudu/p/10761745.html
Copyright © 2011-2022 走看看