52. Sort Colors && Combinations

Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note: You are not suppose to use the library‘s sort function for this problem.

click to show follow up.

Follow up: A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0‘s, 1‘s, and 2‘s, then overwrite array with total number of 0‘s, then 1‘s and followed by 2‘s.

Could you come up with an one-pass algorithm using only constant space?

思路: 1. 类似快排,走两遍(v=1, 分出0;v = 2,分出1)。

void partition(int A[], int n, int v) {
    int start = 0, end = n-1;
    while(start < end) {
        while(start < end && A[start] < v) ++start;
        while(start < end && A[end] >= v) --end;
        int tem = A[start];
        A[start++] = A[end];
        A[end--] = tem;
    }
}
class Solution {
public:
    void sortColors(int A[], int n) {
        partition(A, n, 1);
        partition(A, n, 2);
    }
};

2. 计数排序。计数与重写。

class Solution {
public:
    void sortColors(int A[], int n) {
        int count[3] = {0};
        for(int i = 0; i < n; ++i) count[A[i]]++;
        int id = 0;
        for(int i = 0; i < 3; ++i)
            for(int j = 0; j < count[i]; ++j)
                A[id++] = i;
    }
};

Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example, If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

思路:递归,每层从前往后逐步取元素。
void combination(int k, int num, int begin, int end, vector<int> & vec2, vector<vector<int> > &vec) {
    if(num == k) {
        vec.push_back(vec2);
        return;
    }
    for(int i = begin; i <= end; ++i) {
        vec2.push_back(i);
        combination(k, num+1, i+1, end, vec2, vec);
        vec2.pop_back();
    }
}

class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        vector<int> vec2;
        vector<vector<int> > vec;
        combination(k, 0, 1, n, vec2, vec);
        return vec;
    }
};
时间: 2024-08-24 02:02:06

52. Sort Colors && Combinations的相关文章

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

No.75 Sort Colors

No.75 Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color

Sort colors系列

75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第二遍开始摆放数字. 代码如下: 1 void sortColors(vector<int>& nums) { 2 int i=0,j=0,k=0; 3 int n=nums.size(); 4 for(int p=0;p<n;p++) 5 { 6 if(nums[p]==0) 7 i

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

LeetCode OJ平台上Sort Colors题目算法探讨

原题如下,意思就是说无序数组(由0,1,2组成),一遍扫描,把数组整理成若干个0-若干个1-若干个2这样的序列. Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integ

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integer

LeetCode: Sort Colors 题解

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[LeetCode OJ] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl