diff --git a/solution/1500-1599/1529.Minimum Suffix Flips/README.md b/solution/1500-1599/1529.Minimum Suffix Flips/README.md index 863f44b03a495..dc611213b4e9c 100644 --- a/solution/1500-1599/1529.Minimum Suffix Flips/README.md +++ b/solution/1500-1599/1529.Minimum Suffix Flips/README.md @@ -81,9 +81,9 @@ tags: ### 方法一:贪心 -从前往后遍历 $target$,判断每个位置是否需要翻转,如果需要翻转,则翻转,并记录翻转次数。 +我们从左到右遍历字符串 $\textit{target}$,用变量 $\textit{ans}$ 记录翻转次数。 当遍历到下标 $i$ 时,如果当前的翻转次数 $\textit{ans}$ 的奇偶性与 $\textit{target}[i]$ 不同,则需要在下标 $i$ 处进行一次翻转操作,将 $\textit{ans}$ 加 $1$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 @@ -149,6 +149,37 @@ func minFlips(target string) int { } ``` +#### TypeScript + +```ts +function minFlips(target: string): number { + let ans = 0; + for (const c of target) { + if (ans % 2 !== +c) { + ++ans; + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_flips(target: String) -> i32 { + let mut ans = 0; + for c in target.chars() { + let bit = (c as u8 - b'0') as i32; + if ans % 2 != bit { + ans += 1; + } + } + ans + } +} +``` + diff --git a/solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md b/solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md index e696a2f4a69e6..b04b545c908d5 100644 --- a/solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md +++ b/solution/1500-1599/1529.Minimum Suffix Flips/README_EN.md @@ -73,7 +73,11 @@ We need at least 3 flip operations to form target. -### Solution 1 +### Solution 1: Greedy + +We traverse the string $\textit{target}$ from left to right, using a variable $\textit{ans}$ to record the number of flips. When we reach index $i$, if the parity of the current flip count $\textit{ans}$ is different from $\textit{target}[i]$, we need to perform a flip operation at index $i$ and increment $\textit{ans}$ by $1$. + +The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$. @@ -139,6 +143,37 @@ func minFlips(target string) int { } ``` +#### TypeScript + +```ts +function minFlips(target: string): number { + let ans = 0; + for (const c of target) { + if (ans % 2 !== +c) { + ++ans; + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_flips(target: String) -> i32 { + let mut ans = 0; + for c in target.chars() { + let bit = (c as u8 - b'0') as i32; + if ans % 2 != bit { + ans += 1; + } + } + ans + } +} +``` + diff --git a/solution/1500-1599/1529.Minimum Suffix Flips/Solution.rs b/solution/1500-1599/1529.Minimum Suffix Flips/Solution.rs new file mode 100644 index 0000000000000..2430fb747e3e3 --- /dev/null +++ b/solution/1500-1599/1529.Minimum Suffix Flips/Solution.rs @@ -0,0 +1,12 @@ +impl Solution { + pub fn min_flips(target: String) -> i32 { + let mut ans = 0; + for c in target.chars() { + let bit = (c as u8 - b'0') as i32; + if ans % 2 != bit { + ans += 1; + } + } + ans + } +} diff --git a/solution/1500-1599/1529.Minimum Suffix Flips/Solution.ts b/solution/1500-1599/1529.Minimum Suffix Flips/Solution.ts new file mode 100644 index 0000000000000..e6fc368649f3f --- /dev/null +++ b/solution/1500-1599/1529.Minimum Suffix Flips/Solution.ts @@ -0,0 +1,9 @@ +function minFlips(target: string): number { + let ans = 0; + for (const c of target) { + if (ans % 2 !== +c) { + ++ans; + } + } + return ans; +}