Java elementary brushing questions training

 here I come~

content

1. Determine whether the characters are rearranged for each other

2. Search for the insertion position

3. The first wrong version


1. Determine whether the characters are rearranged for each other

Check icon-default.png?t=M1L8https://leetcode-cn.com/problems/check-permutation-lcci/

Topic requirements and examples:

 

Problem solving ideas:

① Put them into an array, and use the toCharArray() method to convert the string into a character array. (Note: If the lengths of the arrays are different, it is obviously not in line with the meaning of the question)

② Sort the two arrays using the directly sortable method, and use the Array.sort() method to sort

③ Traverse the array to determine whether each corresponds to the corresponding data

code show as below:

class Solution {
    public boolean CheckPermutation(String s1, String s2) {
 boolean flag =true;
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        if(c1.length!=c2.length) {
            return false;
        }
Arrays.sort(c1);
Arrays.sort(c2);
for(int i=0;i<c1.length;i++){
    if(c1[i]!=c2[i]){
        return false;
    }
}return true;
}
}

2. Search for the insertion position

Leetcode icon-default.png?t=M1L8https://leetcode-cn.com/problems/search-insert-position/

Topic requirements and examples:

 

Problem-solving ideas: (The dichotomy method is used, because the dichotomy method can save half the time compared to directly seeking)

①The question is in two cases: a. The number exists in the array (then directly use the subscript of the number larger than it as the subscript to return)

                                b. The number does not exist in the array (then the length of the array is directly returned at the end, because it must be inserted at the end of the array)

②Set left=0 respectively; right=nums.length-1; use the dichotomy mid to solve

code show as below:

class Solution {
    public int searchInsert(int[] nums, int target) {
int n = nums.length;
        int left=0,right=n-1;
        while(left<=right){
            int mid=(right+left)/2;
            if(nums[mid]<target)
                left=mid+1;
            else right=mid-1;
        }
        return left;
    }
}

3. The first wrong version

Leetcode icon-default.png?t=M1L8https://leetcode-cn.com/problems/first-bad-version/

Topic requirements and examples:

 This question is essentially a search for data

Problem-solving ideas: (using the idea of ​​dichotomy to search for data)

①Set left=0 respectively; right=nums.length-1; use the dichotomy mid to solve

Notice! ! !

(left+right)/2 cannot be used here, because left and right are both int, and the initial value of both values ​​exceeds half of the limited size of int, then left+right will overflow, so left+(right- left)/2 to prevent overflow when calculating the median.

code show as below:

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        int left=0;
        int right=n;
        while(left<=right){
            int mid=left+(right-left)/2;
            if(isBadVersion(mid)){
                 right=mid-1;
            }else{
                left=mid+1;
            }
        }return left;
    }
}

Thanks for watching~

 

Guess you like

Origin blog.csdn.net/weixin_58850105/article/details/123137495
Recommended