Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
1, Finds the subscript of the specified element
Given an n-element ordered (ascending) integer array nums and a target value target, write a function to search the target in nums. If the target value exists, return the subscript, otherwise return - 1.
Source: LeetCode
Title Link
Tips:
- You can assume that all elements in nums are non repeatable
- n will be between [110000]
- Each element of num will be between [- 99999999]
//non-recursive public static int select2(int[] nums,int target){ int left = 0; int right = nums.length-1; while (left<=right){ int mid = (left+right)/2; if (target<nums[mid]){ right = mid-1; }else if (target>nums[mid]){ left = mid+1; }else { return mid; } } return -1; }
2, Search insertion location
Given a sort array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, returns the position where it will be inserted in order.
You must use an algorithm with a time complexity of O(log n).
Source: LeetCode
Title Link
Use dichotomy
public static int searchInsert(int[] nums, int target) { int left = 0; int right = nums.length-1; if (target>nums[nums.length-1]){ return nums.length; } while (left<=right){ int mid = (left+right)/2; if (target<nums[mid]){ right = mid-1; }else if (target>nums[mid]){ left = mid+1; }else { return mid; } } return left; }
More simply, there is violent cracking
brute force
//Non dichotomy, violence public static int searchInsert2(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { if (nums[i]>=target){ return i; } } return nums.length; }
3, Finds the first and last elements in a sorted array
Given an integer array nums arranged in ascending order and a target value target. Find the start and end positions of the given target value in the array.
If the target value target does not exist in the array, return [- 1, - 1].
Advanced:
Can you design and implement an algorithm with time complexity of O(log n) to solve this problem?
Source: LeetCode
Title Link
brute force
private static int[] searchRange(int[] nums, int target){ int[] test = new int[2]; test[0]=-1; test[1]=-1; for (int i = 0; i < nums.length; i++) { if (nums[i]==target){ //Left boundary test[0] = i; test[1] = i; int j=i; while (j!=nums.length-1&&nums[j+1]==nums[j]){ test[1] = j + 1; j++; } break; } } return test; }
Dichotomy
public static int[] searchRange3(int[] nums, int target){ int index = erfen(nums,target); int left = index; int right = index; if (index!=-1){ //towards the left while (left-1>=0&&nums[left-1]==nums[left]){ left--; } while (right+1<nums.length&&nums[right+1]==nums[right]){ right++; } } return new int[]{left,right}; } private static int erfen(int[] nums, int target) { int l = 0; int r = nums.length-1; while (l<=r){ int mid = (l+r)/2; if (target<nums[mid]){ r = mid-1; }else if (target>nums[mid]){ l = mid+1; }else { return mid; } } return -1; }
4, Remove element
Give you an array num and a value val. you need to remove all elements with a value equal to Val in place and return the new length of the removed array.
Instead of using extra array space, you must use only O(1) extra space and modify the input array in place.
The order of elements can be changed. You don't need to consider the elements in the array that exceed the new length.
Source: LeetCode
Title Link
brute force
public static int removeElement(int[] nums, int val) { int length = nums.length; for (int i = 0; i < length; i++) { if (val==nums[i]){ for (int j=i;j<length-1;j++){ nums[j] = nums[j+1]; } length--; i--; } } return length; }
Double pointer algorithm
public static int removeElement2(int[] nums, int val){ int top = 0; int bot = 0; while (top<nums.length){ if (nums[top]!=val){ nums[bot] = nums[top]; bot++; } top++; } return bot; }
5, Square of ordered array
Give you an integer array nums sorted in non decreasing order, and return a new array composed of the square of each number. It is also required to sort in non decreasing order.
Source: LeetCode
Title Link
Double pointer
private static int[] method1(int[] arr) { int[] temp = new int[arr.length]; int index = temp.length-1; int p1 = 0; int p2 = arr.length-1; while (p1<=p2){ if (arr[p1]*arr[p1]<arr[p2]*arr[p2]){ temp[index--] = arr[p2]*arr[p2]; p2--; }else{ temp[index--] = arr[p1]*arr[p1]; p1++; } } return temp; }
6, Minimum length subarray
Given an array containing n positive integers and a positive integer target.
Find out the continuous sub array [numsl, numsl+1,..., numsr-1, numsr] with the smallest length satisfying its sum ≥ target in the array, and return its length. If there is no eligible subarray, 0 is returned.
Source: LeetCode
Title Description
brute force
private static int check(int[] arr, int target) { int result = Integer.MAX_VALUE; int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = i; j < arr.length; j++) { sum+=arr[j]; if(sum>=target){ //Statistical length int length = j-i+1; result = result<length?result:length; } } sum=0; } return result<Integer.MAX_VALUE?result:0; }
Sliding window algorithm
public static int check2(int[] arr, int target){ int left = 0; int sum= 0; int result = Integer.MAX_VALUE; for (int right=0;right<arr.length;right++){ sum+=right; while (sum>=target){ //Window shrinkage result = result<right-left+1?result:right-left+1; sum-=arr[left++]; } } return result<Integer.MAX_VALUE?result:0; }
7, Spiral matrix (high frequency)
Give you a positive integer n to generate an n x n square matrix containing all elements from 1 to n2 and the elements are spirally arranged in clockwise order.
Buckle link:
Title Link
public static int[][] generateMatrix(int n) { int[][] arr = new int[n][n]; //Lower right upper left int loop = n/2;//Number of cycles / / 1 int t = 1; int Xstart = 0;//Start position of x-axis for each cycle int Ystart = 0;//Start position of y-axis for each cycle int offset = 1;//Offset int mid = n/2; while (loop>0){ int i = Xstart; int j = Ystart; //upper for (;j<Ystart+n-offset;++j){ arr[Xstart][j]=t++; } //right for (;i<Xstart+n-offset;++i){ arr[i][j]=t++; } //Left //At this time, i=3,j=3 for (;j>Ystart;j--){ arr[i][j]=t++; } //At this time, I = 3 and j = 0 for (;i>Xstart;i--){ arr[i][j]=t++; } Xstart+=1; Ystart+=1; offset+=2; loop--; } if (n%2==1){ arr[mid][mid]=t; } return arr; }