Stay button --- 2020.3.20

40. The minimum number of interview questions k

class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        int[] res = new int[k];
        Arrays.sort(arr);
        for(int i = 0;i<k;i++){
            res[i] = arr[i];
        }
        return res;
    }
}
class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        return Arrays.stream(arr).sorted().limit(k).toArray();
    }
}
class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        Arrays.sort(arr);
        return Arrays.copyOfRange(arr, 0, k);
    }
}
class Solution {
    public int[] getLeastNumbers(int[] arr, int k) {
        int[] res = new int[k];
        quickSort(arr,0,arr.length-1);
        for(int i = 0;i<k;i++){
            res[i] = arr[i];
        }
        return res;
    }

    public static void quickSort(int arr[],int left,int right){
        int l = left;  //左下标
        int r = right;  // 右下标
        int pivot = arr[(left+right)/2];  //中间值
        int temp = 0;

        while (l<r){
            while (arr[l] < pivot){
                l +=1;
            }

            while (arr[r] > pivot){
                r -=1;
            }

            if (l >= r){
                break;
            }

            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;

            if (arr[l] == pivot){
                r -=1;
            }
            if (arr[r] == pivot){
                l +=1;
            }
        }

        if (l == r){
            l +=1;
            r -=1;
        }
        if (left<r){
            quickSort(arr,left,r);
        }
        if (right>l){
            quickSort(arr,l,right);
        }
    }
}

25. merging two sorted lists interview questions

//迭代
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);
        ListNode node = dummyHead;
        while(l1 != null && l2 != null){
            if(l1.val <= l2.val){
                node.next = l1;
                l1 = l1.next;
            }else{
                node.next = l2;
                l2 = l2.next;
            }
            node = node.next;
        }
        if (l1 != null) {
            node.next = l1;
        }
        if (l2 != null) {
            node.next = l2;
        }
        return dummyHead.next;
    }
}
//迭代的简化版
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dum = new ListNode(0), cur = dum;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            }
            else {
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        cur.next = l1 != null ? l1 : l2;
        return dum.next;
    }
}
//递归
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1.val <= l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }
}

15. The binary number is inscribed 1

public class Solution {
    public int hammingWeight(int n) {
        return Integer.bitCount(n);
    }
}
  • Convert an integer number minus 1, and then to do with the original integer arithmetic, the integer will rightmost 1 becomes a binary 0. So how many integer 1, on how many times such an operation can be performed.
class Solution {
    public int hammingWeight(int n) {
        int count=0;
        while(n!=0){
            count++;
            n=n&(n-1);
        }
        return count;
    }
}
class Solution {
    public int hammingWeight(int n) {
        int count=0;
        while(n!=0){
            //n&1 转换为二进制运算,当且仅当两个对应的位置都是1,结果才是1,否则结果为0
            count +=n&1;
            n=n>>>1;
        }
        return count;
    }
}

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

He published 193 original articles · won praise 116 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/105007247