其他分享
首页 > 其他分享> > LeetCode 0075 Sort Colors

LeetCode 0075 Sort Colors

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
类似快排思想,把所有的0放到数组头部,把所有2放到数组尾部,这样1全部留在中间。

2、代码实现

package Q0099.Q0075SortColors;

public class Solution {
    /*
       The idea is to sweep all 0s to the left and all 2s to the right, then all 1s are left in the middle.
       It is hard to define what is a "one-pass" solution but this algorithm is bounded by O(2n),
       meaning that at most each element will be seen and operated twice (in the case of all 0s).
       You may be able to write an algorithm which goes through the list only once,
       but each step requires multiple operations, leading the total operations larger than O(2n).
       类似快排思想
     */
    public void sortColors(int[] nums) {
        if (nums == null || nums.length < 2) return;
        int low = 0, high = nums.length - 1;
        int i = 0;
        while (i <= high) {
            if (nums[i] == 0) {
                exchange(nums, i, low);
                i++;
                low++;
            } else if (nums[i] == 2) {
                exchange(nums, i, high);
                high--;
            } else {
                i++;
            }
        }
    }

    private void exchange(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)

标签:Sort,operations,nums,int,复杂度,length,快排,Colors,0075
来源: https://www.cnblogs.com/junstat/p/16188582.html