Skip to content

Commit 8283ba2

Browse files
committed
旋转数组的最小数字
1 parent 1efeb24 commit 8283ba2

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Code/6.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def minNumberInRotateArray(self, rotateArray):
4+
# write code here
5+
6+
length = len(rotateArray)
7+
8+
if length == 0:
9+
return 0
10+
11+
for i in range(length-1, 0, -1):
12+
if rotateArray[i - 1] > rotateArray[i]:
13+
return rotateArray[i]
14+
15+
16+
solution=Solution()
17+
print solution.minNumberInRotateArray([3,4,5,1,2])

Doc/旋转数组的最小数字.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 旋转数组的最小数字
2+
3+
<center>知识点:查找</center>
4+
5+
## 题目描述
6+
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
7+
8+
9+
## 解题思路
10+
11+
输入的数组是一个非减数组的旋转,即其原数组是一个非递减序列,所以其原数组的第一个值就是这个序列的最小数字。
12+
13+
只要从给定的旋转数组中找到第一个上一个值比其大的数,其就是最小数。
14+
15+
考虑到效率问题,从后往前找第一个前一个值比其大的数效率更高。
16+
17+
## 代码
18+
19+
[这里](../Code/6.py)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [从尾到头打印链表](./Doc/从尾到头打印链表.md)
66
- [重建二叉树](./Doc/重建二叉树.md)
77
- [用两个栈实现队列](./Doc/用两个栈实现队列.md)
8+
- [旋转数组的最小数字](./Doc/旋转数组的最小数字.md)

0 commit comments

Comments
 (0)