#include"defineNodeStruct.h" //该头文件定义了下面代码所用到的类型
https://mp.youkuaiyun.com/postedit/81742215
剑指offer题1:在一个二维数组中,每一行都按照从左往右递增的顺序排序,每一列都按照从上往下递增的顺序排序。
完成一个函数,输入一个数,判断该数是否在数组中。
// 思路一:从左下角来看,向上数字递减,向右数字递增,
因此,从左下角开始,当要查的数字比左下角数字大时,右移;否则上移
#include<vector>
bool Find(vector<vector<int>> arr, int target)
{
int rowcount = arr.size();
int colcount = arr[0].size();
int i, j;
for (i = rowcount - 1, j = 0; i >= 0 && j < colcount;)
{
if (target == arr[i][j])return true;
if (target < arr[i][j])--i;//target小于array[i][j],上移
else ++j;//否则右移
}
return false;
}
// 思路二:从左上角开始查找,先在第一行找到大于target的数,
然后,target比array[i][j]大时,下移;否则左移
bool Find2(vector < vector<int>> arr, int target)
{
int rowcount = arr.size();
int colcount = arr[0].size();
int i = 0, j = 0;
while (j < colcount - 1 && arr[i][j] <= target)++j;//第一行,从左往右,找到第一个大于target的数
while (i < rowcount && j >= 0)
{
if (arr[i][j] == target)return true;
if (arr[i][j] < target)//array[i][j]小于target,下移
++i;
else --j;//否则左移
}
return false;
}
剑指offer题2:实现一个函数,将一个字符串中的空格替换成"%20"。例如,当字符串We Are Happy经过变换之后变成 We%20Are%20Happy
//①如果给你一个string对象的字符串
#include<string>
string& ReplaceSpace(string& str)
{
for (size_t i = 0; i < str.size(); ++i)
{
if (str[i] == ' ')
str.replace(i, 1, "%20");
}
return str;
}
//②如果给你一个char*的字符串
char *ReplaceSpace2(char *str)
{
int count = 0, size = strlen(str);
for (size_t i = 0; i < size; ++i)
{
if (str[i] == ' ')count++;//先计算出有多少空格
}
int size2 = size + 2 * count + 1;
char *tmp = (char*)malloc(size2);//算出替换后的空间大小
count = 0;//从下面开始,count做str的index.
for (size_t i = 0; i <= size2; ++i)
{
if (str[count] == ' ')
{
tmp[i++] = '%';
tmp[i++] = '2';
tmp[i] = '0';
++count;
continue;
}
tmp[i] = str[count++];
}
return tmp;
}
剑指offer题3:输入一个单链表,从尾到头打印链表每个结点的值
// 思路一:递归 思路二:顺序遍历链表存入vector数组,
void PrintListFromTailtoHead(FlistNode *root)//递归
{
if (!root)return;
PrintListFromTailtoHead(root->next);
printf("%d ", root->val);
}
剑指offfer题4:输入某二叉树的前序遍历和中序遍历的结果,重建该二叉树。
// 思路一:递归 思路二:非递归
TreeNode *ReConstructBinaryTree(vector<int> pre, vector<int> vin)
{
if (pre.empty())return NULL;
vector<int> leftpre; vector<int> rightpre;
vector<int> leftvin; vector<int> rightvin;
int i = 0, j = 0;
TreeNode *root = new TreeNode(pre[i++]);
while (pre[0] != vin[j++])
{
leftpre.push_back(pre[i++]);
leftvin.push_back(vin[j - 1]);
}
while (i < pre.size())
{
rightpre.push_back(pre[i++]);
rightvin.push_back(vin[j++]);
}
root->left = ReConstructBinaryTree(leftpre, leftvin);
root->right = ReConstructBinaryTree(rightpre, rightvin);
return root;
}
剑指offer题5:用两个栈来实现一个队列,完成队列的Pop和Push操作。注,元素为int类型
#include<stack>
template<class T>
class Queue_By_TwoStack{
public:
void Push(const T &x)
{
s1.push(x);
}
void Pop()
{
if (s2.empty())
{
while (!s1.empty()){ s2.push(s1.top()); s1.pop(); }
}
s2.pop();
}
const T &Front()
{
if (s2.empty())
{
while (!s1.empty()){ s2.push(s1.top()); s1.pop(); }
}
return s2.top();
}
const T &Back()
{
if (s1.empty())
{
while (!s2.empty()){ s1.push(s2.top()); s1.pop(); }
}
return s1.top();
}
size_t size()
{
return s1.size() + s2.size();
}
private:
stack<T> s1;
stack<T> s2;
};
剑指offer题6:把一个数组最开始的若干元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个
旋转,输出旋转数组的最小元素。eg.数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1.
(注,数组给出的所有元素都大于0,若数组大小为0,则返回0)
思路:从头开始遍历,若发现下一个数比当前要小,那么下一个数一定是最小的数
#include<vector>
int MinNumberInRotateArray(vector<int> arr)
{
if (arr.empty())return 0;//数组大小为0时
size_t i = 0;
for (; i < arr.size() - 1; ++i)
{
if (arr[i] > arr[i + 1])
break;
}
return i == arr.size() - 1 ? arr[0] : arr[i + 1];
}