mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-26 04:56:06 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* File: permutations_i.rs
|
||||
* Created Time: 2023-07-15
|
||||
* Author: codingonion (coderonion@gmail.com)
|
||||
*/
|
||||
|
||||
/* バックトラッキング:順列 I */
|
||||
fn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {
|
||||
// 状態の長さが要素数に等しければ、解を記録
|
||||
if state.len() == choices.len() {
|
||||
res.push(state);
|
||||
return;
|
||||
}
|
||||
// すべての選択肢を走査
|
||||
for i in 0..choices.len() {
|
||||
let choice = choices[i];
|
||||
// 枝刈り:要素の重複選択を許可しない
|
||||
if !selected[i] {
|
||||
// 試行: 選択を行い、状態を更新
|
||||
selected[i] = true;
|
||||
state.push(choice);
|
||||
// 次の選択へ進む
|
||||
backtrack(state.clone(), choices, selected, res);
|
||||
// バックトラック:選択を取り消し、前の状態に戻す
|
||||
selected[i] = false;
|
||||
state.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 全順列 I */
|
||||
fn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {
|
||||
let mut res = Vec::new(); // 状態(部分集合)
|
||||
backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);
|
||||
res
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
pub fn main() {
|
||||
let mut nums = [1, 2, 3];
|
||||
|
||||
let res = permutations_i(&mut nums);
|
||||
|
||||
println!("入力配列 nums = {:?}", &nums);
|
||||
println!("すべての順列 res = {:?}", &res);
|
||||
}
|
||||
Reference in New Issue
Block a user