作文以记之 ~ 寻找旋转排序数组中的最小值2

0、前言

这是一篇题解,具体题目描述可 点击此处 进行查看!c此题与 作文以记之 ~ 寻找旋转排序数组中的最小值 这篇博客中的题极为相似,只不过所给示例中出现了重复的值。

写此博客没有其他想法,就是想记录一下

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.2 方法2 ~ 利用二分法

2.2.1 思路

这种方法就是利用正常的二分查找,然后逐步去寻找最小值,但需要注意的是,此题需要增添一个对重复值的判断,关于这一点,最简单的就是将遇到重复值的位置也就是end进行左移,直至遇不到重复值。这种方法可以找到数组的最小值,但无法用其准确的找到最大值!

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;
}
class Solution {
public:
    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 if(nums[mid] > nums[end])
                start = mid+1;
            else
                end--;
        }
        return nums[start];
    }
};
/*方法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 if(nums[mid] > nums[end])
            start = mid+1;
        else //遇到相同的值则左移
            end--;
    }
    return nums[start];
}

void test()
{
	//原数组为:0,1,2,3,4,5,6,7
	vector<int> nums = { 2, 2, 2, 2, 0, 2 };
	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;
}

3、总结

此题不难,有简单的解法,也有比较有意思的做法。但个人建议,做题时候不要单纯的使用API,虽然使用API爽,一直使用一直爽,但对程序编码思维没有太大帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值