Leetcode

白天 夜间 首页 下载 阅读记录
  我的书签   添加书签   移除书签

852. Peak Index in a Mountain Array

浏览 95 扫码 分享 2023-11-24 20:49:49
  • 1. Use binary search:

    https://leetcode.com/problems/peak-index-in-a-mountain-array/

    1. Use binary search:

    1. //12 ms 7.2 MB
    2. class Solution {
    3. public:
    4. int peakIndexInMountainArray(vector<int>& A) {
    5. int l = 0;
    6. int r = A.size() - 1;
    7. while(l < r){
    8. int median = l + (r - l) / 2;
    9. if(A[median] < A[median + 1])
    10. l = median + 1;
    11. else if(A[median] > A[median + 1])
    12. r = median;
    13. }
    14. return l;
    15. }
    16. };

    若有收获,就点个赞吧

    0 人点赞

    上一篇:
    下一篇:
    • 书签
    • 添加书签 移除书签
    • 97. Interleaving String
    • Top Interview Questions - Medium Collection
      • 1. Array and Strings (Q4 unfinished)
      • 2. Linked List
    • Top Interview Questions - Easy Collection
      • 1. Array (Q6 unfinished)
      • 2. Strings (Q8 unfinished)
      • 3. Linked List
      • 4. Trees
      • 5. Sorting and Searching
      • 6. Dynamic Programming (Q4 unfinished)
      • 8. Math
      • 9. Others
    • 1. Two Sum
    • 2. Add Two Numbers
    • 3. Longest Substring Without Repeating Characters
    • 4. Median of Two Sorted Arrays
    • 5. Longest Palindromic Substring
    • 6. ZigZag Conversion
    • 9. Palindrome Number
    • 15. 3 sum
    • 17. Letter Combinations of a Phone Number
    • 19. Remove Nth Node From End of List
    • 21. Merge Two Sorted Lists
      • 23. Merge k Sorted Lists
    • 34. Find First and Last Position of Element in Sorted Array
    • 36. Valid Sudoku
    • 38. Count and Say
    • 45. Jump Game II
    • 55. Jump Game
    • 62. Unique Paths
    • 69. Sqrt(x)
    • 74. Search a 2D Matrix
    • 83. Remove Duplicates from Sorted List
    • 88. Merge Sorted Array
    • 93. Restore IP Addresses
    • 98. Validate Binary Search Tree
    • 99. Recover Binary Search Tree
    • 100. Same Tree
      • 101. Symmetric Tree
    • 108. Convert Sorted Array to Binary Search Tree
    • 131. Palindrome Partitioning
    • 141. Linked List Cycle
      • 142. Linked List Cycle II
    • 147. Insertion Sort List
    • 148. Sort List
    • 153. Find Minimum in Rotated Sorted Array
      • 154. Find Minimum in Rotated Sorted Array II
    • 160. Intersection of Two Linked Lists
    • 162. Find Peak Element
    • 167. Two Sum II - Input array is sorted
    • 169. Majority Element
      • 229. Majority Element II
    • 190. Reverse Bits
    • 191. Number of 1 Bits
    • 201. Bitwise AND of Numbers Range
    • 203. Remove Linked List Elements
    • 226. Invert Binary Tree
    • 230. Kth Smallest Element in a BST
    • 234. Palindrome Linked List
    • 235. Lowest Common Ancestor of a Binary Search Tree
    • 236. Lowest Common Ancestor of a Binary Tree
    • 242. Valid Anagram
    • 257. Binary Tree Paths
    • 328. Odd Even Linked List
    • 337. House Robber III
    • 338. Counting Bits
    • 340. Longest Substring with At Most K Distinct Characters
    • 394. Decode String
    • 429. N-ary Tree Level Order Traversal
      • 589. N-ary Tree Preorder Traversal
      • 590. N-ary Tree Postorder Traversal
    • 437. Path Sum III
    • 438. Find All Anagrams in a String
    • 445. Add Two Numbers II
    • 450. Delete Node in a BST
    • 451. Sort Characters By Frequency
    • 459. Repeated Substring Pattern
    • 501. Find Mode in Binary Search Tree
    • 523. Continuous Subarray Sum
    • 525. Contiguous Array
    • 530. Minimum Absolute Difference in BST
    • 538. Convert BST to Greater Tree
    • 540. Single Element in a Sorted Array
    • 572. Subtree of Another Tree
    • 617. Merge Two Binary Trees
    • 662. Maximum Width of Binary Tree
    • 669. Trim a Binary Search Tree
    • 687. Longest Univalue Path
    • 700. Search in a Binary Search Tree
    • 701. Insert into a Binary Search Tree
    • 725. Split Linked List in Parts
    • 852. Peak Index in a Mountain Array
    • 872. Leaf-Similar Trees
    • 876. Middle of the Linked List
    • 917. Reverse Only Letters
    • 965. Univalued Binary Tree
    • 973. K Closest Points to Origin
    • 977. Squares of a Sorted Array
    • 979. Distribute Coins in Binary Tree
    • 1046. Last Stone Weight
      • 1124. Longest Well-Performing Interval
    • 1302. Deepest Leaves Sum
    • 1329. Sort the Matrix Diagonally
    • 1346. Check If N and Its Double Exist
    • 1356. Sort Integers by The Number of 1 Bits
    • 1365. How Many Numbers Are Smaller Than the Current Number
    • 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
    • 1512. Number of Good Pairs
    • 1545. Find Kth Bit in Nth Binary String
    暂无相关搜索结果!

      让时间为你证明

      展开/收起文章目录

      分享,让知识传承更久远

      文章二维码

      手机扫一扫,轻松掌上读

      文档下载

      请下载您需要的格式的文档,随时随地,享受汲取知识的乐趣!
      PDF文档 EPUB文档 MOBI文档

      书签列表

        阅读记录

        阅读进度: 0.00% ( 0/0 ) 重置阅读进度

          思维导图备注