1. 程式人生 > >劍指offer每日六題---------day five

劍指offer每日六題---------day five

劍指offer題25:複製一個隨機連結串列

RondomListNode* RandomLinkCopy(RondomListNode *head)
{
	if (!head)return NULL;//頭結點是空直接返回

	RondomListNode *cur = head, *next = head->next;//由①②③變成①①②②③③,但複製出來的結點的隨機指標都指向空
	while (cur){
		RondomListNode *tmp = new RondomListNode(cur->val);
		cur->next = tmp; tmp->next = next;
		cur = next; if (next)next = next->next;
	}
	cur = head; next = cur->next;//將隨機指標指向正確的位置
	while (cur){
		next->rand = cur->rand ? cur->rand->next : NULL;
		cur = next->next; if (cur)next = cur->next;
	}
	cur = head; next = cur->next;//將新舊連結串列分離開
	RondomListNode *tmp = next;
	while (cur){
		cur->next = next->next; cur = next->next;
		if (cur){ next->next = cur->next; next = cur->next; }
		else next->next = NULL;
	}
	return tmp;//返回複製出來的新連結串列的頭結點
}

 

劍指offer題26:輸入一棵二叉搜尋樹,將該二叉樹轉換成一個排序的雙向連結串列。要求不能建立新的結點,只能調整樹中結點的指標

TreeNode *_TreeToLink(TreeNode *root)
{
	if (!root)return NULL;//遞迴返回條件

	TreeNode *left = _TreeToLink(root->left);//left是左邊單鏈表的最左結點
	TreeNode *right = _TreeToLink(root->right);//right是右邊單鏈表的最左結點

	TreeNode *left_to_right;//左邊單鏈表的最右結點
	left_to_right = left ? left->left : NULL;
	TreeNode *right_to_right;//右邊單鏈表的最右結點
	right_to_right = right ? right->left : NULL;

	root->left = left_to_right;//根的左孩子
	if (left_to_right)left_to_right->right = root;
	root->right = right;//根的右孩子
	if (right)right->left = root;

	if (!left)left = root;
	left->left = right ? right_to_right : root;

	return left;
}
TreeNode *TreeToLink(TreeNode *root)//遞迴思路
{
	if (!root)return NULL;

	TreeNode *head = _TreeToLink(root);
	head->left = NULL;
	return head;
}

 

劍指offer題28:陣列中有一個數字出現的次數超過陣列長度的一半,請找出這個數字,ps:如果不存在返回0.
  方法一:排序    方法二:map<K,V>    方法三:陣地攻守思想,先讓第一個元素作為第一個士兵來守陣地,count=1;
   遇到相同的元素++count,否則--count;當count=0時,又以新的i值作為守陣地的士兵,直到最後的士兵,那即是所求元素
 方法四:用堆找出一般元素,返回堆頂    

int MoreThanHalfNum(vector<int> arr)//方法三
{
	if (arr.empty())return 0;

	int count = 1, soldier = arr[0];
	for (int i = 1; i < arr.size(); ++i)
	{
		arr[i] == soldier ? ++count : --count;

		if (count == 0)
		{
			soldier = arr[i];
			count = 1;
		}
	}

	count = 0;
	for (int i = 0; i < arr.size(); ++i)
	{
		if (arr[i] == soldier)
			++count;
	}

	return count > arr.size() / 2 ? soldier : 0;
}

 

劍指offer題29:輸入n個整數,找出其中最小的k個數

#include<queue>
vector<int> GetLeastNumbers(vector<int> input, int k)
{
	if (input.size() < k || k <= 0)return vector<int>();//特別注意k的取值

	priority_queue<int> pq;//找最小的前k個數,建大根堆 (孩子 less 雙親)
	for (int i = 0; i < input.size(); ++i)
	{
		if (i < k)
			pq.push(input[i]);
		else if (pq.top() > input[i])
		{
			pq.pop();
			pq.push(input[i]);
		}
	}
	vector<int> arr(k); int index = k - 1;
	while (!pq.empty())
	{
		arr[index--] = pq.top();
		pq.pop();
	}
	return arr;
}

 

劍指offer題30:輸入一個數組,求該陣列的最大子陣列和是多少。ps,該陣列長度最少為1.

int FindGreatestSumOfSubArray(vector<int> arr)
{
	int max = arr[0], val = arr[0];
	for (size_t i = 1; i < arr.size(); ++i)
	{
		val = val < 0 ? 0 : val;//確定val是保留原值還是置零
		val += arr[i];
		if (val > max)max = val;//max代表原陣列[0,i]範圍內的最大子陣列加和
	}
	return max;
}