typestatusdateslugsummarytagscategoryiconpassword创建时间Jan 18, 2025 01:19 PM给你一个正整数 n。如果一个二进制字符串 x 的所有长度为 2 的子字符串中包含至少一个 "1",则称 x 是一个有效字符串。返回所有长度为 n 的 有效 字符串,可以以任意顺序排列。https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/description/位运算基础怎么判断二进制中是否有相邻的 0?我们可以把 i 取反(保留低 n 位),记作 x。问题变成:判断 x 中是否有相邻的 1。用 x & (x >> 1) 来判断,如果这个值不为零,则说明 x 中有相邻的 1,反之没有。📎 参考两种方法:回溯/位运算(Python/Java/C++/Go/JS/Rust)