目录
例2:查找无序数组中最长连续序列的长度,时间复杂度为O(n).
1.剑指Offer
面试题7:重建二叉树
题目描述:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:1.前序序列的第一个节点为根结点,中序序列根节点前的部分为左子树序列,根节点后的部分为右子树序列
2.首先在前序遍历中找到根节点,然后在中序遍历中找到根节点的位置,以此位置分别得到前序和中序遍历中的左右子树序列,最后采用递归实现。
代码:
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
int vin_size=vin.size();
if(vin_size==0){
return NULL;
}
vector<int> pre_left,pre_right,vin_left,vin_right;
int rootval=pre[0];
TreeNode* node=new TreeNode(rootval);
int p=0;
for(p;p<vin_size;p++){
if(vin[p]==rootval){
break;
}
}
for(int i=0;i<vin_size;i++){
if(i<p){
pre_left.push_back(pre[i+1]);
vin_left.push_back(vin[i]);
}
else if(i>p){
pre_right.push_back(pre[i]);
vin_right.push_back(vin[i]);
}
}
node->left = reConstructBinaryTree(pre_left,vin_left);
node->right = reConstructBinaryTree(pre_right,vin_right);
return node;
}
};
面试题22:链表中倒数第k个节点
题目描述:
输入一个链表,输出该链表中倒数第k个结点。
思路:1.由于本题是单向链表,只有从前往后的指针,故找倒数第k个节点等价于求从头开始的第n-k+1个节点,但由于不知道单链表的节点个数n故采用此方法需遍历两次链表,故舍弃
2.采用两个指针,第一个指针从头开始先走k-1步,然后在第k步第二个指针也从头开始遍历,当第一个指针到达尾节点,则第二个指针正好指向倒数第k个节点。
3.需要判断对(1)输入是否为空指针;(2)链表节点个数小于k个;(3)参数k等于0 的三种情况考虑代码的鲁棒性
考察点:代码鲁棒性
解析:该思路可延展去解决链表中间节点等问题,应学习使用两个指针(比如快慢指针)等方式解决链表遍历问题。
代码:
class Solution {
public:
ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
if(pListHead==nullptr||k<=0){
return NULL;
}
ListNode* pAhead=pListHead;
for(int i=0;i<k-1;i++){
if(pAhead->next!=nullptr){
pAhead=pAhead->next;
}
else{
return NULL;
}
}
ListNode* pBehind=pListHead;
while(pAhead->next){
pAhead=pAhead->next;
pBehind=pBehind->next;
}
return pBehind;
}
};
2.Leetcode
例1:已知字符串s,求s所有回文串的划分。
题目描述:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s ="aab",
Return
[
["aa","b"],
["a","a","b"]
]
思路:若要求输出所有可能的解,往往都是要用深度优先搜索,若要求找出最优的解或解的数量,往往使用动态规划。
代码:
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<vector<string>> res;
vector<string> cur;
dfs(s,cur,res);
return res;
}
bool isPalindrome(string s){
return s==string(s.rbegin(),s.rend());
}
void dfs(string s,vector<string> &cur,vector<vector<string>> &res){
if(s==""){
res.push_back(cur);
return;
}
for(int i=1;i<=s.length();i++){
string sub=s.substr(0,i);
if(isPalindrome(sub)){
cur.push_back(sub);
dfs(s.substr(i,s.length()-i),cur,res);
cur.pop_back();
}
}
}
};
注意:代码中dfs函数中将cur放入res中的条件为s==""(s已是空字符串)而非s==
" "(s为空格),两者本质是不一样的,编程时要格外注意。
例2:查找无序数组中最长连续序列的长度,时间复杂度为O(n).
题目描述:
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
For example,
Given[100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.
Your algorithm should run in O(n) complexity.
思路:
用散列表,首先将数字都映射到散列表上,然后对于每个数字,找到后就删除,接着找该数字相邻大小的数字,只要找到就删除,最后求出长度。哈希表查找时间复杂度是O(1),因为每个数字只会添加一次,删除一次,所以总体复杂度是O(n)。
代码:
class Solution {
public:
int longestConsecutive(vector<int> &num) {
unordered_set<int> st(num.begin(),num.end());
int ans=0;
for(auto v:num){
if(st.find(v)==st.end()) continue;
st.erase(v);
int l=v,r=v;
while(st.find(r+1)!=st.end()) st.erase(++r);
while(st.find(l-1)!=st.end()) st.erase(--l);
ans=max(r-l+1,ans);
}
return ans;
}
};