随笔分类 -  binary search

274. H-Index
摘要:Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index. According 阅读全文

posted @ 2018-07-18 09:06 猪猪🐷

350. Intersection of Two Arrays II
摘要:Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any order. Follow up: What if the given 阅读全文

posted @ 2018-07-18 08:51 猪猪🐷

69. Sqrt(x)
摘要:5 1 5 Mid = 3 9 > 5 Right = 3 Left = 1 Mid = 2 注意这个退出条件 , 和其他 二分不太一样, 0 8 4 16 > 8 Right = 4 Mid = 2 Left = 0 2 * 2 < 8, 3 * 3 > 8 4. 8. 9 2. x. 3 Ret 阅读全文

posted @ 2018-07-18 08:25 猪猪&#128055;

240. Search a 2D Matrix II
摘要:如果不是那种把所有的行都放在一起, 可以是一长串的 sorted numbers 的 话, 就用这种筛选法 We start search the matrix from top right corner, initialize the current position to top right c 阅读全文

posted @ 2018-07-18 08:24 猪猪&#128055;

74. Search a 2D Matrix
摘要:Use binary search. n * m matrix convert to an array => matrix[x][y] => a[x * m + y] an array convert to n * m matrix => a[x] =>matrix[x / m][x % m]; W 阅读全文

posted @ 2018-07-18 08:23 猪猪&#128055;

275. H-Index II
摘要:Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a researcher, write a function to compute the resea 阅读全文

posted @ 2018-07-18 08:22 猪猪&#128055;

162. Find Peak Element
摘要:这个题就是比较 mid 的时候, 和 mid + 1 index 的value 比较, 如果 mid + 1 的 value 比较大的话, 那么 local peak 肯定在 右边. it also covers the corner case , like 1 2 , then it return 阅读全文

posted @ 2018-07-18 08:21 猪猪&#128055;

540. Single Element in a Sorted Array
摘要:Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element 阅读全文

posted @ 2018-07-18 08:20 猪猪&#128055;

33. Search in Rotated Sorted Array
摘要:comment 的几个 等号 ,要注意为啥这么写 题目一看就知道是binary search。所以关键点在于每次要能判断出target位于左半还是右半序列。解这题得先在纸上写几个rotated sorted array的例子出来找下规律。Rotated sorted array根据旋转得多少有两种情 阅读全文

posted @ 2018-07-18 08:19 猪猪&#128055;

81. Search in Rotated Sorted Array II
摘要:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]). You are given a target value to search. If found... 阅读全文

posted @ 2018-07-18 08:19 猪猪&#128055;

154. Find Minimum in Rotated Sorted Array II
摘要:和Search in Rotated Sorted Array II这题换汤不换药。同样当A[mid] = A[end]时,无法判断min究竟在左边还是右边。 3 1 2 3 3 3 3 3 3 3 3 1 2 3 但可以肯定的是可以排除A[end]:因为即使min = A[end],由于A[end 阅读全文

posted @ 2018-07-18 08:18 猪猪&#128055;

153. Find Minimum in Rotated Sorted Array
摘要:先找出哪一段是sorted, 然后再根据这个去unsorted 的那部分找 min. 找 target 那道题是 先找出哪一段是sorted, 然后先看 target 在不在 sorted的 那一部分, 如果在, 就根据那一段继续二分, 如果不在, 去 unsorted 的那一段 二分去找。 这俩个 阅读全文

posted @ 2018-07-18 08:17 猪猪&#128055;

278. First Bad Version
摘要:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c 阅读全文

posted @ 2018-07-18 08:16 猪猪&#128055;

35. Search Insert Position
摘要:Index of the target or index of Smallest larger than the target 下面这几个例子都要考虑到才行, 面试的时候也是一样, 自己把这些情况都要考虑到, 面试的时候很可能不给你这么多的例子, 面试官可能指望你自己考虑周全, 把所有或者大部分的c 阅读全文

posted @ 2018-07-18 08:15 猪猪&#128055;

导航