new coder 我自己做的几个题

本文深入探讨了C++中的链表、二叉树、递归算法等数据结构及其实现,包括链表反转、二叉树的构建与遍历、字符串处理等核心算法。同时,展示了如何在实际编程中运用这些数据结构解决复杂问题。

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

// Algorithm.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
using namespace std;
struct ListNode {
	int val;
	struct ListNode* next;
	ListNode(int x) :val(x), next(NULL) {
	}
	ListNode() :val(0), next(NULL) {}
};
struct TreeNode {
	int val;
	TreeNode* pleft;
	TreeNode* pright;
	TreeNode(int a) :val(a), pleft(NULL), pright(NULL) {};
	void PrintPre(TreeNode* pRoot) {
		if (pRoot == NULL) return;
		cout << pRoot->val << endl;
		PrintPre(pRoot->pleft);
		PrintPre(pRoot->pright);
		return;
	}
	void PrintIn(TreeNode* pRoot) {
		if (pRoot == NULL) return;
		PrintIn(pRoot->pleft);
		cout << pRoot->val << endl;
		PrintIn(pRoot->pright);
		return;
	}
	void PrintPost(TreeNode* pRoot) {
		if (pRoot == NULL) return;
		PrintPost(pRoot->pleft);
		PrintPost(pRoot->pright);
		cout << pRoot->val << endl;
		return;
	}
};
class Solution {
public:
	bool Find(int target, vector<vector<int>> array) {
		auto iVec = --array.end();
		auto iLastEle = --iVec->end();
		auto iVecbeg = array.begin();
		auto iFirstEle = iVecbeg->begin();
		if (target > * iLastEle) return false;
		if (target < *iFirstEle) return false;
		//至此可能存在
		for (auto iVec = array.begin(); iVec != array.end(); ++iVec) {
			if (target > * (--iVec->end())) continue;
			for (auto iEle = iVec->begin(); iEle != iVec->end(); ++iEle) {
				if (target == *iEle) return true;
			}
		}
		return false;
	};
	void replaceSpace(char* str, int length) {
		//直接在传入str上替换
		int ich = 0;
		while (ich < length) {
			if (str[ich] == ' ') {
				//先把空格到length的字符保存
				int lenPG = (length - 1) - (ich + 1) + 1;
				char* tempch = new char[lenPG];
				char* tempch1 = tempch;
				for (int i = ich + 1; i < length; ++i) {
					tempch[i - ich - 1] = str[i];
					//*(tempch1++) = str[i];
				}
				//增加
				str[ich++] = '%';
				str[ich++] = '2';
				str[ich++] = '0';

				//回填屁股
				int ibe = ich;
				char* tempch2 = tempch;
				for (int i = 0; i < lenPG; ++i) {
					str[ibe++] = tempch[i];
					//str[ibe++] = *(tempch2++);
				}
				length = length + 2;
				delete[]tempch;
				continue;
			}
			else if (str[ich] == '\0') {
				break;
			}
			else {
				ich++;
				continue;
			}
		}
		str[ich] = '\0';
		return;

		//赋值替换空格到新的字符串 然后地址给str
		/*char* newstr = new char[256];
		int ich = 0;
		int inew = 0;
		while (ich<length) {
			if (str[ich] != ' ') {
				newstr[inew++] = str[ich];
				++ich;
				continue;
			}else {
				newstr[inew++] = '%';
				newstr[inew++] = '2';
				newstr[inew++] = '0';
				++ich;
				continue;
			}
		}
		newstr[inew] = '/0';
		str = newstr;*/
	};
	vector<int> printListFromTailToHead(ListNode* head) {
		vector<int> vectemp;
		vectemp.clear();
		if (head == NULL) return vectemp;
		vectemp.push_back(head->val);
		struct ListNode* pnext = head->next;
		while (pnext != NULL) {
			vectemp.push_back(pnext->val);
			pnext = pnext->next;
		}
		vector<int> vecres;
		vecres.clear();
		auto ite = --vectemp.cend();
		for (; ite != vectemp.cbegin(); --ite) {
			vecres.push_back(*ite);
		}
		vecres.push_back(*ite);
		return vecres;
	};
	TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin) {
		//1、找根节点
		vector<int>::iterator iRootVal = pre.begin();
		TreeNode* Root = new TreeNode(*iRootVal);
		if (pre.size() == 1) return Root;
		//2、在中序遍历序列中找左、右子树
		vector<int> vinL;
		vinL.clear();
		vector<int>::iterator ipre = vin.begin();
		for (; *ipre != *iRootVal; ++ipre) {
			vinL.push_back(*ipre);
		}
		vector<int>vinR;
		vinR.clear();
		++ipre;
		for (; ipre != vin.end(); ++ipre) {
			vinR.push_back(*ipre);
		}
		//3、在前序遍历中找左、右子树
		vector<int>preL, preR;
		preL.clear();
		preR.clear();
		for (auto i = (++pre.begin()); i != pre.end(); ++i) {
			for (auto j = vinL.begin(); j != vinL.end(); ++j) {
				if (*i == *j) {
					preL.push_back(*i);
					break; //unique
				}
			}
			for (auto k = vinR.begin(); k != vinR.end(); ++k) {
				if (*i == *k) {
					preR.push_back(*i);
					break; //unique
				}
			}
		}
		TreeNode* NodeL = new TreeNode(0);
		if (preL.size() <= 0) {
			NodeL = NULL;
		}
		else {
			NodeL = reConstructBinaryTree(preL, vinL);
		}
		TreeNode* NodeR = new TreeNode(0);
		if (preR.size() <= 0) {
			NodeR = NULL;
		}
		else {
			NodeR = reConstructBinaryTree(preR, vinR);
		}
		Root->pleft = NodeL;
		Root->pright = NodeR;
		return Root;
	}
};
int main()
{

	Solution mySol;
	vector<int> vec1 = {1,2,3};
	vector<int> vec2 = { 4,5,6 };
	vector<int> vec3 = { 7,8,9 };
	vector<vector<int>> vec = { vec1,vec2,vec3 };
	bool bFound = mySol.Find(5,vec);
    std::cout << "Hello World!\n";
	std::cout << bFound << std::endl;
	char str[256] = "We Are Happy";
	cout << str << endl;
	int len = strlen(str);
	mySol.replaceSpace(str, len);
	cout << str << endl;
	int nCount = 0;

	ListNode* head = new ListNode(0);
	nCount++;
	ListNode* pnode = head;
	for (int i = 1; i < 3; ++i) {
		ListNode* node = new ListNode(i);
		pnode->next = node;
		nCount++;
		pnode = node;
	}
	ListNode** addressarr = new ListNode*[nCount];
	ListNode** addresstemp = addressarr;
	*addressarr = head;
	ListNode* pnext = head->next;
	while (pnext != NULL) {
		addressarr++;
		*addressarr = pnext;
		pnext = pnext->next;
	}
	/*pnode = head;
	*addressarr = pnode;
	addressarr++;
	while(pnode->next != NULL) {
		pnode = pnode->next;
		*addressarr = pnode;
		addressarr++;
	}
	addressarr--;*/
	//释放链表 (单向链表只能从头寻到尾 如果每个节点都是堆指针 不能从头到尾释放内存 必须记录好单向链表指针数组)
	for (int i = 0; i < nCount; ++i) {
		/*delete* addresstemp;
		addresstemp++;*/
		delete* addressarr;
		addressarr--;
	}

	//前序和中序反算二叉树
	vector<int> pre = {1,2,4,7,3,5,6,8};
	vector<int> vin = {4,7,2,1,5,3,8,6};
	TreeNode* pRoot = mySol.reConstructBinaryTree(pre, vin);
	pRoot->PrintPre( pRoot);
	cout << endl;
	pRoot->PrintIn(pRoot);
	//宜用后序遍历方式释放二叉树节点堆指针 或者不论什么遍历方式在释放一个节点的左右子节点堆指针时记录左右子节点的左右子节点指针
	system("pause");
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值