LeetCode题目---找最长子串 的长度

博客围绕找出给定字符串中不含有重复字符的最长子串的长度展开,介绍了思路一的C++代码实现,且该方法存在字符数限制,还提及了思路二。

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

思路一:字符串为s,vector<char>容器

从第一个字符开始放入vector容器里边,看第二个字符是否在容器里边,没有的话就放入容器继续往下查,有的话就停止此次查找,返回容器中字符数量。

函数:bool find(string s,char a)找到字符串s中是否有字符a,有返回false,没有返回true。

bool find(vector<char> s, char a)
{
	for (auto it = s.begin(); it != s.end(); it++)
	{
		if ((*it) == a)
			return true;
		else
			continue;
	}
	return false;
}

字符串查找一次就删除首元素一次
函数:string deletestr(string s)删除字符串首元素,

查找完整个字符串即应该有一个循环或者递归函数。


bool find(vector<char> s,char a)
{
    for(auto it = s.begin();it!=s.end();it++)
        
}

思路一C++代码实现:(此方法有字符数限制)

class Solution {
public:
int lengthOfLongestSubstring(string s)
{
	if (s == "")
		return 0;
	vector<char> ch1;
	vector<int> length;//每个子串的长度
	
	while (!s.empty())
	{
		bool bf = false;
		for (auto it = s.begin(); it != s.end(); ++it)
		{//此循环为找到每一个子串的长度

			for (auto itt = ch1.begin(); itt != ch1.end(); itt++)
			{//此循环查找ch1中是否有元素*it;
				//cout << *itt << endl;
				if ((*itt) == *it)
				{
					bf = true;
					break;
				}								
			}
			if (!bf)//查找字符是否在ch1里边
				ch1.push_back(*it);
			//cout << " "<<ch1.size() <<endl;
			
			
		}
		length.push_back(ch1.size());
		ch1.clear();
		s.erase(s.begin());			
	}


	int changdu = -100;
	vector<int>::iterator it = length.begin();

	while (it != length.end())
	{
		if ((*it) > changdu)
			changdu = (*it);
		it++;
	}
	return changdu;//return max(length);
	
	
}
};

 

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

class fun
{
public:
bool find(vector<char> s, char a)
{
	for (auto it = s.begin(); it != s.end(); it++)
		if ((*it) == a)
			return true;
	return false;
}

int max(vector<int> length)
{
	int changdu = -100;
	vector<int>::iterator it = length.begin();

	while (it != length.end())
	{
		if ((*it) > changdu)
			changdu = (*it);
		it++;
	}
	return changdu;
}
int lengthOfLongestSubstring(string s)
{
	if (s == "")
		return 0;
	vector<char> ch1;
	vector<int> length;//每个子串的长度
	
	while (!s.empty())
	{
		bool bf = false;
		for (auto it = s.begin(); it != s.end(); ++it)
		{//此循环为找到每一个子串的长度

			for (auto itt = ch1.begin(); itt != ch1.end(); itt++)
			{//此循环查找ch1中是否有元素*it;
				cout << *itt << endl;
				if ((*itt) == *it)
				{
					bf = true;
					break;
				}								
			}
			if (!bf)//查找字符是否在ch1里边
				ch1.push_back(*it);
			//cout << " "<<ch1.size() <<endl;
			
			
		}
		length.push_back(ch1.size());
		ch1.clear();
		s.erase(s.begin());			
	}


	int changdu = -100;
	vector<int>::iterator it = length.begin();

	while (it != length.end())
	{
		if ((*it) > changdu)
			changdu = (*it);
		it++;
	}
	return changdu;//return max(length);
	
	
}

};

int main()
{
	//vector<char> s = { 'a', 'b', 'c', 'd', 'a', 'b' };
	string a("aa");
	fun f;
	//f.lengthOfLongestSubstring(a);
	cout << f.lengthOfLongestSubstring(a) << endl;
	
	return 0;
	
}

 思路二:

 

LeetCode 题目 491 - 递增子序列 (Incremental Subsequence) 是一道关于算法设计的中等难度题目。这道题要你在给定整数数组 nums 中出所有长度大于等于 1 的递增子序列。递增子序列是指数组中的一连续元素,它们按照顺序严格增大。 解决这个问题的一个常见策略是使用动态规划(Dynamic Programming),特别是哈希表或者单调栈(Monotonic Stack)。你可以维护一个栈,每当遍历到一个比栈顶元素大的数字时,就将它推入栈,并更新当前长递增子序列的长度。同时,如果遇到一个不大于栈顶元素的数字,就从栈顶开始检查是否存在更长的递增子序列。 以下是 C++ 解决此问题的一种简单实现: ```cpp class Solution { public: vector<int> lengthOfLIS(vector<int>& nums) { int n = nums.size(); if (n == 0) return {}; // 使用单调栈存储当前已知的大子序列 stack<pair<int, int>> stk; stk.push({nums[0], 1}); for (int i = 1; i < n; ++i) { while (!stk.empty() && nums[i] > stk.top().first) { // 如果新数大于栈顶元素,到一个更长的递增子序列 int len = stk.top().second + 1; ans.push_back(len); stk.pop(); } // 如果新数不大于栈顶元素,尝试从当前位置开始寻长子序列 if (!stk.empty()) { stk.top().second = max(stk.top().second, 1); } else { stk.push({nums[i], 1}); } } return ans; } private: vector<int> ans; }; ``` 在这个解决方案中,`ans` 存储所有的递增子序列长度后返回这个结果向量即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值