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

3 - Two Pointers Algorithm

时间:2019-05-19 10:10:21      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:number   turn   bool   https   linked   -o   result   algorithm   boolean   

380. Intersection of Two Linked Lists

https://www.lintcode.com/problem/intersection-of-two-linked-lists/description?_from=ladder&&fromId=1

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // write your code here
        if(headA == null || headB == null) {
            return null;
        }
        ListNode a = headA;
        ListNode b = headB;
        while(a != b) {
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }

 

102. Linked List Cycle

https://www.lintcode.com/problem/linked-list-cycle/description?_from=ladder&&fromId=1

    public boolean hasCycle(ListNode head) {
        // write your code here
        if(head == null || head.next == null) {
            return false;
        }
        ListNode slow = head, fast = head.next;
        while(slow != fast) {
            if(fast == null || fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

 

58. 4Sum

https://www.lintcode.com/problem/4sum/description?_from=ladder&&fromId=1

    public List<List<Integer>> fourSum(int[] numbers, int target) {
        // write your code here
        List<List<Integer>> result = new ArrayList<>();
        if(numbers == null) {
            return result;
        }
        Arrays.sort(numbers);
        for(int i = 0; i < numbers.length - 3; i++) {
            if(i != 0 && numbers[i] == numbers[i - 1]) {
                continue;
            } 
            for(int j = i + 1; j < numbers.length - 2; j++) {
                if(j != i + 1 && numbers[j] == numbers[j - 1]) {
                    continue;
                }
                int left = j + 1, right = numbers.length - 1;
                while(left < right) {
                    if(numbers[i] + numbers[j] + numbers[left] + numbers[right] < target) {
                        left++;
                    } else if(numbers[i] + numbers[j] + numbers[left] + numbers[right] > target) {
                        right--;
                    } else {
                        List<Integer> curr = new ArrayList<>();
                        curr.add(numbers[i]);
                        curr.add(numbers[j]);
                        curr.add(numbers[left]);
                        curr.add(numbers[right]);
                        result.add(new ArrayList<>(curr));
                        left++;
                        right--;
                        while(numbers[left] == numbers[left - 1]) {
                            left++;
                        }
                        while(numbers[right] == numbers[right + 1]) {
                            right--;
                        }
                    }
                }
            }
        }
        return result;
    }

 

3 - Two Pointers Algorithm

标签:number   turn   bool   https   linked   -o   result   algorithm   boolean   

原文地址:https://www.cnblogs.com/jenna/p/10887872.html

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