(1) 对于重复的问题要想到递归。
(2) 函数的 递归调用尽量在递归调用前就进行有效判断,无效就提前结束, 避免引起不必要的函数调用。例如:19
16.反转链表
题目描述: 输入一个链表,反转链表后,输出新链表的表头。
思路:
(1) 输入空链: 直接返回NULL
(2) 只有一个结点: 返回原头节点
(3) 多个结点: 针对普通情况进行分析,假设有:i -> j -> k,如果想要反转这段链表,首先需要j - > i,但是这样的话节点k就会丢失,因此需要三个指针来分别指向连续的三个结点。
class Solution {
public:
ListNode* ReverseList(ListNode* pHead)
{
ListNode* pCurr = pHead;
ListNode* pPrev = NULL;
ListNode* pNext = NULL;
while(pCurr != NULL)
{
pNext = pCurr->next;
pCurr->next = pPrev;
pPrev = pCurr;
pCurr = pNext;
}
return pPrev;
}
};
17.合并两个排序的链表
题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
思路: 两个链表的头结点不断的比较大小,较小者为新链表的头节点。接着在两个链表剩余的部分中重新找次较小者。 这就是一个重复的问题,对于重复的问题要想到递归。
(1) pHead1为空: 返回pHead2
(2) pHead2为空: 返回pHead1
(3) pHead1,pHead2均为空的情况在(1)(2)中已经隐含者解决了
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
/*通过实例可以看出就是不断寻找新的头节点的问题,
相同的子任务可以通过递归来解决。*/
if(pHead1 == NULL)
return pHead2;
else if(pHead2 == NULL)
return pHead1;
ListNode* pNewHead = NULL;
if(pHead1->val <= pHead2->val)
{
pNewHead = pHead1;
pNewHead->next = Merge(pHead1->next,pHead2);
}
else
{
pNewHead = pHead2;
pNewHead->next = Merge(pHead1,pHead2->next);
}
return pNewHead;
}
};
18.树的子结构
题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
思路:
(1) 输入为空,返回false
(2) 遍历A,找到与B根节点相等的位置
(3) 结点数值相同,在接着判断B的左右子树是不是也属于A
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
/* 1.输入为空,返回false
2.遍历A,找到与B根节点相等的位置
3.结点数值相同,在接着判断B的左右子树是不是也属于A
*/
bool result = false;
if(pRoot1 != NULL && pRoot2 != NULL)
{
//主函数用于遍历TreeA,找到与TreeB根节点相等的结点
if(pRoot1->val == pRoot2->val)
result = DoseTree1HaveTree2(pRoot1,pRoot2);
//如果找不到,那么就再去root的左儿子当作起点,去判断时候包含Tree2
if(!result)
result = HasSubtree(pRoot1->left,pRoot2);
//如果还找不到,那么就再去root的右儿子当作起点,去判断时候包含Tree2
if(!result)
result = HasSubtree(pRoot1->right,pRoot2);
}
return result;
}
bool DoseTree1HaveTree2(TreeNode* pRoot1, TreeNode* pRoot2)
{
//子函数用于递归判断TreeB的左右子树是否也属于TreeA某节点的相应子树
//如果Tree2已经遍历完了都能对应的上,返回true
if(pRoot2 == NULL)
return true;
//如果Tree2还没有遍历完,Tree1却遍历完了。返回false
if(pRoot1 == NULL)
return false;
//如果其中有一个点没有对应上,返回false
if (pRoot1->val != pRoot2->val)
return false;
//如果根节点对应的上,那么就分别去子节点里面匹配
return DoseTree1HaveTree2(pRoot1->left,pRoot2->left) &&
DoseTree1HaveTree2(pRoot1->right,pRoot2->right);
}
};
19.二叉树的镜像
题目描述: 操作给定的二叉树,将其变换为源二叉树的镜像。
思路: 先考虑普通情况,首先交换根节点a的左右子树b,c,然后再分别交换子树b,c各自的左右子树。这样看就是一个递归的镜像所有节点的左右子树。既然是递归就一定有终止递归的条件。
(1) 输入为空,直接跳出
(2) 输入的节点没有儿子,直接跳出
(3) 输入的节点只有一个儿子,也要按照两个儿子的情况进行交换指针。
class Solution {
public:
void Mirror(TreeNode *pRoot)
{
//空树不操作
if(pRoot == NULL)
return ;
//树叶不用操作
if(pRoot->left==NULL && pRoot->right==NULL)
return;
std::swap(pRoot->left,pRoot->right);
//递归镜像左子树,镜像右子树,这里提前对子树的根节点进行非空判断,避免无用的程序调用
if(pRoot->left)
Mirror(pRoot->left);
if(pRoot->right)
Mirror(pRoot->right);
}
};
20.顺时针打印矩阵
题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
思路: 遍历矩阵分为四个方向,先后顺序为:右->下->左->上。逐个分析。
(1) 右:至少有一列,从左往右读取所有元素。
(2) 下:至少有两行,从第二行往下读取所有元素
(3) 左:至少有两行两列,从倒数第二列往左遍历所有元素。
(4) 上:至少有三行两列,从倒数第二行往上遍历到正数第二行元素。
一个循环结束,整个矩阵会缩小一圈,因此更新四个标志。
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix)
{
vector<int> result;
if(matrix.empty())
return result;
int endCol = matrix[0].size()-1;
int endRow = matrix.size()-1;
int startCol =0, startRow = 0;
while(startCol <= endCol && startRow <= endRow)
{
if(endCol >= startCol)//右
for(int i=startCol; i<=endCol; ++i)
result.push_back(matrix[startRow][i]);
if(endRow > startRow)//下
for(int i=startRow+1; i<=endRow; ++i)
result.push_back(matrix[i][endCol]);
if(endCol > startCol && endRow > startRow)//左
for(int i=endCol-1; i>=startCol; --i)
result.push_back(matrix[endRow][i]);
if(endCol > startCol && endRow > (startRow+1))//上
for(int i=endRow-1; i>startRow; --i)
result.push_back(matrix[i][startCol]);
startCol++;
startRow++;
endCol--;
endRow--;
}
return result;
}
};
#include <iostream>
#include <vector>
using namespace std;
class solution
{
public:
vector<int> ReadArr(vector<vector<int>> & arr,int n)
{
vector<int> result;
if(arr.empty())
{
cerr<<"empty arr"<<endl;
return result;
}
int left = 0, right=n-1;
int top = 0, bottum= n-1;
while(left <= right && top<=bottum)
{
//从左往右
if(left <= right)
{
for(int i=left;i<=right;++i)
result.push_back(arr[top][i]);
}
//从上到下
if(left <= right && top < bottum)
{
for(int i=top+1;i<=bottum;++i)
result.push_back(arr[i][right]);
}
//从右往左
if(left < right && top < bottum)
{
for(int i=right-1;i>=left;--i)
result.push_back(arr[bottum][i]);
}
//从下到上
if(left < right && (top+1) < bottum)
{
for(int i=bottum-1;i>top;--i)
result.push_back(arr[i][left]);
}
left++;
right--;
top++;
bottum--;
}
return result;
}
};
int main()
{
int n;
cin>>n;
if(n<=0)
cout<<"error: n>0"<<endl;
cout<<"n = "<<n<<endl;
vector<vector<int>> arr(n);
cout<<"arr.size()="<<arr.size()<<endl;
int temp;
int count=0;
while(cin>>temp)
{
arr[count/n].push_back(temp);
count++;
}
for(auto &i:arr)
{
for(auto &j:i)
cout<<j<<" ";
cout<<endl;
}
vector<int> result;
solution s;
result = s.ReadArr(arr,n);
for(auto &i:result)
cout<<i<<" ";
cout<<endl;
return 0;
}