25、复杂链表的复制
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
思路分析
大致分三步
1)复制旧链表节点:将旧链表的节点一个个复制后插入到新链表,像下图那样,A是旧节点,A’是新节点。
2)置random指向:如上图所示,旧节点的下一个节点就是新节点,如果旧节点的random有指向,那么新节点的random应该是旧节点random的next。比如A的random指向C,那么A’的random就应该指向C的下一个节点C’。
3)分离新旧链表:首先让list和tail都指向pHead->next,如上图的A’位置,然后不断从原链表隔一个节点拆下来链到tail之后,把旧链表也在链上。
/*
struct RandomListNode {
int label;
struct RandomListNode *next, *random;
RandomListNode(int x) :
label(x), next(NULL), random(NULL) {
}
};
*/
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
RandomListNode*cur=pHead;
RandomListNode*list=nullptr;
RandomListNode*tail=nullptr;
if(cur==nullptr)return nullptr;
//拷贝原节点
while(cur)
{
RandomListNode*node=new RandomListNode(cur->label);//用cur的值构建新节点
RandomListNode*next=cur->next;//记录下cur的next,将node插入到cur和next中间
cur->next=node;
node->next=next;
cur=node->next;
}
//置random指向
cur=pHead;
while(cur)
{
if(cur->random)//如果cur的random不指向空
{
RandomListNode*next=cur->next;//cur->next为cur的拷贝
next->random=cur->random->next;
//cur的random的下一个位置就是next的random
}
else
cur->next->random=nullptr;
cur=cur->next->next;
}
//分链表
list=tail=pHead->next;
pHead->next=list->next;
cur=pHead->next;
while(cur)
{
RandomListNode*copy=cur->next;
cur->next = copy->next;
tail->next = copy;
tail = copy;
cur = cur->next;
}
return list;
}
};
26、二叉树与双向链表
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
定义了head和tail分别作为新双链表的头结点、尾节点,按照中序顺序将节点重新连接。
递归代码
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
TreeNode*tail=nullptr;
TreeNode*head=nullptr;
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(pRootOfTree==nullptr)return head;
Convert(pRootOfTree->left);
if(tail==nullptr)//叶子节点
{
head=pRootOfTree;
tail=pRootOfTree;
}
else
{
tail->right=pRootOfTree;
pRootOfTree->left=tail;
tail=pRootOfTree;
}
Convert(pRootOfTree->right);
return head;
}
};
非递归代码
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(pRootOfTree==nullptr)return nullptr;
TreeNode*head=nullptr;
TreeNode*tail=nullptr;
TreeNode*cur=pRootOfTree;
stack<TreeNode*> s;
while(cur!=nullptr||!s.empty())
{
while(cur!=nullptr)
{
s.push(cur);
cur=cur->left;
}
//cur到达最左节点的左叶子空节点,现在需要从栈中取数据,取访问此刻cur的祖先节点
cur=s.top();
s.pop();
if(tail==nullptr)
{
head=cur;
tail=cur;
}
else
{
tail->right=cur;
cur->left=tail;
tail=cur;
}
cur=cur->right;
}
return head;
}
};
27、字符串的排列
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
本题考查递归解决问题的思路。首先将输入的字符串转化为有序字符串。将字符串分为两部分,第一个字符,和后续N-1个字符。后续问题可以用递归函数。
首先应该排序,排序之后,比如abcd 首先是a作为第一个字符,后续是bcd 。第二次,首位的a和b互换位置,变成,b acd显然acd有序,再接着,首位的b和c互换位置,得c abd 又是有序的。
class Solution {
private: string temp;
private: vector<string> result;
public:
vector<string> Permutation(string str) {
vector<string> result; //创建字符串数组
int len = str.length(); //求出字符串长度,作为参数传递控制循环次数
if(!len) return result; //当输入为空时,直接返回
BubbleSort(str,0,len-1);
Permutations(result, str, 0, len);
return result;
}
void Permutations(vector<string>&result, string str,int index, int len){
//当索引指向字符串尾部时,将str压入数组
if (index == len){//index==len,说明首位置已经是末尾了,这时的递归输入已经只有一个元素了
result.push_back(str);
return;
}
for (int i = index; i < len; ++i) {
if (i!=index && str[i]== str[index]) continue;// 保证当输入多个重复字符时,不会重复计算
swap(str[i],str[index]);//每一次,交换第i个位置和首位置的元素
Permutations(result, str, index+1, len);
}
}
void BubbleSort(string &A,int left,int high){
int p,i;
bool flag;
int N = A.length();
for(p=N-1; p >= 0; p--){
flag = false;
for(i=0; i<p; i++){
if(A[i]>A[i+1]){
swap(A[i],A[i+1]);
flag = true;
}
}
if(flag == false) break;
}
}
};