標籤:: BinarySearch

0

Kth Smallest Element in a Sorted Matrix

Kth Smallest Element in a Sorted MatrixGiven a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the kth smal

0

Longest Increasing Subsequence

Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence. For example:Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence

0

Find the Duplicate Number

Find the Duplicate NumberGiven an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is onl

0

H-Index II

H-Index IIFollow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? Default:123func hIndex(citations []int) int {} 解答思路:建議可以先參考先前H-

0

Search a 2D Matrix II

Search a 2D Matrix IIWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to ri

0

Kth Smallest Element in a BST

Kth Smallest Element in a BSTGiven a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ? k ? BST’s total elements. Fol

0

Minimum Size Subarray Sum

Minimum Size Subarray SumGiven an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead. F

0

Two Sum II - Input array is sorted

Two Sum II - Input array is sortedGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should

0

Find Peak Element

Find Peak ElementA peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multipl

0

Search in Rotated Sorted Array II

Search in Rotated Sorted Array II Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose an array sorted in a

0

Search a 2D Matrix

Search a 2D MatrixWrite an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first i

0

Sqrt(x)

Sqrt(x)Implement int sqrt(int x). Compute and return the square root of x. 提示 解題應用 Math 規律觀查 Default:123func mySqrt(x int) int {} 解答思路:這題姑且只用最簡單的方式去解決,當然有人用牛頓法的方法來找出結果,不過一開始我就是只用最

0

Pow(x, n)

Pow(x, n)Implement pow(x, n). 提示 解題應用 BinarySearch 二分法 Math 規律觀查 Default:123func myPow(x float64, n int) float64 {} 解答思路:先考慮次方值n只有正數的情況下,如果n是偶數的話x^n就可以變成(x*x)^(n/2),也就是說每次的次方數都可

0

Search Insert Position

Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no dupl

0

Search for a Range

Search for a RangeGiven an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(lo

0

Search in Rotated Sorted Array

Search in Rotated Sorted ArraySuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target v

0

Heaters

HeatersWinter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizo

0

Arranging Coins

Arranging CoinsYou have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can b

0

Valid Perfect Square

Valid Perfect SquareGiven a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1:12

0

Intersection of Two Arrays

Intersection of Two ArraysGiven two arrays, write a function to compute their intersection. ###For Example: 1Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result

0

Intersection of Two Arrays II

Intersection of Two Arrays IIGiven two arrays, write a function to compute their intersection. For Example:1Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the resul