随笔分类 -  leetcode-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 1 public class So 阅读全文
posted @ 2014-02-08 04:48 krunning
摘要: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.[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, ... 阅读全文
posted @ 2014-02-08 04:32 krunning
摘要:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array. 1 public class Solution { 2 //rotated array all is A[start]){10 if(target>=A[start] ... 阅读全文
posted @ 2014-02-06 14:40 krunning
摘要: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 ord... 阅读全文
posted @ 2014-02-06 14:10 krunning
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand. 1 public class Solution { 2 //all is equal in sorted array 3 public int search(int[] A, int target) { 4 if(A.length=A[start]){//don't forget the equal10 if(target=A[start]){11 ... 阅读全文
posted @ 2014-02-06 14:09 krunning