LeetCode的medium题集合(C++实现)十四

1 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.

这里只有3个数进行排序,我们可以采用两指针方法,遍历数组,遇到红色时与头指针交换数据,头指针加1,遇到蓝色时与尾指针交换数据,尾指针减1,遍历直到遇到尾指针时结束。

void sortColors(vector<int>& nums) {
        int len = nums.size();
    int begin = 0, end = len - 1;
    for (int i = 0; i<len; i++)
    {
        if (i>end) return;
        if (nums[i] == 0)
        {
            swap(nums[i], nums[begin]);
            begin++;
        }
        else if (nums[i] == 2)
        {
            swap(nums[i], nums[end]);
            end--;
            i--;
        }
     }
    }

2 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],

]

从n个数中选取k个数的组合,我们可以选出1,再从2到n中选出k-1个数;,选出2,再从3到n中选出k-1个数;以此方法得到所有的组合。

 void combinedst(int n,int k,int level, vector<int> &res,vector<vector<int>> &result)
  {
    if(res.size() == k)
    {
        result.push_back(res);
        return;
    }
    for(int i = level; i<=n; i++)
    {
        res.push_back(i);
        combinedst(n,k,i+1,res,result);
        res.pop_back();
    }
  }
    vector<vector<int>> combine(int n, int k)
    {
        vector<int> res;
        vector<vector<int> > result;
        if(n<k||k<=0) return result;
        combinedst(n,k,1,res,result);
        return result;
    }
时间: 2024-11-05 14:43:41

LeetCode的medium题集合(C++实现)十四的相关文章

LeetCode的medium题集合(C++实现)十五

1 Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,3], a solution is: [ [3], [1],

LeetCode的medium题集合(C++实现)十

1 Permutation Sequence The set [1,2,3,-,n] contains a total of n! unique permutations.Given n and k, return the kth permutation sequence. 使用Next Permutation循环k次可以得到序列,但leetcode上提交会出现时间超过限制.下面采用数学法: 在n!个排列中,第一位元素相同的排列总是有(n?1)!个,如果p=k/(n?1)!,那么排列的第一位元素

LeetCode的medium题集合(C++实现)十二

1 Sqrt(x) Implement int sqrt(int x).Compute and return the square root of x. 因为这里都是整数,可以直接采用二分法求解 int mySqrt(int x) { if (x <= 1) return x; int begin = 1, end =x, mid = 0; while (begin<=end) { mid = (begin + end) / 2; if (mid<x / mid) //不要用乘法判断,否

LeetCode的medium题集合(C++实现)十六

1 Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of

LeetCode的medium题集合(C++实现)十一

1 Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked

LeetCode的medium题集合(C++实现)五

1 Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. 可以采用递归的方法解决这个问题,当找到一组数之和

LeetCode的medium题集合(C++实现)六

1 Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative. 该题实际就是利用字符串来解决大数的乘法问题.为了计算方便先将两组数翻转,将字符串转化为整数利用两个循环逐位相乘,将结果保存.然后逐位解决进位问题. s

LeetCode的medium题集合(C++实现)四

1 Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending or

LeetCode的medium题集合(C++实现)九

1 Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index. 利用两指针法