力扣刷题笔记20——最长不含重复字符的子字符串/ascii码表取值256

最长不含重复字符的子字符串/ascii码表取值256

问题

来自力扣

请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

我的代码


#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include<queue>
#include <typeinfo>
#include <numeric>
#include<cmath>
#include<map>
#include<string>
class Solution {
public:
	int lengthOfLongestSubstring(string s) {
		int maxlen = 0, clen = 0;
		char tmp;
		map<char, bool> charm;
		queue<char> charq;
		//vector<char> charv;
		for (int i = 0; i < s.size(); ++i) {
			charq.push(s[i]);
			if (charm.find(s[i])==charm.end()|| charm.find(s[i])->second==false) {
				charm[s[i]] = true;
			}
			else {
				while (!charq.empty()) {
					tmp = charq.front();
					if (s[i] == tmp) {
						charq.pop();
						cout << "find"<<charq.size();
						break;
					}
					charq.pop();
					cout << s[i] << " be set false"<<tmp;
					charm[tmp] = false;
				}

			}
			
			for (auto a : charm) {
				cout << a.first << a.second << "  ";
			}
			cout << charq.size() << endl;
			if (maxlen < charq.size())
				maxlen = charq.size();
		}
		return maxlen;
	}
};
int main() {
	string s = "pwwkew";
	Solution mysolution;
	int a = mysolution.lengthOfLongestSubstring(s);
	cout << a;
	return 0;
}

这道题的解题思路和笔记18很像。
我用一个队列q和一个map。
q用来将每个字符压入,如果发现字符重复了,就pop直到不重复。
map是记录目前q中还有哪些字符的(我感觉用map怪怪的,但是没想到好的方法。)
maxlen则是一直记录q的最大长度。

示例代码


class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ans = 0; 
        int a[256] = {0};  //用来保存每个字符出现的次数,初始化为0
        for(int i = 0, j = 0; i < s.size(); i ++){
            a[s[i]]++;
            while(j < i && a[s[i]] > 1){//若右指针i的字符si出现>1次
                a[s[j]]--;//右移左指针即删除最左字符,直到si仅出现一次
                j ++;//例如abcdd-bcdd-cdd-dd-d.
            }//每次跳出循环即得到一个无重复字符的子串
            ans = max(ans, i - j + 1);//比较子串长度得到最大值即可
        }
        return ans;
    }
};

我本来也想用一个数组来记录字符是否出现的,因为很方便,每个字符都对应1个ascii码。但是我以为只有26个小写字母,就创建了1个长度26的数组,测试代码的时候发现有其他的字符,然后我的代码报错了。所以才改得那么复杂。

看了示例代码后又去查了下ascii码表,好像它是固定的,就是0~255。又学到了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小欣CZX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值