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

3 - Two Pointers Algorithm

时间:2019-05-13 12:37:59      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:numbers   public   temp   void   count   nbsp   style   array   flip   

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

标签:numbers   public   temp   void   count   nbsp   style   array   flip   

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

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