3 - Two Pointers Algorithm

Posted jenna

tags:

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

143. Sort Colors II (quick sort 变种)

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

public class Solution {
    /**
     * @param colors: A list of integer
     * @param k: An integer
     * @return: nothing
     */
    public void sortColors2(int[] colors, int k) {
        // write your code here
        quickSort(colors, 0, colors.length - 1, 1, k);
    }
    
    public void quickSort(int[] colors, int left, int right, int colorFrom, int colorTo) {
        if(left >= right) {
            return;
        }
        if(colorFrom >= colorTo) {
            return;
        }
        int l = left, r = right;
        int mid = colorFrom + (colorTo - colorFrom) / 2;
        while(l <= r) {
            while(l <= r && colors[l] <= mid) {
                l++;
            }
            while(l <= r && colors[r] > mid) {
                r--;
            }
            if(l <= r) {
                int temp = colors[l];
                colors[l] = colors[r];
                colors[r] = temp;
                l++;
                r--;
            }
        }
        quickSort(colors, left, r, colorFrom, mid);
        quickSort(colors, l, right, mid + 1, colorTo);
    }
}

57. 3Sum

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

public class Solution {
    /**
     * @param numbers: Give an array numbers of n integer
     * @return: Find all unique triplets in the array which gives the sum of zero.
     */
    public List<List<Integer>> threeSum(int[] numbers) {
        // write your code here
        List<List<Integer>> result = new LinkedList<>();
        if(numbers == null || numbers.length < 3) return result;
        Arrays.sort(numbers);
        int len = numbers.length - 1;
        int i = 0;
        while(i <= len - 2) {
            while(i >= 1 && i < len && numbers[i] == numbers[i - 1]) {
                i++;
            } 
            int sum = 0 - numbers[i];
            int left = i + 1;
            int right = len;
            while(left + 1 <= right) {
                if(numbers[left] + numbers[right] == sum) {
                    result.add(Arrays.asList(numbers[i], numbers[left], numbers[right]));
                    left++;
                    right--;
                    while(left + 1 <= right && numbers[left] == numbers[left - 1]) {
                        left++;
                    }
                    while(left + 1 <= right && numbers[right] == numbers[right + 1]) {
                        right--;
                    }
                } else if(numbers[left] + numbers[right] > sum) {
                    right--;
                } else {
                    left++;
                }
            }
            i++;
        }
        return result;
    }
}

31. Partition Array (quick sort)

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

public class Solution {
    /**
     * @param nums: The integer array you should partition
     * @param k: An integer
     * @return: The index after partition
     */
    public int partitionArray(int[] nums, int k) {
        // write your code here
        if(nums == null || nums.length == 0) return 0;
        int left = 0, right = nums.length - 1;
        while(left <= right) {
            while(left <= right && nums[left] < k) {
                left++;
            }
            while(left <= right && nums[right] >= k) {
                right--;
            }
            if(left <= right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
                left++;
                right--;
            }
        }
        return left;
    }
}

 

以上是关于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)