随笔分类 -  Binary Search

摘要:Given 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 duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0参考 :http://www.cnb 阅读全文
posted @ 2014-02-08 03:09 Razer.Lu 阅读(241) 评论(0) 推荐(0)
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, 3, ... 阅读全文
posted @ 2014-02-08 02:17 Razer.Lu 阅读(185) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.click to show more practice.More practice:If you have figured out the O(n) solution, try 阅读全文
posted @ 2014-02-06 03:40 Razer.Lu 阅读(535) 评论(0) 推荐(0)
摘要:Implementint sqrt(int x).Compute and return the square root ofx.public class Solution { public int sqrt(int x) { int start = 0; int end = x; if(x == 0 || x == 1) return x; while(start x/m){ end = m -1; }else{ s... 阅读全文
posted @ 2014-01-28 16:30 Razer.Lu 阅读(112) 评论(0) 推荐(0)
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].public cl 阅读全文
posted @ 2014-01-25 05:11 Razer.Lu 阅读(153) 评论(0) 推荐(0)
摘要:Search in Rotated Sorted ArraySuppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.A[ 阅读全文
posted @ 2014-01-24 16:24 Razer.Lu 阅读(170) 评论(0) 推荐(0)