↓ 跳过正文
  1. Posts/

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

有一个算法完美解决了这个问题,空间复杂度O(1),时间复杂度O(n)

The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place.

  1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
  2. Find the largest index l greater than k such that a[k] < a[l].
  3. Swap the value of a[k] with that of a[l].
  4. Reverse the sequence from a[k + 1] up to and including the final element a[n].
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月13日 星期四 14时39分33秒
 4File Name :31.cpp
 5************************************************ */
 6class Solution {
 7
 8public:
 9
10    void nextPermutation(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	    if (nums[i]<nums[i+1])
17	    {
18		k = i;
19		break;
20	    }
21	if (k==-1)
22	{
23	    reverse(nums.begin(),nums.end());
24	    return;
25	}
26	int l = -1;
27	for ( int i = n-1 ; i > k ; i--)
28	{
29	    if (nums[k]<nums[i])
30	    {
31		l = i;
32		break;
33	    }
34	}
35	swap(nums[l],nums[k]);
36	reverse(nums.begin()+k+1,nums.end());
37
38    }
39
40};

相关文章

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的组合。

leetcode 495. Teemo Attacking

·251 字·1 分钟
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s attacking ascending time series towards Ashe and the poisoning time duration per Teemo’s attacking, you need to output the total time that Ashe is in poisoned condition. You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately. 题意:若干长度相同的区间,升序给出区间左端点,以及区间长度。问区间被覆盖的长度总和。