Rust代码
```rust
const ELEMS: [&str; 4] = ["A", "B", "C", "D"];
fn main() {
let combinations = generate_combinations(&ELEMS, 0, "");
println!("Combinations: {:?}", combinations);
}
fn generate_combinations(elems: &[&str], start: usize, prefix: &str) -> Vec<String> {
let mut combinations = Vec::new();
for i in start..elems.len() {
let new_prefix = prefix.to_string() + elems[i];
combinations.extend(generate_combinations(elems, i + 1, &new_prefix));
combinations.push(new_prefix);
}
combinations
}
```
这是递归,但对栈有压力
如果不想递归,遍历用2^N种可能的0-1组合,直接扁平化去做
--
FROM 1.202.157.*