作文以记之 ~ 寻找旋转排序数组中的最小值
0、前言
这是一篇题解,具体题目描述可 点击此处 进行查看!刚开始做这个题时,以为是 作文以记之 ~ 轮转数组 这篇博客中同类型的题,后面才发现并不是,只不过有些共同点。具体代码实现以及其他内容可 点击此处 进行查看!
没有其他想法,就是想记录一下
1、题目描述
2、解题思路
2.1 方法1 ~ 调用API
2.1.1 思路
因为这个题其实只是让找出数组中的最小值,所以最简单的方法就是先给数组排序,然后找出最小的数即可,这种可以直接调用API
实现,如下面的程序!
2.1.2 程序代码
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
void shownums(vector<int>& nums)
{
for (int& d : nums)
cout << d << " ";
cout << endl;
}
/* 方法1 ~ 调用API */
int findMin(vector<int>& nums)
{
sort(nums.begin(), nums.end());
return nums[0];
}
void test()
{
//原数组为:0,1,2,3,4,5,6,7
vector<int> nums = { 4, 5, 6, 7, 0, 1 };
cout << "\n需处理的数组:"; shownums(nums); cout << endl;
vector<int> tmp = nums;
sort(tmp.begin(), tmp.end());
cout << "\n目标数组的原数组:"; shownums(tmp); cout << endl;
cout << "\n最小值:" << findMin(nums) << endl << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.1.3 运行结果
2.2 方法2 ~ 利用二分法
2.2.1 思路
这种方法就是利用正常的二分查找,然后逐步去寻找最小值,但需要注意的是,这种方法可以找到数组的最小值,但无法用其准确的找到最大值!
2.2.2 程序代码
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
void shownums(vector<int>& nums)
{
for (int& d : nums)
cout << d << " ";
cout << endl;
}
/*方法2 利用二分法*/
int findMin(vector<int>& nums)
{
int start = 0, end = nums.size() - 1;
while (start < end)
{
int mid = start + (end - start) / 2;
if (nums[mid] < nums[end])
end = mid;
else
start = mid + 1;
}
return nums[start];
}
void test()
{
//原数组为:0,1,2,3,4,5,6,7
vector<int> nums = { 4, 5, 6, 7, 0, 1 };
cout << "\n需处理的数组:"; shownums(nums); cout << endl;
vector<int> tmp = nums;
sort(tmp.begin(), tmp.end());
cout << "\n目标数组的原数组:"; shownums(tmp); cout << endl;
cout << "\n最小值:" << findMin(nums) << endl << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2.2.3 运行结果
3、总结
此题不难,有简单的解法,也有比较有意思的做法。但个人建议,做题时候不要单纯的使用API
,虽然使用API
爽,一直使用一直爽,但对程序编码思维没有太大帮助!