Skip to content

Commit 84f545b

Browse files
authored
Added LeetCode 46: Permutations (#360)
* Added LeetCode 23: Merge K Sorted Lists * Added LeetDoe 46: Permutations * Added LeetCode 46: Permutation
1 parent ee21b78 commit 84f545b

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode *merge(ListNode *l1, ListNode *l2)
14+
{
15+
if(l1==NULL || l2==NULL) return l1==NULL ? l2 : l1;
16+
ListNode *p=new ListNode(-1), *d=p, *c1=l1, *c2=l2;
17+
while(c1!=NULL and c2!=NULL)
18+
{
19+
if(c1->val<=c2->val)
20+
{
21+
p->next=c1;
22+
c1=c1->next;
23+
}
24+
else
25+
{
26+
p->next=c2;
27+
c2=c2->next;
28+
}
29+
p=p->next;
30+
}
31+
p->next = c1!=NULL ? c1 : c2;
32+
return d->next;
33+
}
34+
ListNode *mergeK(vector<ListNode*> lists, int s, int e)
35+
{
36+
if(s==e) return lists[s];
37+
int m=s+((e-s)/2);
38+
return merge(mergeK(lists,s,m),mergeK(lists,m+1,e));
39+
}
40+
ListNode* mergeKLists(vector<ListNode*>& lists) {
41+
if(lists.size()==0) return NULL;
42+
int n=lists.size();
43+
return mergeK(lists,0,n-1);
44+
}
45+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int perm(vector<int> nums, vector<vector<int>> &ans, vector<int> &smallAns)
4+
{
5+
if(smallAns.size()==nums.size())
6+
{
7+
ans.push_back(smallAns);
8+
return 1;
9+
}
10+
int count=0;
11+
for(int i=0; i<nums.size(); i++)
12+
{
13+
if(nums[i]!=-11)
14+
{
15+
int temp=nums[i];
16+
nums[i]=-11;
17+
vector<int> tem = smallAns;
18+
tem.push_back(temp);
19+
count+=perm(nums,ans,tem);
20+
nums[i]=temp;
21+
}
22+
}
23+
return count;
24+
}
25+
vector<vector<int>> permute(vector<int>& nums) {
26+
vector<vector<int>> ans;
27+
vector<int> smallAns;
28+
int s=perm(nums,ans,smallAns);
29+
return ans;
30+
}
31+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int perm(vector<int> nums, vector<vector<int>> &ans, vector<int> &smallAns)
4+
{
5+
if(smallAns.size()==nums.size())
6+
{
7+
ans.push_back(smallAns);
8+
return 1;
9+
}
10+
int count=0;
11+
for(int i=0; i<nums.size(); i++)
12+
{
13+
if(nums[i]!=-11)
14+
{
15+
int temp=nums[i];
16+
nums[i]=-11;
17+
vector<int> tem = smallAns;
18+
tem.push_back(temp);
19+
count+=perm(nums,ans,tem);
20+
nums[i]=temp;
21+
}
22+
}
23+
return count;
24+
}
25+
vector<vector<int>> permute(vector<int>& nums) {
26+
vector<vector<int>> ans;
27+
vector<int> smallAns;
28+
int s=perm(nums,ans,smallAns);
29+
return ans;
30+
}
31+
};

0 commit comments

Comments
 (0)