Sort

作者: MisAutumn | 来源:发表于2018-07-20 15:44 被阅读16次
  1. Sort Colors
class Solution {
    //inplace two pass (2n)
    public void sortColors(int[] nums) {
        if (nums==null||nums.length<2) return;
        int red = 0, white = 0, blue = 0;
        for (int i : nums) {
            if (i==0) red++;
            else if (i==1) white++;
            else blue++;
        }
        for (int i = 0; i<nums.length; i++) {
            if (red>0) {
                nums[i]=0;
                red--;
            }else if (white>0) {
                nums[i]=1;
                white--;
            }else {
                nums[i]=2;
                blue--;
            }
        }
    }
}

improve:

class Solution {
    public void sortColors(int[] nums) {
        int len = nums.length;
        if (nums==null||len<2) return;
        int left=0, right=len-1, index=0;
        while (index<=right) {
            if (nums[index]==2) swap(nums, index, right--);
            else if (nums[index]==0) swap(nums, left++, index++);
            else if (nums[index]==1) index++;
        }
    }
    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}
  1. Wiggle Sort
class Solution {
    //nlogn + n
    public void wiggleSort(int[] nums) {
        int len=nums.length;
        if(nums==null||len<2) return;
        Arrays.sort(nums);
        for (int i =2; i<len; i+=2){
            swap(nums, i, i-1);
        }
    }
    private void swap(int[] nums, int p1, int p2) {
        int temp=nums[p1];
        nums[p1]=nums[p2];
        nums[p2]=temp;
    }
}
class Solution {
    public void wiggleSort(int[] nums) {
        int len=nums.length;
        if(nums==null||len<2) return;
        for (int i=1; i<len; i++){
            if(i%2==1 && nums[i]<nums[i-1] || i%2==0 && nums[i]>nums[i-1])
                swap(nums, i, i-1);
        }
    }
    private void swap(int[] nums, int p1, int p2) {
        int temp=nums[p1];
        nums[p1]=nums[p2];
        nums[p2]=temp;
    }
}
  1. Merge Intervals
class Solution {
    public boolean canAttendMeetings(Interval[] intervals) {
        if (intervals == null || intervals.length < 2) return true;
        Comparator<Interval> comparator = new Comparator<Interval>(){
            public int compare(Interval a, Interval b) {
                return a.start - b.start;
            }
        };
        Arrays.sort(intervals, comparator);
        // 不知道为什么,比直接写Arrays.sort(intervals, (a, b) -> a.start - b.start); 要快
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i].start < intervals[i-1].end) return false;
        }
        return true;
    }
}

相关文章

  • Algorithms

    BinarySearch Sort Selection sort Insertion sort

  • 笔记

    分页查询排序 Sort sort = new Sort(Sort.Direction.DESC, "id");Pa...

  • sort

    bubble_sort: select_sort: insert_sort: merge_sort: quick_...

  • Sort of sort

    排序算法 定义 对一序列对象根据某个关键字进行排序 评判标准 稳定:如果a原本在b前面,而a=b,排序之后a仍然在...

  • sorting algorithoms

    Bubble Sort Selection Sort Insertion Sort search : O(n) o...

  • 二维数组排序

    $sort = array( 'direction' => 'SORT_ASC', //排序顺序标志 SORT...

  • python中sort与sorted的区别

    1 sort sort是python中列表的方法 1.1 sort() 方法语法 list.sort(key=No...

  • Leetcode 215. Kth Largest Elemen

    Approach 1: sort sort the array using merge sort (n log n...

  • algorithm库介绍之---- stable_sort()方

    关于stable_sort()和sort()的区别: 你发现有sort和stable_sort,还有 partit...

  • insertion sort

    insertion sort用来sort基本sort好的序列,时间是O(n)

网友评论

      本文标题:Sort

      本文链接:https://www.haomeiwen.com/subject/vbropftx.html