剑指offer,靠谱分析及牛客网可AC代码,c++(61~end)

本文深入解析了包括序列化二叉树、二叉搜索树的第k个结点、数据流中的中位数、滑动窗口的最大值、矩阵中的路径及机器人运动范围等经典数据结构与算法题目,提供了详细的C++实现代码与思路分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

61、序列化二叉树
https://blog.youkuaiyun.com/malele4th/article/details/79335409
https://blog.youkuaiyun.com/u013575812/article/details/50185719

class Solution {
	vector<int> vec;
public:
	void dfs1(TreeNode *p)
	{
		if (!p) vec.push_back(0x23333);
		else
		{
			vec.push_back(p->val);
			dfs1(p->left);
			dfs1(p->right);
		}
	}

	TreeNode* dfs2(int* &p)
	{
		if (*p == 0x23333)
		{
			++p;
			return nullptr;
		}
		TreeNode* res = new TreeNode(*p);
		++p;
		res->left = dfs2(p);
		res->right = dfs2(p);
		return res;
	}
	char* Serialize(TreeNode *root) {
		vec.clear();
		dfs1(root);
		int *res = new int[vec.size()];
		for (int i = 0; i < vec.size(); i++) res[i] = vec[i];
		return (char*)res;
	}
	TreeNode* Deserialize(char *str) {
		int *p = (int*)str;
		return dfs2(p);
	}
};

62、二叉搜索树的第k个结点
https://blog.youkuaiyun.com/sun10081/article/details/82805991
https://blog.youkuaiyun.com/lzuacm/article/details/51328529

class Solution {
public:
    TreeNode* KthNode(TreeNode* pRoot, unsigned int k)
    {
        TreeNode *p=pRoot;
        TreeNode *resNode;
		if(p==NULL || k==0)  return NULL;
		stack<TreeNode *> st;
		unsigned int count=0;
		
	while(!st.empty() || p != NULL)
	{			
		if(p != NULL)
		{
			st.push(p);
			p=p->left;
		}			
		if(p == NULL)
		{
			p=st.top();  // 取当前节点
			count++;
			if(count==k)  resNode=p;  
			st.pop();
			p=p->right;
		} 
	}
        return resNode;
    }
};

63、数据流中的中位数
https://blog.youkuaiyun.com/derrantcm/article/details/46872339
https://blog.youkuaiyun.com/malele4th/article/details/79335602

class Solution {
public:
	priority_queue<int, vector<int>, less<int>>p;
	priority_queue < int, vector<int>, greater<int>>q;
	void Insert(int num)
	{
		if (p.empty() || num < p.top())
			p.push(num);
		else
			q.push(num);
		if (p.size() == q.size() + 2)
		{
			q.push(p.top());
			p.pop();
		}
		if (p.size() + 1 == q.size())
		{
			p.push(q.top());
			q.pop();
		}
	}

	double GetMedian()
	{
		return p.size() > q.size() ? p.top() : (q.top()+p.top())/2.0;
	}

};

63、滑动窗口的最大值:
https://blog.youkuaiyun.com/malele4th/article/details/79336991
https://blog.youkuaiyun.com/derrantcm/article/details/46872411

class Solution {
public:
	vector<int> maxInWindows(const vector<int>& num, unsigned int size)
	{
		vector<int>res;
		deque<int>q;
		for (int i = 0; i < num.size(); i++)
		{
			while (q.size() && num[q.back()] <= num[i]) q.pop_back();
			while (q.size() && i - q.front() + 1 > size)q.pop_front();
			q.push_back(i);
			if (size&&i + 1 >= size)
				res.push_back(num[q.front()]);
		}
		return res;
	}
};

64、矩阵中的路径
https://blog.youkuaiyun.com/derrantcm/article/details/46887767
https://blog.youkuaiyun.com/malele4th/article/details/79337003

class Solution {
public:
	bool hasPath(char* matrix, int rows, int cols, char* str)
	{
		vector<bool> used(strlen(matrix), false);
		if (str == nullptr)return true;
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				if (has(matrix, rows, cols,i,j,str,0, used))
					return true;
			}
		}
		return false;
	}

	bool has(char*matrix, int rows, int cols,int row,int col, char* str, int length, vector<bool>used)
	{
		if (strlen(str) == length) return true;
		if (row > rows || row<0 || col>cols || col < 0) return false;
		int index = row * cols + col;
		bool result = false;
		if (!used[index] && matrix[index] == str[length])
		{
			used[index] = true;
			result = has(matrix, rows, cols, row+1,col, str, length + 1, used) ||
					has(matrix, rows, cols, row-1,col, str, length + 1, used) ||
					has(matrix, rows, cols, row, col+1, str, length + 1, used) ||
					has(matrix, rows, cols, row, col-1, str, length + 1, used);
		}
		if (result) return true;
		return false;
	}

};

65、机器人的运动范围
https://blog.youkuaiyun.com/malele4th/article/details/79337046
https://blog.youkuaiyun.com/derrantcm/article/details/46887811

class Solution {
public:
	int movingCount(int threshold, int rows, int cols)
	{
		int **flag = new int*[rows];
		for (int i = 0; i < rows; i++)
		{
			flag[i] = new int[cols];
		}
		return count(0,0,rows,cols,flag,threshold);
	}

	int count(int i, int j, int rows, int cols, int **flag,int threshold)
	{
		if (i < 0 || j<0 || j >= cols || i >= rows || sum(i) + sum(j)>threshold || flag[i][j] == 1) return 0;
		flag[i][j] = 1;
		return count(i + 1, j, rows, cols, flag, threshold)
			+ count(i - 1, j, rows, cols, flag, threshold)
			+ count(i, j + 1, rows, cols, flag, threshold)
			+ count(i, j - 1, rows, cols, flag, threshold)+1;
	}

	int sum(int x)
	{
		int sum = 0;
		do { sum += x % 10; }
		while ((x=x/10)>0);
		return sum;
	}
};

完结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值