力扣---2020.3.20

面试题40. 最小的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. 合并两个排序的链表

//迭代
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. 二进制中1的个数

public class Solution {
    public int hammingWeight(int n) {
        return Integer.bitCount(n);
    }
}
  • 把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。
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;
    }
}

你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步

你可能感兴趣的:(数据结构与算法)