69. Square root of X
Implement the int sqrt(int x) function.
Calculates and returns the square root of X, where x is a non negative integer.
Because the return type is an integer, the result only retains the integer part, and the decimal part will be rounded off.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Note: the square root of 8 is 2.82842 ,
Because the return type is an integer, the decimal part will be rounded off.
public class mySqrt { public static void main(String[] args) { System.out.println(mysqrt(8)); } public static int mysqrt(int x){ //Square root x //The sum of squares must be in the range of 0-x/2 if(x <= 1){ return x; } int left = 1; int right = x/2; while(left <= right){ int mid = left + (right - left)/2; if(mid == x/mid ){ return mid; }else if(mid > x/mid){ //Indicates that the square of mid is greater than x //The value is large right = mid - 1; }else{ left = mid + 1; } } return left-1; //Because, for example, the square sum of 8 is 2 instead of 3 //At this time, left is greater than right and out of bounds, indicating that the number taken by left is 3 } }
744.
Given an ordered array of letters containing only lowercase letters and a target letter target, look for the smallest letter in the ordered array that is larger than the target letter.
The order of the letters in the array is cyclic. For example, if the target letter target = 'z' and the ordered array is letters = ['a', 'b'], the answer returns' a '.
Example:
Input:
letters = ["c", "f", "j"]
target = "a"
Output: "c"
Input:
letters = ["c", "f", "j"]
target = "c"
Output: "f"
Input:
letters = ["c", "f", "j"]
target = "d"
Output: "f"
Input:
letters = ["c", "f", "j"]
target = "g"
Output: "j"
Input:
letters = ["c", "f", "j"]
target = "j"
Output: "c"
Input:
letters = ["c", "f", "j"]
target = "k"
Output: "c"
//Template code of dichotomy
class Solution { public char nextGreatestLetter(char[] letters, char target) { //When we see the word "ordered", we should think of binary search first //When breaking the cycle, l is at most equal to r int n = letters.length; int l = 0; int r = n;//Left and right open while(l<r){ int m = l+(r-l)/2; if(letters[m] <= target){ l = m+1; }else{ r = m; } } //If no larger than target is found after searching the array, letters [0] will be returned return l < n ? letters[l] : letters[0]; } } //As for whether l < h or l < = h, l < h can be taken as l=h, this problem is obviously not.
540. Single element of ordered array
Given an ordered array containing only integers, each element will appear twice, and only one number will appear once. Find out the number.
Example 1:
Input: [1,1,2,3,3,4,4,8,8]
Output: 2
Example 2:
Input: [3,3,7,7,10,11,11]
Output: 10
class Solution { public int singleNonDuplicate(int[] nums) { int l = 0; int r = nums.length-1; //It is not allowed to use left closing and right opening, which will lead to overstepping while(l<r){ int m = l+(r-l)/2; int n = m%2== 0 ? m+1:m-1; if(nums[m] == nums[n]){ //Not on the left l = m + 1; }else{ r = m; } } return nums[l]; //l always points to the first element that does not meet the condition. } }
278. First wrong version
You are a product manager and are currently leading a team to develop new products. Unfortunately, the latest version of your product didn't pass the quality test. Since each version is based on the previous version, all versions after the wrong version are wrong.
Suppose you have n versions [1, 2,...] , n], you want to find the first wrong version that caused all subsequent versions to go wrong.
You can call the bool isBadVersion(version) interface to determine whether the version number version is wrong in the unit test. Implement a function to find the first wrong version. You should try to minimize the number of API calls.
Example:
Given n = 5, and version = 4 is the first bad version.
Call isbadversion (3) - > false
Call isbadversion (5) - > true
Call isbadversion (4) - > true
So, 4 is the first wrong version.
/* The isBadVersion API is defined in the parent class VersionControl. boolean isBadVersion(int version); */ public class Solution extends VersionControl { public int firstBadVersion(int n) { //The previous versions are all good. After the first wrong version, the later ones are all bad //To find the first wrong version //Dichotomy int l = 0; int r = n; while(l<r){ int mid = l + (r-l)/2; if(isBadVersion(mid)== false){ l = mid + 1; }else{ r = mid; } } return l; } }
153. Find the minimum value in the rotation sort array
Let's say that the array sorted in ascending order is rotated at a pre-known point.
(for example, the array [0,1,2,4,5,6,7] may change to [4,5,6,7,0,1,2]).
Please find the smallest element.
You can assume that there are no duplicate elements in the array.
Example 1:
Input: [3,4,5,1,2]
Output: 1
Example 2:
Input: [4,5,6,7,0,1,2]
Output: 0
class Solution { public int findMin(int[] nums) { //The array is divided into two parts //The first point of the rotating part is the smallest number int l =0; int r = nums.length-1; while(l<r){ int mid = l +(r-l)/2; //The start array is ordered, such as 0, 1, 2, 4, 5, 6, 7 //Then we flip 4, 5, 6, 7, 0, 1, 2 //So the critical point 0, which was originally on the left side of the array, is now on the right side of the array //When dichotomy is used to narrow the search scope, we want to find the segmentation points on the left and right sides, //That is, the number on the left side of the array must be greater than the number on the right side of the array //As a judgment point //mid is on the left half, cut off the left half of the title if(nums[mid] > nums[r]){ l = mid+1; }else{ r = mid; } } return nums[l]; } }
34. find the first and last position of the element in the sorted array
Given an ascending array of integers, nums, and a target value, target. Find the start and end position of the given target value in the array.
Your algorithm time complexity must be O(log n) level.
Returns [- 1, - 1] if the target value does not exist in the array.
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [- 1, - 1]
class Solution { public int[] searchRange(int[] nums, int target) { //Two searches and two scores int[] res = new int[2]; res[0] = binarySearch1(nums,target); res[1] = binarySearch2(nums,target); return res; } public int binarySearch1(int[] nums, int target){ //Search the location of the first target //Find the index of the first value equal to 8 int l = 0; int n = nums.length; int r = n -1; while(l <= r){ int m = l+(r-l)/2; if(nums[m] < target){ l = m+1; }else{ r = m-1; } } return l<n && nums[l] == target? l :-1; } public int binarySearch2(int[] nums, int target){ //Search for the location of the last target //Index of the last value equal to target int l = 0; int n = nums.length; int r = n -1; while(l <= r){ int m = l+(r-l)/2; if(nums[m] <= target){ l = m+1; }else{ r = m-1; } } return r>=0 && nums[r] == target? r :-1; } }
At last, we attach the complete set of dichotomy
package yu.practiceLeetCode02.shuangzhizhen; public class BinarySort { public static void main(String[] args) { //The premise of dichotomy is an ordered array, and the arrays from small to small are used here int[] nums = {1,2,3,4,5,8}; System.out.println(binarySort1(nums,5)); //4 System.out.println(binarySort1(nums,1));//0 System.out.println(binarySort1(nums,6));//-1 System.out.println(binarySort1(nums,3));//2 System.out.println("================================="); int[] arr = {1,2,3,3,4,4,5,8}; System.out.println(binarySort2(arr,9)); //-1 System.out.println(binarySort2(arr,8));//-1 System.out.println(binarySort2(arr,3));//4 System.out.println(binarySort2(arr,2));//2 System.out.println(binarySort2(arr,0));//0 System.out.println("================================="); System.out.println(binarySort3(arr,9)); //-1 System.out.println(binarySort3(arr,8));//-1 System.out.println(binarySort3(arr,3));//2 System.out.println(binarySort3(arr,2));//1 System.out.println(binarySort3(arr,4));//4 System.out.println(binarySort3(arr,0));//0 System.out.println("================================="); System.out.println(binarySort4(arr,9)); //-1 System.out.println(binarySort4(arr,3)); //2 System.out.println("================================="); System.out.println(binarySort5(arr,9)); //-1 System.out.println(binarySort5(arr,3)); //3 System.out.println("================================="); System.out.println(binarySort6(arr,9)); //7 System.out.println(binarySort6(arr,4)); //5 System.out.println("================================="); System.out.println(binarySort7(arr,9)); //7 System.out.println(binarySort7(arr,4)); //3 System.out.println(binarySort7(arr,5)); //5 } /** * Function: find the index of a number in the array, return - 1 if not found * @param nums Array searched * @param target Number to find * @return m Or -1 * This is the original dichotomy. It follows the left closed and closed interval. The value found is the value of mid */ public static int binarySort1(int[] nums, int target){ int n = nums.length; int l = 0; int r = n-1; while(l<=r){ int m = l + (r-l)/2; if(nums[m]== target){ return m; }else if(nums[m] < target){ l = m+1; //Approaching to the left }else{ r = m-1;//Approximation to the right } } //Can't find return -1; } /** * Function: find the first number larger than target, return - 1 if not found * @param nums * @param target * @return */ public static int binarySort2(int[] nums, int target){ int n = nums.length; int l = 0; int r = n; //Here is [0,n) left closed right open interval while(l<r) { //So we don't need to consider the case of l=r, because r= m, n can't be taken. int m = l + (r - l) / 2; if (nums[m] >= target) { //I can't find it. Cut off half of it l = m + 1; } else { r = m; } } return l>=n ? -1 : l; } /** * Function: find the first number whose ratio is greater than or equal to target, return - 1 if not found * @param nums * @param target * @return */ public static int binarySort3(int[] nums, int target){ int n = nums.length; int l = 0; int r = n; //Here is [0,n) left closed right open interval while(l<r) { //So we don't need to consider the case of l=r, because r= m, n can't be taken. int m = l + (r - l) / 2; if (nums[m] < target) { //I can't find it. Cut off half of it l = m + 1; } else { r = m; } } return l>=n ? -1 : l; } /** * Function: find the index of a number in the array, return - 1 if not found * @param nums Array searched * @param target Number to find * @return m Or -1 * This is the original dichotomy. It follows the left closed and closed interval. The value found is the value of mid */ //Find the index of the first number equal to target public static int binarySort4(int[] nums, int target){ int n = nums.length; int l = 0; int r = n-1; while(l<=r){ int m = l + (r-l)/2; if(nums[m] < target){ //nums target value is large //Search left to right l= m+1; }else{ r = m-1; } } //Judge whether target exists return l<n && nums[l]==target ? l : -1; } //Find the index of the last number equal to target public static int binarySort5(int[] nums, int target){ int n = nums.length; int l = 0; int r = n-1; while(l<=r){ int m = l + (r-l)/2; if(nums[m] > target){ //Search right to left //Remove values that do not meet the conditions r = m-1; //Approaching to the left }else{ l = m + 1;//Approximation to the right } } //Judge whether target exists return r <= n && nums[r] == target ? r: -1; } //Find the last number less than or equal to target public static int binarySort6(int[] nums, int target){ int n = nums.length; int l = 0; int r = n-1; //Here is [0,n) left closed right open interval while(l<=r) { //So we don 't need to consider the case of l=r, because r= m can' t take n. int m = l + (r - l) / 2; if (nums[m]> target) { //Search right to left //Dissatisfaction removal r= m -1; } else { l = m+1; } } return r>0 ? r : -1; } //Find the last number less than target public static int binarySort7(int[] nums, int target){ int n = nums.length; int l = 0; int r = n-1; //Here is [0,n) left closed right open interval while(l<=r) { //So we don't need to consider the case of l=r, because r= m, n can't be taken. int m = l + (r - l) / 2; if (nums[m]>= target) { //Search right to left //Dissatisfaction removal r= m -1; } else { l = m+1; } } return r>0 ? r : -1; } }