publicintsqrt(int x){ if (x <= 0) return0; long v = x; while(v * v > x) v = (v + (x / v)) >> 1; return (int)v; }
习题地址 Sqrt(x)
Search Insert Position - easy
题目
1 2 3 4 5 6 7 8 9 10 11 12 13
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.
Example [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 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Java:
public int searchInsert(int[] A, int target) { if (A == null && A.length < 1) return0; int low = 0, high = A.length - 1; int mid = 0; while (low <= high) { mid = low + (high - low) / 2; if (A[mid] == target) returnmid; elseif (A[mid] < target) low = mid + 1; else high = mid - 1; } return high - 1; }
习题地址 Search Insert Position
Search a 2D Matrix - easy
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Write 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 firstinteger of each rowis greater than the lastinteger of the previous row.
public boolean searchMatrix(int[][] matrix, inttarget) { if (matrix == null || matrix.length < 1) return false; introw = matrix.length; int column = matrix[0].length - 1; for (int i = 0; i < row; i++) { if (matrix[i][column] == target) return true; elseif (matrix[i][column] < target) continue; elsereturn binarySearch(matrix[i], target); } return false; }
public boolean binarySearch(int[] arr, inttarget) { if (arr == null || arr.length < 1) return false; int low = 0, mid = 0, high = arr.length - 1; while (low <= high) { mid = low + (high - low) / 2; if (arr[mid] == target) return true; elseif (arr[mid] < target) low = mid + 1; else high = mid - 1; } return false; }
习题地址 Search a 2D Matrix
First Position of Target - easy
题目
1 2 3 4 5 6 7
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
Example If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Java:
public int binarySearch(int[] nums, int target) { if (nums == null && nums.length == 0) return-1; int low = 0, mid = 0, high = nums.length - 1; while (low <= high) { mid = low + (high - low) / 2; if (nums[mid] >= target) high = mid - 1; else low = mid + 1; } if (nums[high - 1] == target) return high - 1; elsereturn-1; }
习题地址 First Position of Target
Wood Cut - medium
题目
1 2 3 4 5 6 7
Given n pieces of wood withlength L[i] (integer array). Cut them into small pieces to guarantee you could have equalor more than k pieces withthe same length. What isthe longest length you can getfromthe n pieces of wood? Given L & k, returnthe maximum lengthofthe small pieces.
The code base version is an integer start from1to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version.
You can call isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code's annotation part.
Notice
Please read the annotation in code area toget the correct way tocall isBadVersion in different language. For example, Java is SVNRepo.isBadVersion(v)
Example Given n = 5:
isBadVersion(3) -> false isBadVersion(5) -> true isBadVersion(4) -> true Here we are 100% sure that the 4th version is the first bad version.
代码
1 2 3 4 5 6 7 8 9 10 11 12 13
Java:
public int findFirstBadVersion(intn) { if (n < 1) return 0; int low = 0, mid = 0, high = n; while (low < high) { mid = low + (high - low) / 2; if (SVNRepo.isBadVersion(mid)) high = mid; else low = mid + 1; } return low; }
习题地址 First Bad Version
Search in Rotated Sorted Array - medium
题目
1 2 3 4 5 6 7 8 9 10 11 12 13
Suppose a sorted array 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 value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Example For [4, 5, 1, 2, 3] and target=1, return 2.