python--c++--旋转数组的最小数字
来源:互联网 发布:虚拟机 mac os 优化 编辑:程序博客网 时间:2024/06/15 00:29
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
思路:当前半部分旋转之后,则遍历数组,当以一次出现前一个数大于后一个数时,则后一个数为数组的最小值。
c++解法:
int minNumberInRotateArray(vector<int> rotateArray) { if (rotateArray.size()==0) return 0; for (int i=0;i<rotateArray.size();i++) { //若前半部分旋转了 if(rotateArray[i]>rotateArray[i+1]) return rotateArray[i+1]; } //若前半部分没有旋转 return rotateArray[0]; }
python解法:
def minNumberInRotateArray(self, rotateArray): if len(rotateArray)==0: return 0 for i in range(0,len(rotateArray)): if rotateArray[i]>rotateArray[i+1]: return rotateArray[i+1] return rotateArray[0]
阅读全文
0 0
- python--c++--旋转数组的最小数字
- 旋转数组的最小数字 python
- Python:旋转数组的最小数字
- C语言 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 旋转数组的最小数字
- 解决数据导入(impdp)时ORA-29283报错
- python+scrapy+win10安装和使用
- 时间戳
- LoadRunner如何使用lr_save_var截取任意字符串长度
- spring 容器中配置 map 成员变量
- python--c++--旋转数组的最小数字
- boost设计TCP服务器中的strand与心跳机制实现
- 线程创建的两种方式
- linux 基于timerfd的纳秒级定时器
- 刷清橙OJ--A1032.画三角形2
- js从table中获得所需信息转为json
- 【VirtualBox】安装增强功能
- PHP可逆加密解密算法
- HTML5 5大存储方式总结