日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

剑指offer 算法 (分解让复杂问题简单)

發布時間:2025/10/17 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 剑指offer 算法 (分解让复杂问题简单) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述

輸入一個復雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點)。

解析:
分三步:
第一步:cloneList(pHead);復制鏈表,每個節點后新連接一個復制的自己的節點,random先置空;
第二步:connectRandom(pHead);依次給每個新節點的random連接
第三步:return apartList(pHead);把新建的結點按鏈表分離出來

/* struct RandomListNode {int label;struct RandomListNode *next, *random;RandomListNode(int x) :label(x), next(NULL), random(NULL) {} }; */ class Solution { public:RandomListNode* Clone(RandomListNode* pHead){cloneList(pHead);connectRandom(pHead);return apartList(pHead);}void cloneList(RandomListNode* pHead){RandomListNode* node;node=pHead;while(node!=NULL){RandomListNode* newNode;newNode=(RandomListNode*)malloc(sizeof(RandomListNode));newNode->label=node->label;newNode->random=NULL;newNode->next=node->next;node->next=newNode;node=newNode->next;}}void connectRandom(RandomListNode* pHead){RandomListNode* node;node=pHead;while(node!=NULL){RandomListNode* newNode;newNode=(RandomListNode*)malloc(sizeof(RandomListNode));newNode=node->next;if(node->random!=NULL){newNode->random=node->random->next;}node=newNode->next;}}RandomListNode* apartList(RandomListNode* pHead){RandomListNode* node=pHead;RandomListNode* cloneNode=NULL;RandomListNode* cloneList=NULL;if(node!=NULL){cloneNode=node->next;cloneList=cloneNode;node->next=cloneList->next;node=node->next;}while(node!=NULL){cloneNode->next=node->next;cloneNode=cloneNode->next;node->next=cloneNode->next;node=node->next;}return cloneList;} };
題目描述

輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只能調整樹中結點指針的指向。

解析:根據二叉搜索樹的特點,中序遍歷正好由樹的最小值搜到最大值;每當搜到一個點,加入鏈表;用lastNode保存當前的鏈表尾。

/* 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){TreeNode* lastNode=NULL;//鏈表尾convertList(pRootOfTree,&lastNode);//遞歸完lastHead為鏈表尾 so向左不斷搜索直到鏈表頭TreeNode* listHead=lastNode;while(listHead!=NULL&&listHead->left!=NULL)listHead=listHead->left;return listHead;}void convertList(TreeNode* pRootOfTree,TreeNode** lastNode){if(pRootOfTree==NULL)return;TreeNode* current=pRootOfTree;if(current->left)convertList(current->left,lastNode);current->left=*lastNode;if(*lastNode!=NULL)(*lastNode)->right=current;*lastNode=current;//更新lastNodeif(current->right)convertList(current->right,lastNode);} };
題目描述

輸入一個字符串,按字典序打印出該字符串中字符的所有排列。例如輸入字符串abc,則打印出由字符a,b,c所能排列出來的所有字符串abc,acb,bac,bca,cab和cba。
輸入描述:
輸入一個字符串,長度不超過9(可能有字符重復),字符只包括大小寫字母。]

解析:字符串逐個逐個字母替換,每個字母替換時逐個循環未使用的字符

class Solution { public:vector<string> Permutation(string str) {vector<string> v;if(str.empty())return v;Permutation(v,str,0);return v;}void Permutation(vector<string> &v,string str,int cnt){int size=str.size();if(cnt==str.size()-1)v.push_back(str);else{Permutation(v,str,cnt+1);for(int i=cnt+1;i<size;i++){if(str[i]!=str[cnt]){char ch=str[i];str[i]=str[cnt];str[cnt]=ch;Permutation(v,str,cnt+1);}}}} };

總結

以上是生活随笔為你收集整理的剑指offer 算法 (分解让复杂问题简单)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。