|
|
@ -32,60 +32,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
```java title="permutations_i.java"
|
|
|
|
```java title="permutations_i.java"
|
|
|
|
[class]{permutations_i}-[func]{backtrack}
|
|
|
|
[class]{permutations_i}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{permutations_i}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="permutations_i.cpp"
|
|
|
|
```cpp title="permutations_i.cpp"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="permutations_i.py"
|
|
|
|
```python title="permutations_i.py"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutations_i}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="permutations_i.go"
|
|
|
|
```go title="permutations_i.go"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="permutations_i.js"
|
|
|
|
```javascript title="permutations_i.js"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="permutations_i.ts"
|
|
|
|
```typescript title="permutations_i.ts"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
|
|
```c title="permutations_i.c"
|
|
|
|
```c title="permutations_i.c"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="permutations_i.cs"
|
|
|
|
```csharp title="permutations_i.cs"
|
|
|
|
[class]{permutations_i}-[func]{backtrack}
|
|
|
|
[class]{permutations_i}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{permutations_i}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="permutations_i.swift"
|
|
|
|
```swift title="permutations_i.swift"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="permutations_i.zig"
|
|
|
|
```zig title="permutations_i.zig"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsI}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
需要重点关注的是,我们引入了一个布尔型数组 `selected` ,它的长度与输入数组长度相等,其中 `selected[i]` 表示 `choices[i]` 是否已被选择。我们利用 `selected` 避免某个元素被重复选择,从而实现剪枝。
|
|
|
|
需要重点关注的是,我们引入了一个布尔型数组 `selected` ,它的长度与输入数组长度相等,其中 `selected[i]` 表示 `choices[i]` 是否已被选择。我们利用 `selected` 避免某个元素被重复选择,从而实现剪枝。
|
|
|
@ -114,60 +134,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
```java title="permutations_ii.java"
|
|
|
|
```java title="permutations_ii.java"
|
|
|
|
[class]{permutations_ii}-[func]{backtrack}
|
|
|
|
[class]{permutations_ii}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{permutations_ii}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="permutations_ii.cpp"
|
|
|
|
```cpp title="permutations_ii.cpp"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="permutations_ii.py"
|
|
|
|
```python title="permutations_ii.py"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutations_ii}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="permutations_ii.go"
|
|
|
|
```go title="permutations_ii.go"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "JavaScript"
|
|
|
|
=== "JavaScript"
|
|
|
|
|
|
|
|
|
|
|
|
```javascript title="permutations_ii.js"
|
|
|
|
```javascript title="permutations_ii.js"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "TypeScript"
|
|
|
|
=== "TypeScript"
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="permutations_ii.ts"
|
|
|
|
```typescript title="permutations_ii.ts"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
|
|
```c title="permutations_ii.c"
|
|
|
|
```c title="permutations_ii.c"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="permutations_ii.cs"
|
|
|
|
```csharp title="permutations_ii.cs"
|
|
|
|
[class]{permutations_ii}-[func]{backtrack}
|
|
|
|
[class]{permutations_ii}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{permutations_ii}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="permutations_ii.swift"
|
|
|
|
```swift title="permutations_ii.swift"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="permutations_ii.zig"
|
|
|
|
```zig title="permutations_ii.zig"
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
[class]{}-[func]{backtrack}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{permutationsII}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
注意,虽然 `selected` 和 `duplicated` 都起到剪枝的作用,但他们剪掉的是不同的分支:
|
|
|
|
注意,虽然 `selected` 和 `duplicated` 都起到剪枝的作用,但他们剪掉的是不同的分支:
|
|
|
|