数据结构易错题

PAT刷题:

注意事项:
1,容易超时,用快输,快出
2,数组越界,提前设定范围
3,设置时间,一题不超过1小时

L2-004 这是二叉搜索树吗

前序转后序。
在这里插入图片描述
solution:
1:双指针判断 左,有
2:递归,存后序遍历
3.判断镜像

package 天梯赛l2;
//2021年4月10日下午9:49:31
//writer:apple
import java.util.*;
import java.io.*;
import java.math.*;
public class l2004_2 {

	static int n;
	static LinkedList<Integer> fron=new LinkedList<>();
	static LinkedList<Integer> rear=new LinkedList<>();
	static boolean mi=false;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();
		for(int i=0;i<n;i++)
		{
			fron.add(s.nextInt());
		}
//		if(fron.get(1)>=fron.get(0)) mi=true;
		check(0,fron.size()-1);
		if(rear.size()!=fron.size())
		{
			mi=true;rear.clear();check(0,fron.size()-1);
		}
		if(fron.size()!=rear.size())
		{
			System.out.println("NO");
		}
		else {
			System.out.println("YES");
			for(int i=0;i<rear.size();i++) {
				if(i!=rear.size()-1)
				{
					System.out.print(rear.get(i)+" ");
				}
				else {
					System.out.println(rear.get(i));
				}
			}
		}
	}
	private static void check(int i, int size) {
		// TODO Auto-generated method stub
		if(i>size) return;
		int sta=i+1;
		int end=size;
		if(mi)
		{
			while(sta<=size&&fron.get(sta)>=fron.get(i))
				sta++;
			while(end>i&&fron.get(end)<fron.get(i))
				end--;
		}
		else {
			while(sta<=size&&fron.get(sta)<fron.get(i))
			sta++;
			while(end>i&&fron.get(end)>=fron.get(i))
			end--;
		}
		
		if(sta!=end+1) return;
		check(i+1,sta-1);//左
		check(sta,size);//右
		rear.add(fron.get(i));
	}
}

最长对称子串

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const double eps = 1e-8; //const只是用来定义常量,常量在代码中一直存在,但define是预处理功能,有本质区别
const int maxx = 0x7f7f7f7f;//0x7f7f7f7f表示数据的无穷大
//常用的浮点数比较宏定义:
#define Equ(a,b) ((fabs((a)-(b)))<(eps)) //等于
#define More(a,b) (((a)-(b))>(esp)) //大于
#define Less(a,b) (((a)-(b))<(-esp))//小于
#define MoreEqu(a,b) (((a)-(b))>(-esp))//大于等于
#define LessEqu(a,b) (((a)-(b))<(esp))//小于等于
#define  MAX( x, y )  ( ((x) > (y)) ? (x) : (y) )//
//使用了algorithm头文件就可以直接使用max函数;
#define  MIN( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define ll long long
#define PI 3.1415926
#define eps 1e-8
#define Conn(x,y) x##y;

int ans=1;
int main()
{
	string str="";
	getline(cin,str);
	int len=str.size();
	int a[len][len];
	
	memset(a,0,sizeof(a));
	for(int i=0;i<len;i++)
		a[i][i]=1; 
	for(int length=1;length<len;length++)
	{
		for(int l=0;l<len-length;l++)
		{
			int r=l+length;
			if(str[l]==str[r])
			{
				if(a[l+1][r-1] == r - l - 1)
                {
				a[l][r]=a[l+1][r-1]+2;
				ans=max(ans,a[l][r]);
				}
			}
		}
	}
	cout<<ans;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值