Search Insert position( 二分查找)

public int searchInsert(int[] A, int target) {
			if (A == null || A.length == 0) {
				return -1;
			}
			int start = -1, end = A.length;
			while (start + 1 < end) {
			int mid = start + (end - start) / 2;
			if (A[mid] == target) {
				return mid; // no duplicates
			} else if (A[mid] < target) {
				start = mid;
			} else {
				end = mid;
			}
			}
			return start + 1;
			}
		}


猜你喜欢

转载自blog.csdn.net/zhangbaoanhadoop/article/details/80036816
今日推荐