标签:ble profile solution bsp 直接 png int tps stc
links:
https://leetcode.com/problems/peak-index-in-a-mountain-array/
我的思路,直接遍历查找,找到第一个变小的数的位置,之前的那个位置就目的index
class Solution(object): def peakIndexInMountainArray(self, A): """ :type A: List[int] :rtype: int """ index = 0 for i in range(len(A)-1): if A[i] > A[i+1]: index = i break return index
看了其他人的解决方案,如下:
class Solution2(object): def peakIndexInMountainArray(self, A): """ :type A: List[int] :rtype: int """ return A.index(max(A))
做了下小小的测试,这里让长度尽量大一些(这里N=50000+50000),看下两者执行速度上的差距。
import cProfile A = [i for i in range(50000)] + [50000-j for j in range(50000)] s = Solution() s2 = Solution2() def testCase2(): cProfile.run("s.peakIndexInMountainArray(A)") cProfile.run("s2.peakIndexInMountainArray(A)")
发现方案2要快一些。
leetcode.852.PeakIndexinaMountainArray
标签:ble profile solution bsp 直接 png int tps stc
原文地址:https://www.cnblogs.com/Wolfanature/p/11462615.html