leetcode 刷题笔记四

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

    public List<List<Integer>> permute(int[] nums){
       List<List<Integer>> result =new ArrayList<List<Integer>>();
       List<Integer> list = new ArrayList<Integer>();
       helper(result,list,nums);
       return result;
    }
    
    public void helper(List<List<Integer>> result ,List<Integer> list,int [] nums){
       if(list.size() == nums.length) {
           if(!result.contains(list)) {
               result.add(new ArrayList<Integer>(list));
               return;
           }
       }
       for(int i=0; i<nums.length;i++) {
           if(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<Integer> queue =new PriorityQueue<Integer>(11,new Comparator<Integer>(){
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
               if(queue.size()<k){
                   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.val<tempLeft.val){
                result.next=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.val<cur.val){
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next =cur;
            cur =temp;
    
        }
        return result.next;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值