码迷,mamicode.com
首页 > 其他好文 > 详细

2019/08/25 leetcode

时间:2019-08-26 11:33:38      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:blog   lis   tac   poi   https   --   ack   4sum   csdn   

2019/08/25: L230, L226, L18, L234, L23

1. L230 Kth smallest element in BST

借助 Stack 或 LinkedList inorder traveresal, LinkedList会更快

ArrayList, LinkedList, Vector, Stack  https://blog.csdn.net/iteye_11495/article/details/82618748 

 

2. L226 Invert Binary Tree

3. L18 4Sum

   Two pointers

 1 public List<List<Integer>> fourSum(int[] nums, int target) {
 2         
 3         List<List<Integer>> res = new LinkedList<>();
 4         if(nums == null || nums.length < 4) return res;
 5         Arrays.sort(nums);
 6         int k, z;
 7         for(int i = 0; i<nums.length-1;i++){
 8             if(i>0 && nums[i]==nums[i-1]) continue;
 9             
10             //pruning
11             if(nums[i]*4 > target) break;
12             if(nums[i] + 3* nums[nums.length-1]<target) continue;
13             
14             for(int j =i+1; j<nums.length;j++){
15                 if(j>i+1 && nums[j]==nums[j-1]) continue;
16                 
17                 //pruning
18                 if(nums[i]+ nums[j]*3 >target) break;
19                 if(nums[i] + nums[j]+ 2* nums[nums.length-1]<target) continue;
20                 
21                 k = j+1;
22                 z = nums.length -1;
23                 while(k<z){
24                     int sum = nums[i] + nums[j] + nums[k] + nums[z];
25                     if(sum == target){
26                         List<Integer> list = new ArrayList<>();
27                         list.add(nums[i]);
28                         list.add(nums[j]);
29                         list.add(nums[k]);
30                         list.add(nums[z]);
31                         res.add(list);
32                         while(k<z && nums[k]==nums[k+1])k++;
33                         k++;
34                         while(k<z && nums[z]==nums[z-1])z--;
35                         z--;
36                     }else if(sum > target) z--;
37                     else k++;
38                 }
39             }
40         }  
41         return res;
42     }

加上pruning代码,beat 100%。

 

4. L234 Palindrome Linked List

slow fast pointers, 后半段reverse

Time O(n) spaceO(1)

 public boolean isPalindrome(ListNode head) {
        if( head==null || head.next==null ) return true;
        
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast!=null && fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode pre = null;
        slow = slow.next;
        //reveres node after slow
        while(slow!=null){
            ListNode next = slow.next;
            slow.next = pre;
            pre = slow;
            slow = next;
        }
        
        while(pre!=null && head!=null){
            if(head.val!=pre.val) return false;
            head = head.next;
            pre = pre.next;
        }
        return true;
    }

 

5. L23. Merge k Sorted Lists

min heap

public ListNode mergeKLists(ListNode[] lists) {
        if(lists == null || lists.length == 0) return null;
        if(lists.length ==1) return lists[0];
        PriorityQueue<ListNode> pq = new PriorityQueue<ListNode>(new Comparator<ListNode>(){
            public int compare(ListNode l1, ListNode l2){
                return l1.val - l2.val;
            }
        });
        for(ListNode l : lists){
            if(l!=null) pq.offer(l);
        }
        ListNode head = new ListNode(0);
        ListNode curr = head;
        while(!pq.isEmpty()){
            ListNode newNode = pq.poll();
            curr.next = newNode;
            curr = curr.next;
            if(newNode.next!=null){
                pq.offer(newNode.next);
            }
        }
        return head.next;
    }

 

2019/08/25 leetcode

标签:blog   lis   tac   poi   https   --   ack   4sum   csdn   

原文地址:https://www.cnblogs.com/rySZ/p/11411472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!