剑指offer之每日六题
1.二维数组中的查找
2.替换空格
3.从尾到头打印链表
4.重建二叉树
5.用两个栈实现队列
6.旋转数组的最小数字
1.二维数组的查找
/**
这一题有两种方法可以做,一种就是从第一行最后一个元素开始查找,
如果坐标所在的元素比要查找的那个值小,则向下查找,如果坐标所在元素
比所要查找的那个值大,则向左查找
第二种可以直接二分每一行,时间复杂度O(nlog(n))
**/
bool Find(int target, vector<vector<int> > array) {
int i=0,j=array[0].size()-1;
while(i>=0&&i<array.size()&&j>=0&&j<array[0].size()){
if(array[i][j]>target)
j--;
else if(array[i][j]<target)
i++;
else
return true;
}
return false;
}
2.替换空格
/**
用了另一个数组来保存原来数组的元素,如果遇到空格,则存入%20
**/
void replaceSpace(char *str,int length) {
int cnt=0;
char str2[4*length];
memset(str2,0,sizeof(str2));
for(int i=0;i<length;i++){
if(str[i]==' '){
str2[cnt++]='%';
str2[cnt++]='2';
str2[cnt++]='0';
}
else{
str2[cnt++]=str[i];
}
}
strcpy(str,str2);
}
3.从尾到头打印链表
/**
本题方法很多,可以直接用两个数组转置一下,或者直接调用STL里面
的reverse函数
我想巩固一下数据结构的知识,便想将链表倒置然后存到数组里,很多
大神都是用递归解决的
**/
vector<int> printListFromTailToHead(ListNode* head)
{
vector<int> s;
if(head==NULL)
{
return s;
}
else if(head->next==NULL)
{
s.push_back(head->val);
return s;
}
ListNode* now=head->next;
ListNode* pre=head;
ListNode* temp=head->next->next; //链表中下一个元素的指针
while(now)
{
temp=now->next; //temp后移
now->next=pre; //使后面的元素的指针指向前面的一个元素
pre=now; //pre指针后移
now=temp; //now指针后移
}
head->next=NULL;
//原本链表的第一个元素也就是现在的最后一个元素的next指向NULL
while(pre)
{
s.push_back(pre->val);
pre=pre->next;
}
return s;
}
/**递归**/
class Solution
{
public:
vector<int> dev;
vector<int>& printListFromTailToHead(struct ListNode* head)
{
if(head!=NULL)
{
if(head->next!=NULL)
{
dev=printListFromTailToHead(head->next);
}
dev.push_back(head->val);
}
return dev;
}
};
4.重建二叉树
/**
这题是分治的思想吧,从先序遍历中很容易找到根节点,然后在中序遍历中
找到根节点的位置,中序遍历左边的数的序列是这颗树的左子树的中序遍历,
右边的序列是右子树的中序遍历,同理,从前序遍历中找到相同数量的元素,
就可以看作是左子树的先序遍历和右子树的先序遍历,就这样一步步分解,便
可得到结果
**/
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in)
{
if(pre.size()==0) return NULL;
else if (pre.size() == 1)
{
return new TreeNode(pre[0]);
}
TreeNode* root=new TreeNode(pre[0]);
int tmp=0;
for(; in[tmp]!=root->val; tmp++) {}
//找到根节点在中序遍历里面的位置
vector<int> pre_left=vector<int>(pre.begin()+1,pre.begin()+tmp+1);
vector<int> pre_right=vector<int>(pre.begin()+tmp+1,pre.end());
vector<int> in_left=vector<int>(in.begin(),in.begin()+tmp);
vector<int> in_right=vector<int>(in.begin()+tmp+1,in.end());
root->left=reConstructBinaryTree(pre_left,in_left);
root->right=reConstructBinaryTree(pre_right,in_right);
return root;
}
5.用两个栈实现队列
/**
挺水的,贴个代码
**/
class Solution
{
public:
void push(int node)
{
stack1.push(node);
}
int pop()
{
int ans;
if(stack2.empty())
{
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
}
ans=stack2.top();
stack2.pop();
return ans;
}
private:
stack<int> stack1;
stack<int> stack2;
};
6.旋转数组的最小数字
/**
这题原本以为就是一个普通的查找,没想到还能被大神们搞出花,看了一个二
分的代码,以前听学长说二分可以不是顺序的,这个算不算其中的一个例子?
思想是这样的:两个光标定位在首尾,如果中间元素mid大于left所在的元素,
则说明最小值在mid的后面,因为是将顺序排列的前面的元素放到后面,所以
如果在小的数字所在的那个区间的元素最多也就是等于前面的元素,同理,如
果mid所在的元素小于left所在的元素则令right=mid;
特殊情况是都相等的情况,这样判别不了小数字所在的区间,所以只能遍历
**/
int minNumberInRotateArray(vector<int> rotateArray)
{
if(rotateArray.size()==0) return 0;
int left=0,right=rotateArray.size()-1,mid;
while(rotateArray[left]>=rotateArray[right])
{
if(left+1==right) return rotateArray[right];
mid=left+(right-left)/2;
//如果left,right,mid上的值都相等,则只能顺序查找
if(rotateArray[left]==rotateArray[right]&&rotateArray[left]==rotateArray[mid])
{
int ans=rotateArray[left];
for(int i=left+1; i<right; i++)
{
if(rotateArray[i]<ans)
ans=rotateArray[i];
}
return ans;
}
if(rotateArray[mid]>=rotateArray[left])
left=mid;
else
right=mid;
}
return rotateArray[mid];
}