↓ 跳过正文
  1. Posts/

leetcode 46. Permutations (生成全排列,无重复元素)

·248 字·1 分钟

Given a collection of distinct numbers, return all possible permutations.

思路:调用n-1次 leetcode 31 解题报告 中提到的算法即可。。。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 14时49分34秒
 4File Name :46.cpp
 5 ************************************************ */
 6class Solution {
 7
 8    public:
 9
10	void solve(vector<int> &nums)
11	{
12	    int n = nums.size();
13	    if (n==0) return;
14	    int k = -1;
15	    for ( int i = n-2 ; i >= 0 ; i--)
16	    {
17		if (nums[i]<nums[i+1])
18		{
19		    k = i ;
20		    break;
21		}
22	    }
23	    if (k==-1)
24	    {
25		reverse(nums.begin(),nums.end());
26		return;
27	    }
28	    int l = -1;
29	    for ( int i = n-1 ; i > k ; i-- )
30	    {
31		if (nums[k]<nums[i])
32		{
33		    l = i ;
34		    break;
35		}
36	    }
37	    swap(nums[l],nums[k]);
38	    reverse(nums.begin()+k+1,nums.end());
39	}
40	vector<vector<int>> permute(vector<int>& nums) {
41	    vector<vector<int> >res;
42	    int n = nums.size();
43	    int total = 1;
44	    for ( int i = 2 ; i <= n ; i++) total*=i;
45	    for ( int i = 1 ; i <= total; i++)
46	    {
47		res.push_back(nums);
48		solve(nums);
49	    }
50
51	    return res;
52	}
53
54};

相关文章

leetcode 31. Next Permutation (in-place 生成下一个全排列)

·346 字·1 分钟
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 order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 思路: 参考了wiki_Permutation

leetcode 33. Search in Rotated Sorted Array (无重复数的旋转数组找定值)

·372 字·1 分钟
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. 思路:找规律。。。二分。。。 0 1 2 3 4 5 6 1 2 3 4 5 6 0 2 3 4 5 6 0 1 3 4 5 6 0 1 2 4 5 6 0 1 2 3 5 6 0 1 2 3 4 6 0 1 2 3 4 5 观察发现。。。a[mid]<a[r]的时候,后半段有序;

leetcode 34. Search for a Range (二分,找到一段值为tar的区间)

·333 字·1 分钟
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4]. 思路:二分。。。 我好像根本不会二分啊??? 二分查找

leetcode 39. Combination Sum (dfs,求所有的组合,和为定值,每个数可以重复用)

·278 字·1 分钟
Given a set of candidate numbers (C) (without duplicates) 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. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意:给n个数,求所有的组合,和为定值,每个数可以重复用)

leetcode 40. Combination Sum II (枚举子集,和为定值)

·106 字·1 分钟
* Total Accepted: **106670** * Total Submissions: **329718** * Difficulty: **Medium** * Contributor: **LeetCode** Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: * All numbers (including target) will be positive integers. * The solution set must not contain duplicate combinations. 题意:若干正数,求所有和为target的组合。