基础知识:
二分查找算法:有序表、顺序存储、复杂度o(logn),一般用于强调有序数组查找时。
哈希表:o(1)时间根据关键字,得到所需的值。
二叉搜索树(排序树):根节点左边值小于根结点,右边值大于根结点。
int Binary_Search(int* data, int length, int key)
{
int low = 0, high = length - 1, mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (data[mid] < key)
low = mid + 1;
else if (data[mid] > key)
high = mid - 1;
else
return mid;
}
return -1;
}
1 把一个数组最开始的若干个元素搬到数组的末尾称为数组的旋转。
输入一个递增的排序数组的旋转,输出旋转数组的最小元素。例如{3,4,5,1,2}是{1,2,3,4,5}的一个旋转,最小值为1.**
int MinInOrder(int* data, int low, int high)
{
int min = INT_MAX;
for (int i = low; i <= high; ++i)
{
if (data[i] < min)
min = data[i];
}
return min;
}
int GetNumSmall(int* data, int length)
{
if (data == nullptr || length < 1)
throw new exception("Invalid parameters");
int low = 0, high = length - 1, mid=0;
if (data[high] > data[low])
return data[low];
while (low!=high-1)
{
mid = (low + high) / 2;
if (data[mid] == data[low] && data[low] == data[high]) //考虑{1,0,1,1,1}的情况,只能顺序查找最小值
return MinInOrder(data, low, high);
if (data[mid] >= data[high])
low = mid;
else if(data[mid]<=data[high])
high = mid;
}
return data[high];
}
2 数字在排序数组中出现的次数
统计一个数组在排序数组中出现的次数,例如输入{1,2,3,3,3,3,4,5},和数字3,输出4。
int GetFirstK(int* data, int k, int start, int end) //递归写法
{
if (start > end)
return -1;
int mid = (start + end) / 2;
if (data[mid] == k)
{
if (mid > 0 && data[mid - 1] != k || mid == 0)
return mid;
else
end = mid - 1;
}
else if (data[mid] > k)
end = mid - 1;
else
start = mid + 1;
return GetFirstK(data, k, start, end);
}
int GetFirstK(int* data, int k, int start, int end) //循环写法
{
while (start<=end)
{
int mid = (start + end) / 2;
if (data[mid] == k)
{
if (mid > 0 && data[mid - 1] != k || mid == 0)
return mid;
else
end = mid - 1;
}
else if (data[mid] > k)
end = mid - 1;
else
start = mid + 1;
}
return -1;
}
int GetLastK(int* data,int length, int k, int start, int end)
{
if (start > end)
return -1;
int mid = (start + end) / 2;
if (data[mid] == k)
{
if (mid < length-1 && data[mid + 1] != k || mid == length-1)
return mid;
else
start = mid + 1;
}
else if (data[mid] > k)
end = mid - 1;
else
start = mid + 1;
return GetLastK(data,length, k, start, end);
}
int GetNumK(int* data, int length, int k)
{
int number = 0;
if (data != nullptr && length > 0)
{
int first = GetFirstK(data, k, 0, length - 1);
int last = GetLastK(data, length, k, 0, length - 1);
if(first>-1 && last>-1)
number = last - first + 1;
}
return number;
}
3 第一个只出现一次的字符
char GetAppearOnce(const string& s)
{
if (s.size() == 0)
return '\0';
int hash[256] = { 0 };
for (int i = 0; i < s.size(); ++i)
hash[s[i]]++;
for (int i = 0; i < s.size(); ++i)
{
if (hash[s[i]] == 1)
return s[i];
}
return '\0';
}
4 二叉搜索树的后序遍历序列
输入一个整数数组,判断是否为某二叉搜索树的后序遍历序列。假设输入数组的任意两个数字不相同。
bool IsBST(int data[], int length)
{
if (data == nullptr || length < 1)
return false;
int root = data[length - 1];
int i, j;
for (i = 0; i < length; ++i)
{
if (data[i] > root)
break;
}
for (j = i; j < length; ++j)
{
if (data[j] < root)
return false;
}
bool left = true;
if (i > 0)
left = IsBST(data, i);
bool right = true;
if (j < length - 1)
right = IsBST(data+i, length-1-i);
return (left&&right);
}