LeetCode训练营之排序

一、merge-sorted-array

Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

    public void merge(int A[], int m, int B[], int n) {
        //参数合法性校验
        if(A == null || A.length < 1 || B == null || B.length < 1)
            return;
        //两个数组的元素总个数
        int len = m + n - 1;
        //A数组元素的下标(从后向前)
        int i = m - 1;
        //B数组元素的下标(从后向前)
        int j = n - 1;
        //循环向前比较并排序
        while(i >= 0 && j >= 0){
            A[len--] = A[i] >= B[j] ? A[i--] : B[j--];
        }
        //A数组中元素有剩余
        while(i >= 0){
            A[len--] = A[i--];
        }
        //B数组中元素有剩余
        while(j >= 0){
            A[len--] = B[j--];
        }
    }

二、 merge-two-sorted-lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        //边界值检测
        if(l1 == null)
            return l2;
        if(l2 == null)
            return l1;
        //返回链表的头结点
        ListNode head = null;
        //中间节点用于连接下一节点
        ListNode temp = null;
        //依次向后比较节点
        while(l1 != null && l2 != null){
            if(head == null){
                if(l1.val <= l2.val){
                    head = l1;
                    l1 = l1.next;
                }else{
                    head = l2;
                    l2 = l2.next;
                }
                temp = head;
            }else{
                if(l1.val <= l2.val){
                    temp.next = l1;
                    temp = temp.next;
                    l1 = l1.next;
                }else{
                    temp.next = l2;
                    temp = temp.next;
                    l2 = l2.next;
                }
            }
        }
        //链接剩余节点
        temp.next = l1 != null ? l1 : l2;
        return head;
    }

三、 merge-k-sorted-lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

方法一:使用归并排序

    //使用归并排序
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        //边界值检测
        if(lists == null || lists.size() == 0)
            return null;
        //调用归并方法
        return mergeCore(lists,0,lists.size() - 1);
    }
    //归并处理K个链表的合并
    private ListNode mergeCore(ArrayList<ListNode> list,int begin,int end){
        if(begin == end)
            return list.get(begin);
        if(begin > end)
            return null;
        int mid = (begin + end) / 2;
        ListNode left = mergeCore(list,begin,mid);
        ListNode right = mergeCore(list,mid+1,end);
        return merge(left,right);
    }
    //合并两个链表
    private ListNode merge(ListNode l1,ListNode l2){
         //边界值检测
        if(l1 == null)
            return l2;
        if(l2 == null)
            return l1;
        //返回链表的头结点
        ListNode head = null;
        //中间节点用于连接下一节点
        ListNode temp = null;
        //依次向后比较节点
        while(l1 != null && l2 != null){
            if(head == null){
                if(l1.val <= l2.val){
                    head = l1;
                    l1 = l1.next;
                }else{
                    head = l2;
                    l2 = l2.next;
                }
                temp = head;
            }else{
                if(l1.val <= l2.val){
                    temp.next = l1;
                    temp = temp.next;
                    l1 = l1.next;
                }else{
                    temp.next = l2;
                    temp = temp.next;
                    l2 = l2.next;
                }
            }
        }
        //链接剩余节点
        temp.next = l1 != null ? l1 : l2;
        return head;
    }

方法二:使用优先队列(堆排序)


四、 insertion-sort-list

Sort a linked list using insertion sort.



五、 first-missing-positive

Given an unsorted integer array, find the first missing positive integer.

For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.

//利用数组下标与数组元素之间的关系将数组元素位置进行调整
    public int firstMissingPositive(int[] A) {
        //输入参数合法性校验
        if(A == null)
            return 0;
        //当数组中没有元素时,应该返回第一个正整数
        if(A.length < 1)
            return 1;
        //循环处理每一个数组元素,按照数组元素与下标的对应关系进行调整
        for(int i = 0 ; i < A.length ; i++){
            //一定要使用循环保证当前位置的元素为负数,或者已经与当前下标正确对应
            while(A[i] > 0 && A[i] != i+1){
                if(A[i]-1 >= A.length){
                    break;
                }
                if(A[i] == A[A[i]-1])
                    break;
                int temp = A[i];
                A[i] = A[A[i]-1];
                A[temp-1] = temp;
            }
        }
        //循环判断每一个元素是否与当前数组下标对应
        for(int i = 0 ; i < A.length ; i++){
            if(A[i] != i+1)
                return i+1;
        }
        //如果每一个数组元素都与下标对应关系正确,那么就返回最后一个数组元素的下一个整数
        return A.length+1;
    }

sort-colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

    public void sortColors(int[] A) {
        if(A == null || A.length <= 1)
            return;
        int zeroIndex = 0;
        int twoIndex = A.length - 1;
        for(int i = 0 ; i < A.length ; i++){
            while(i < twoIndex && A[i] == 2){
                swap(A,i,twoIndex);
                twoIndex--;
            }
            while(i > zeroIndex && A[i] == 0){
                swap(A,i,zeroIndex);
                zeroIndex++;
            }
            if(i == twoIndex)
                return;
        }
    }
    private void swap(int[] A,int i,int j){
        A[i] = A[i] + A[j];
        A[j] = A[i] - A[j];
        A[i] = A[i] - A[j];
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值