Given a collection of distinct integers, return all possible permutations.
給一個集合,每個元素都是不同的整數。回傳所有可能的組合。
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
int size = nums.size();
vector<vector<int>> ans;
sort(nums.begin(),nums.end());
do
{
vector<int> temp;
for(int i=0;i<size;i++)
temp.push_back(nums[i]);
ans.push_back(temp);
}
while(next_permutation(nums.begin(),nums.end()));
return ans;
}
};
LeetCode
C++
1. Two Sum
Nov 15, 2023You are given an array of positive integers arr. Perform some operations (possibly none) on arr so that it satisfies these conditions:
Nov 15, 2023Given a string s, return the number of homogenous substrings of s. Since the answer may be too large, return it modulo 109 + 7.
Nov 9, 2023There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
Nov 9, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up