剑指offer——旋转数组的最小值
来源:互联网 发布:孙俪的等花开淘宝 编辑:程序博客网 时间:2024/05/18 00:34
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
RevolveArray.h
test.cpp
<strong><span style="font-size:18px;">#pragma once#include <iostream>using namespace std;#include <assert.h>template<class T>T& RevolveArray(T* arr,size_t size,size_t k){assert(arr);T min = arr[0];T tmp = 0;while (k--){tmp = arr[0];for (int index = 1;index < size;++index){if (min > arr[index]){min = arr[index];}arr[index - 1] = arr[index];}arr[size-1] = tmp;}return min;}</span></strong>
test.cpp
<strong><span style="font-size:18px;">#define _CRT_SECURE_NO_WARNINGS 1#include "RevolveArray.h"void test1(){//int arr[] = {1,2,3,4,5};int arr[] = {8,3,7,2,0};int size = sizeof(arr)/sizeof(arr[0]);int count = 0;cout<<"请输入需要旋转数组的个数"<<endl;cin>>count;int min = RevolveArray<int>(arr,size,count);for (int i = 0;i < size;i++){cout<<arr[i]<<" ";}cout<<endl<<"min = "<<min<<endl;}int main(){test1();system("pause");return 0;}</span></strong>
0 0
- 剑指offer——旋转数组的最小值
- 【剑指offer】旋转数组的最小值
- [剑指offer]旋转数组的最小值
- 【剑指offer】旋转数组的最小值
- 【剑指offer】旋转数组的最小值
- 【剑指offer之旋转数组的最小值】
- 剑指offer -- 旋转数组最小值
- 剑指 offer——面试题8求旋转数组的最小值
- 剑指Offer:面试题8——旋转数组的最小值(java实现)
- 剑指offer系列之六:旋转数组的最小值
- 【剑指offer】题8:旋转数组的最小值
- Offer题8 旋转数组的最小值
- 剑指offer之旋转数组中的最小值
- 剑指offer(5) 旋转数组中的最小值
- 剑指offer:求一个旋转数组中的最小值
- 【剑指offer】面试题8:旋转数组中的最小值
- 剑指offer 面试题8 输出旋转数组最小值
- LintCode-剑指Offer-(159)寻找旋转排序数组中的最小值
- 手机开发实战191——手机软件测试1
- 移动端输入框的踩坑之旅
- 手机开发实战192——手机软件测试2
- QString与QByteArray互相转换的方法
- Android通知栏微技巧,那些你所没关注过的小细节
- 剑指offer——旋转数组的最小值
- strcspn
- MyEclipse的输入提示配置
- 手机开发实战193——手机软件测试3
- HDU 3473 划分树
- 练习四 1003
- 【数据解析】Android Json数据的解析
- 乐事在线笔试题
- Android中的Service