3 - Two Pointers Algorithm

Posted jenna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3 - Two Pointers Algorithm相关的知识,希望对你有一定的参考价值。

382. Triangle Count

https://www.lintcode.com/problem/triangle-count/description

  public int triangleCount(int[] S) {
        // write your code here
        //int left = 0, right = S.length - 1;
        if(S == null || S.length == 0) {
            return 0;
        }
        int count = 0;
        Arrays.sort(S);
        for(int i = 0; i < S.length; i++) {
            int left = 0, right = i - 1;
            while(left < right) {
                if(S[left] + S[right] > S[i]) {
                    count += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        return count;
    }

 

148. Sort Colors

https://www.lintcode.com/problem/sort-colors/description?_from=ladder&&fromId=1

    public void sortColors(int[] nums) {
        // write your code here
        if(nums == null || nums.length == 0) {
            return;
        }
        int len = nums.length;
        int pl = 0, pr = len - 1;
        int i = 0;
        while(i <= pr) {
            if(nums[i] == 0) {
                swap(nums, i, pl);
                pl++; //之前的元素都是0
                i++;
            } else if(nums[i] == 1) {
                i++;
            } else {
                swap(nums, i, pr);
                pr--; // 之前的元素都是1
            }
        }
        return;
    }
    
    public void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

 

59. 3Sum Closest

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

    public int threeSumClosest(int[] numbers, int target) {
        // write your code here
        if(numbers == null || numbers.length == 0) {
            return -1;
        }
        int len = numbers.length;
        Arrays.sort(numbers);
        int bestSum = numbers[0] + numbers[1] + numbers[2];
        for(int i = 2; i < len; i++) {
            int left = 0, right = i - 1;
            while(left < right) {
                int sum = numbers[i] + numbers[left] + numbers[right];
                if(Math.abs(target - sum) < Math.abs(target - bestSum)) {
                    bestSum = sum;
                }
                if(sum > target) {
                    right--;
                } else {
                    left++;
                }
            }
        }
        return bestSum;
    }

 

894. Pancake Sorting

https://www.lintcode.com/problem/pancake-sorting/description?_from=ladder&&fromId=1

    public void pancakeSort(int[] array) {
        // Write your code here
        if(array == null || array.length == 0) {
            return;
        }
        int len = array.length;
        for(int i = len - 1; i > 0; i--) {
            int Max = 0;
            for(int j = 1; j <= i; j++) {
                if(array[j] > array[Max]) {
                    Max = j;
                }
            }
            if(Max != 0 || Max != i) {
                FlipTool.flip(array, Max);
                FlipTool.flip(array, i);
            } else if(Max == 0) {
                FlipTool.flip(array, i);
            }
        }
    }

 

以上是关于3 - Two Pointers Algorithm的主要内容,如果未能解决你的问题,请参考以下文章

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

3 - Two Pointers Algorithm

sorting, two pointers(cf div.3 1113)

[LeetCode-Java] Two Pointers (1-300)