34. Find First and Last Position of Element in Sorted Array

34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity. 規則 尋找已排序過的陣列找到 first 和 last target 數 測試案例 Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Example 3: Input: nums = [], target = 0 Output: [-1,-1] 思路 透過已排序過的輸入,透過二元搜尋法切割查找 找到 first 和 last,回傳 index 如果沒找到則回傳 -1 實作 nums = [1, 3, 3, 5, 7, 8, 9, 9, 9, 15] target = 9 print(Solution()....

October 9, 2023 · Yish

98. Validate Binary Search Tree

98. Validate Binary Search Tree Given the root of a binary tree, determine if it is a valid binary search tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees....

October 6, 2023 · Yish