Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19b8a93

Browse files
committed
feat: add solutions to lc problems: No.0046,0047
- No.0046.Permutations - No.0047.Permutations II
1 parent 6cefe38 commit 19b8a93

File tree

8 files changed

+376
-60
lines changed

8 files changed

+376
-60
lines changed

‎solution/0000-0099/0046.Permutations/README.md‎

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,42 @@ public:
176176
};
177177
```
178178

179+
### **C#**
180+
181+
```cs
182+
using System.Collections.Generic;
183+
using System.Linq;
184+
185+
public class Solution {
186+
public IList<IList<int>> Permute(int[] nums) {
187+
var results = new List<IList<int>>();
188+
var temp = new List<int>();
189+
var visited = new bool[nums.Length];
190+
Search(nums, visited, temp, results);
191+
return results;
192+
}
193+
194+
private void Search(int[] nums, bool[] visited, IList<int> temp, IList<IList<int>> results)
195+
{
196+
int count = 0;
197+
for (var i = 0; i < nums.Length; ++i)
198+
{
199+
if (visited[i]) continue;
200+
++count;
201+
temp.Add(nums[i]);
202+
visited[i] = true;
203+
Search(nums, visited, temp, results);
204+
temp.RemoveAt(temp.Count - 1);
205+
visited[i] = false;
206+
}
207+
if (count == 0 && temp.Any())
208+
{
209+
results.Add(new List<int>(temp));
210+
}
211+
}
212+
}
213+
```
214+
179215
### **Go**
180216

181217
```go
@@ -210,19 +246,19 @@ func dfs(u, n int, nums []int, used []bool, path []int, res *[][]int) {
210246

211247
```ts
212248
function permute(nums: number[]): number[][] {
249+
const n = nums.length;
213250
const res: number[][] = [];
214-
const dfs = (paths: number[]) => {
215-
if (paths.length === nums.length) {
216-
res.push(paths);
217-
return;
251+
const dfs = (i: number) => {
252+
if (i === n) {
253+
res.push([...nums]);
218254
}
219-
for (const num ofnums) {
220-
if (!paths.includes(num)) {
221-
dfs(paths.concat(num));
222-
}
255+
for (let j =i; j<n; j++) {
256+
[nums[i], nums[j]] = [nums[j], nums[i]];
257+
dfs(i+1);
258+
[nums[i], nums[j]] = [nums[j], nums[i]];
223259
}
224260
};
225-
dfs([]);
261+
dfs(0);
226262
return res;
227263
}
228264
```
@@ -231,23 +267,22 @@ function permute(nums: number[]): number[][] {
231267

232268
```rust
233269
impl Solution {
234-
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
235-
if paths.len() == nums.len() {
236-
res.push(paths.clone());
270+
fn dfs(i: usize, nums: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
271+
let n = nums.len();
272+
if i == n {
273+
res.push(nums.clone());
237274
return;
238275
}
239-
for i in nums.iter() {
240-
if !paths.contains(i) {
241-
paths.push(*i);
242-
Self::dfs(nums, paths, res);
243-
paths.pop();
244-
}
276+
for j in i..n {
277+
nums.swap(i, j);
278+
Self::dfs(i + 1, nums, res);
279+
nums.swap(i, j);
245280
}
246281
}
247282

248-
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
283+
pub fn permute(mutnums: Vec<i32>) -> Vec<Vec<i32>> {
249284
let mut res = vec![];
250-
Self::dfs(&nums, &mut vec![], &mut res);
285+
Self::dfs(0, &mut nums, &mut res);
251286
res
252287
}
253288
}

‎solution/0000-0099/0046.Permutations/README_EN.md‎

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,42 @@ public:
155155
};
156156
```
157157

158+
### **C#**
159+
160+
```cs
161+
using System.Collections.Generic;
162+
using System.Linq;
163+
164+
public class Solution {
165+
public IList<IList<int>> Permute(int[] nums) {
166+
var results = new List<IList<int>>();
167+
var temp = new List<int>();
168+
var visited = new bool[nums.Length];
169+
Search(nums, visited, temp, results);
170+
return results;
171+
}
172+
173+
private void Search(int[] nums, bool[] visited, IList<int> temp, IList<IList<int>> results)
174+
{
175+
int count = 0;
176+
for (var i = 0; i < nums.Length; ++i)
177+
{
178+
if (visited[i]) continue;
179+
++count;
180+
temp.Add(nums[i]);
181+
visited[i] = true;
182+
Search(nums, visited, temp, results);
183+
temp.RemoveAt(temp.Count - 1);
184+
visited[i] = false;
185+
}
186+
if (count == 0 && temp.Any())
187+
{
188+
results.Add(new List<int>(temp));
189+
}
190+
}
191+
}
192+
```
193+
158194
### **Go**
159195

160196
```go
@@ -189,19 +225,19 @@ func dfs(u, n int, nums []int, used []bool, path []int, res *[][]int) {
189225

190226
```ts
191227
function permute(nums: number[]): number[][] {
228+
const n = nums.length;
192229
const res: number[][] = [];
193-
const dfs = (paths: number[]) => {
194-
if (paths.length === nums.length) {
195-
res.push(paths);
196-
return;
230+
const dfs = (i: number) => {
231+
if (i === n) {
232+
res.push([...nums]);
197233
}
198-
for (const num ofnums) {
199-
if (!paths.includes(num)) {
200-
dfs(paths.concat(num));
201-
}
234+
for (let j =i; j<n; j++) {
235+
[nums[i], nums[j]] = [nums[j], nums[i]];
236+
dfs(i+1);
237+
[nums[i], nums[j]] = [nums[j], nums[i]];
202238
}
203239
};
204-
dfs([]);
240+
dfs(0);
205241
return res;
206242
}
207243
```
@@ -210,23 +246,22 @@ function permute(nums: number[]): number[][] {
210246

211247
```rust
212248
impl Solution {
213-
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
214-
if paths.len() == nums.len() {
215-
res.push(paths.clone());
249+
fn dfs(i: usize, nums: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
250+
let n = nums.len();
251+
if i == n {
252+
res.push(nums.clone());
216253
return;
217254
}
218-
for i in nums.iter() {
219-
if !paths.contains(i) {
220-
paths.push(*i);
221-
Self::dfs(nums, paths, res);
222-
paths.pop();
223-
}
255+
for j in i..n {
256+
nums.swap(i, j);
257+
Self::dfs(i + 1, nums, res);
258+
nums.swap(i, j);
224259
}
225260
}
226261

227-
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
262+
pub fn permute(mutnums: Vec<i32>) -> Vec<Vec<i32>> {
228263
let mut res = vec![];
229-
Self::dfs(&nums, &mut vec![], &mut res);
264+
Self::dfs(0, &mut nums, &mut res);
230265
res
231266
}
232267
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
impl Solution {
2-
fn dfs(nums: &Vec<i32>, paths: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3-
if paths.len() == nums.len() {
4-
res.push(paths.clone());
2+
fn dfs(i: usize, nums: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
3+
let n = nums.len();
4+
if i == n {
5+
res.push(nums.clone());
56
return;
67
}
7-
for i in nums.iter() {
8-
if !paths.contains(i) {
9-
paths.push(*i);
10-
Self::dfs(nums, paths, res);
11-
paths.pop();
12-
}
8+
for j in i..n {
9+
nums.swap(i, j);
10+
Self::dfs(i + 1, nums, res);
11+
nums.swap(i, j);
1312
}
1413
}
1514

16-
pub fn permute(nums: Vec<i32>) -> Vec<Vec<i32>> {
15+
pub fn permute(mutnums: Vec<i32>) -> Vec<Vec<i32>> {
1716
let mut res = vec![];
18-
Self::dfs(&nums, &mut vec![], &mut res);
17+
Self::dfs(0, &mut nums, &mut res);
1918
res
2019
}
2120
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
function permute(nums: number[]): number[][] {
2+
const n = nums.length;
23
const res: number[][] = [];
3-
const dfs = (paths: number[]) => {
4-
if (paths.length === nums.length) {
5-
res.push(paths);
6-
return;
4+
const dfs = (i: number) => {
5+
if (i === n) {
6+
res.push([...nums]);
77
}
8-
for (constnumofnums) {
9-
if(!paths.includes(num)){
10-
dfs(paths.concat(num));
11-
}
8+
for (letj=i;j<n;j++) {
9+
[nums[i],nums[j]]=[nums[j],nums[i]];
10+
dfs(i+1);
11+
[nums[i],nums[j]]=[nums[j],nums[i]];
1212
}
1313
};
14-
dfs([]);
14+
dfs(0);
1515
return res;
1616
}

‎solution/0000-0099/0047.Permutations II/README.md‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,49 @@ public:
141141
};
142142
```
143143

144+
### **C#**
145+
146+
```cs
147+
using System.Collections.Generic;
148+
using System.Linq;
149+
150+
public class Solution {
151+
public IList<IList<int>> PermuteUnique(int[] nums) {
152+
var results = new List<IList<int>>();
153+
var temp = new List<int>();
154+
var count = nums.GroupBy(n => n).ToDictionary(g => g.Key, g => g.Count());
155+
Search(count, temp, results);
156+
return results;
157+
}
158+
159+
private void Search(Dictionary<int, int> count, IList<int> temp, IList<IList<int>> results)
160+
{
161+
if (!count.Any() && temp.Any())
162+
{
163+
results.Add(new List<int>(temp));
164+
return;
165+
}
166+
var keys = count.Keys.ToList();
167+
foreach (var key in keys)
168+
{
169+
temp.Add(key);
170+
--count[key];
171+
if (count[key] == 0) count.Remove(key);
172+
Search(count, temp, results);
173+
temp.RemoveAt(temp.Count - 1);
174+
if (count.ContainsKey(key))
175+
{
176+
++count[key];
177+
}
178+
else
179+
{
180+
count[key] = 1;
181+
}
182+
}
183+
}
184+
}
185+
```
186+
144187
### **Go**
145188

146189
```go
@@ -173,6 +216,63 @@ func dfs(u, n int, nums []int, used []bool, path []int, res *[][]int) {
173216
}
174217
```
175218

219+
### **TypeScript**
220+
221+
```ts
222+
function permuteUnique(nums: number[]): number[][] {
223+
const n = nums.length;
224+
const res: number[][] = [];
225+
const dfs = (i: number) => {
226+
if (i === n) {
227+
res.push([...nums]);
228+
}
229+
const set = new Set<number>();
230+
for (let j = i; j < n; j++) {
231+
if (set.has(nums[j])) {
232+
continue;
233+
}
234+
set.add(nums[j]);
235+
[nums[i], nums[j]] = [nums[j], nums[i]];
236+
dfs(i + 1);
237+
[nums[i], nums[j]] = [nums[j], nums[i]];
238+
}
239+
};
240+
dfs(0);
241+
return res;
242+
}
243+
```
244+
245+
### **Rust**
246+
247+
```rust
248+
use std::collections::HashSet;
249+
impl Solution {
250+
fn dfs(i: usize, nums: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
251+
let n = nums.len();
252+
if i == n {
253+
res.push(nums.clone());
254+
return;
255+
}
256+
let mut set = HashSet::new();
257+
for j in i..n {
258+
if set.contains(&nums[j]) {
259+
continue;
260+
}
261+
set.insert(nums[j]);
262+
nums.swap(i, j);
263+
Self::dfs(i + 1, nums, res);
264+
nums.swap(i, j);
265+
}
266+
}
267+
268+
pub fn permute_unique(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
269+
let mut res = vec![];
270+
Self::dfs(0, &mut nums, &mut res);
271+
res
272+
}
273+
}
274+
```
275+
176276
### **...**
177277

178278
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /