写正确函数需要注意的地方:最长递减子序列

int longest=0;
void LongestSubSeq(int* arr, int count, bool* assis, int curindx, int lastnum, int length)
{
	if(curindx==count)
	{
		if(longest<length)
		{
			longest=length;
			for(int i=0;i<count;++i)
				if(true==assis[i])
					cout<<arr[i]<<" ";
			cout<<endl;
		}
	}
	else if(curindx<count)
	{
		if(arr[curindx]<lastnum)
		{
			assis[curindx]=true;
			LongestSubSeq(arr, count, assis, curindx+1, arr[curindx], length+1);
		}
		assis[curindx]=false;
		LongestSubSeq(arr, count, assis, curindx+1, lastnum,length);
	}
}

int LongestSubSeq(int* arr, int count)
{
	if(NULL==arr || count==0)
		return 0;
	if(count<0)
		return -1;

	bool* assis=new bool[count];
	memset(assis,0, sizeof(bool)*count);

	LongestSubSeq(arr, count, assis, 0, 0x7fffffff, 0);

	delete []assis;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中求最长递增子序列的代码可以有多种实现方式。引用\[1\]中给出了一种实现方式,使用了动态规划的思想。该代码定义了一个函数`lis`,接受一个整数数组作为参数。它首先初始化一个长度为数组长度的列表`m`,用于记录以每个元素结尾的最长递增子序列的长度。然后通过两层循环遍历数组,如果当前元素比后面的元素小且以当前元素结尾的子序列长度小于等于以后面元素结尾的子序列长度,则更新以当前元素结尾的子序列长度。最后找到`m`列表中的最大值,然后遍历数组,将与最大值相等的元素添加到结果列表中,并递减最大值,直到最大值为0。最后返回结果列表。给定的示例数组为`\[10, 22, 9, 33, 21, 50, 41, 60, 80\]`,调用`lis`函数后输出结果为`\[10, 22, 33, 50, 60, 80\]`。 另外,引用\[2\]中给出了另一种实现方式,使用了动态规划和二分查找的思想。该代码定义了一个类`Solution`,其中包含一个方法`lengthOfLIS`,接受一个整数数组作为参数。它首先判断数组是否为空,如果为空则返回0。然后初始化一个长度为数组长度的列表`dp`,用于记录以每个元素结尾的最长递增子序列的长度。通过两层循环遍历数组,如果当前元素比前面的元素大,则更新以当前元素结尾的子序列长度为前面元素结尾的子序列长度加1。最后返回`dp`列表中的最大值。这种实现方式的时间复杂度为O(n)。 #### 引用[.reference_title] - *1* [Python 最长递增子序列代码](https://blog.csdn.net/deanhj/article/details/101634446)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [最长连续序列 python](https://blog.csdn.net/dearzhuiyi/article/details/126930325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值