leetcode 刷题笔记四

leetcode 46. Permutations

  1. 题目意思:给出一个数组,获得它所有的全排列
  2. 思路:递归,用一个数组存当前已经放入的数字,遍历数组,如果当前的数字不在list中,则加入,并递归调用该函数,然后将该数字删除,放入其它数字,如果长度跟已有的数组相等,则加入

    public List<List> permute(int[] nums){
       List<List> result =new ArrayList<List>();
       List list = new ArrayList();
       helper(result,list,nums);
       return result;
    }
    
    public void helper(List<List> result ,List list,int [] nums){
       if(list.size() == nums.length) {
           if(!result.contains(list)) {
               result.add(new ArrayList(list));
               return;
           }
       }
       for(int i=0; iif(list.contains(nums[i])){
               continue;
           }
           list.add(nums[i]);
           helper(result,list, nums);
           list.remove(list.size()-1);
       }
    }

leetcode 378. Kth Smallest Element in a Sorted Matrix

  1. 题目意思:给出二维数组,每行按递减顺序排序,求第k小的数
  2. 思路:用最大堆依次遍历每个数,如果堆的大小小于k,则加入,如果大于k,则看堆顶元素是否小于当前元素,如果大于,则继续下一个,如果小于,则把该元素加入,并删除堆顶元素

    public int kthSmallest(int[][] matrix, int k) {
        PriorityQueue queue =new PriorityQueue(11,new Comparator(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i=0;ifor(int j=0;jif(queue.size()queue.add(matrix[i][j]);
               }else{
                   if(queue.peek()<=matrix[i][j]){
                       continue;
                   }else{
                       queue.add(matrix[i][j]);
                       queue.poll();
                   }
               }
            }
        }
        return queue.peek();
    }
  3. 思路2:用二分查找方法

148. Sort List

  1. 思路:单链表的排序,时间复杂度是nlogn,则可以用归并排序或者快速排序
  2. 归并排序的算法:注意在找到中点后,要把中点后面的切断

    public ListNode sortList(ListNode head) {
        if(head==null || head.next==null) return head;
    
        ListNode fast =head,slow=head,cur=null;
        while(fast!=null&&fast.next!=null){
            cur =slow;
            fast= fast.next.next;
            slow= slow.next;
        }
         cur.next =null;
        ListNode left=sortList(head);
        ListNode right=sortList(slow);
    
    
        return merge(right,left);
    }
    
    public ListNode merge(ListNode right, ListNode left){
        ListNode result=new ListNode(0), tempRight=right,tempLeft=left;
        ListNode cur = result;
        if (right==null && left==null) return null;
        while(tempRight!=null&&tempLeft!=null){
            if(tempRight.valnext=tempRight;
                tempRight= tempRight.next;
            }else{
                result.next=tempLeft;
                tempLeft= tempLeft.next;
            }
            result = result.next;
        }
        if(tempRight!=null) result.next=tempRight;
        if(tempLeft!=null) result.next=tempLeft;
        return cur.next;
    
    }

leetcode 147 insertion-sort-list

  1. 思路:插入排序
  2. 新建一个节点,值为整数的最小值,

    public ListNode insertionSortList(ListNode head){
        ListNode result = new ListNode(Integer.MIN_VALUE);
        ListNode cur = head;
        ListNode pre =result;
        while(cur!=null){
            ListNode temp = cur.next;
            pre = result;
            while(pre.next!=null&&pre.next.valnext;
            }
            cur.next = pre.next;
            pre.next =cur;
            cur =temp;
    
        }
        return result.next;
    }

你可能感兴趣的:(编程总结,算法课程笔记汇总,leetcode,链表排序)