diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md index 92940dab02a0d..5e75e24d309ad 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README.md @@ -71,32 +71,69 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3226.Nu -### 方法一 +### 方法一:位运算 + +如果 $n$ 和 $k$ 的按位与结果不等于 $k$,说明 $k$ 存在某一位为 $1$,而 $n$ 对应的位为 $0$,此时无法通过改变 $n$ 的某一位使得 $n$ 等于 $k$,返回 $-1$;否则,我们统计 $n \oplus k$ 的二进制表示中 $1$ 的个数即可。 + +时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minChanges(self, n: int, k: int) -> int: + return -1 if n & k != k else (n ^ k).bit_count() ``` #### Java ```java - +class Solution { + public int minChanges(int n, int k) { + return (n & k) != k ? -1 : Integer.bitCount(n ^ k); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minChanges(int n, int k) { + return (n & k) != k ? -1 : __builtin_popcount(n ^ k); + } +}; ``` #### Go ```go +func minChanges(n int, k int) int { + if n&k != k { + return -1 + } + return bits.OnesCount(uint(n ^ k)) +} +``` +#### TypeScript + +```ts +function minChanges(n: number, k: number): number { + return (n & k) !== k ? -1 : bitCount(n ^ k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md index b98669bc93c3d..453d423551859 100644 --- a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/README_EN.md @@ -68,32 +68,69 @@ It is not possible to make n equal to k.

-### Solution 1 +### Solution 1: Bit Manipulation + +If the bitwise AND result of $n$ and $k$ is not equal to $k$, it indicates that there exists at least one bit where $k$ is $1$ and the corresponding bit in $n$ is $0$. In this case, it is impossible to modify a bit in $n$ to make $n$ equal to $k$, and we return $-1$. Otherwise, we count the number of $1$s in the binary representation of $n \oplus k$. + +The time complexity is $O(\log n)$, and the space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def minChanges(self, n: int, k: int) -> int: + return -1 if n & k != k else (n ^ k).bit_count() ``` #### Java ```java - +class Solution { + public int minChanges(int n, int k) { + return (n & k) != k ? -1 : Integer.bitCount(n ^ k); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int minChanges(int n, int k) { + return (n & k) != k ? -1 : __builtin_popcount(n ^ k); + } +}; ``` #### Go ```go +func minChanges(n int, k int) int { + if n&k != k { + return -1 + } + return bits.OnesCount(uint(n ^ k)) +} +``` +#### TypeScript + +```ts +function minChanges(n: number, k: number): number { + return (n & k) !== k ? -1 : bitCount(n ^ k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} ``` diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cpp b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cpp new file mode 100644 index 0000000000000..9443e7b448dab --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int minChanges(int n, int k) { + return (n & k) != k ? -1 : __builtin_popcount(n ^ k); + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.go b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.go new file mode 100644 index 0000000000000..3415816b54b01 --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.go @@ -0,0 +1,6 @@ +func minChanges(n int, k int) int { + if n&k != k { + return -1 + } + return bits.OnesCount(uint(n ^ k)) +} \ No newline at end of file diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.java b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.java new file mode 100644 index 0000000000000..04157dcaf50a7 --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.java @@ -0,0 +1,5 @@ +class Solution { + public int minChanges(int n, int k) { + return (n & k) != k ? -1 : Integer.bitCount(n ^ k); + } +} \ No newline at end of file diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.py b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.py new file mode 100644 index 0000000000000..5c707555daea3 --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def minChanges(self, n: int, k: int) -> int: + return -1 if n & k != k else (n ^ k).bit_count() diff --git a/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.ts b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.ts new file mode 100644 index 0000000000000..3833d04359b5e --- /dev/null +++ b/solution/3200-3299/3226.Number of Bit Changes to Make Two Integers Equal/Solution.ts @@ -0,0 +1,12 @@ +function minChanges(n: number, k: number): number { + return (n & k) !== k ? -1 : bitCount(n ^ k); +} + +function bitCount(i: number): number { + i = i - ((i >>> 1) & 0x55555555); + i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); + i = (i + (i >>> 4)) & 0x0f0f0f0f; + i = i + (i >>> 8); + i = i + (i >>> 16); + return i & 0x3f; +} diff --git a/solution/3200-3299/3227.Vowels Game in a String/README.md b/solution/3200-3299/3227.Vowels Game in a String/README.md index 998ef8fe47c33..f864618c7259f 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README.md @@ -75,32 +75,89 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3227.Vo -### 方法一 +### 方法一:脑筋急转弯 + +我们不妨记字符串中元音字母的个数为 $k$。 + +如果 $k = 0$,即字符串中没有元音字母,那么小红无法移除任何子字符串,小明直接获胜。 + +如果 $k$ 为奇数,那么小红可以移除整个字符串,小红直接获胜。 + +如果 $k$ 为偶数,那么小红可以移除 $k - 1$ 个元音字母,此时剩下一个元音字母,小明无法移除任何子字符串,小红直接获胜。 + +综上所述,如果字符串中包含元音字母,那么小红获胜,否则小明获胜。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def doesAliceWin(self, s: str) -> bool: + vowels = set("aeiou") + return any(c in vowels for c in s) ``` #### Java ```java - +class Solution { + public boolean doesAliceWin(String s) { + for (int i = 0; i < s.length(); ++i) { + if ("aeiou".indexOf(s.charAt(i)) != -1) { + return true; + } + } + return false; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool doesAliceWin(string s) { + string vowels = "aeiou"; + for (char c : s) { + if (vowels.find(c) != string::npos) { + return true; + } + } + return false; + } +}; ``` #### Go ```go +func doesAliceWin(s string) bool { + vowels := "aeiou" + for _, c := range s { + if strings.ContainsRune(vowels, c) { + return true + } + } + return false +} +``` +#### TypeScript + +```ts +function doesAliceWin(s: string): boolean { + const vowels = 'aeiou'; + for (const c of s) { + if (vowels.includes(c)) { + return true; + } + } + return false; +} ``` diff --git a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md index 95e5c58a97fad..eeb9fe4929ae8 100644 --- a/solution/3200-3299/3227.Vowels Game in a String/README_EN.md +++ b/solution/3200-3299/3227.Vowels Game in a String/README_EN.md @@ -73,32 +73,89 @@ There is no valid play for Alice in her first turn, so Alice loses the game.

-### Solution 1 +### Solution 1: Brain Teaser + +Let's denote the number of vowels in the string as $k$. + +If $k = 0$, meaning there are no vowels in the string, then Little Red cannot remove any substring, and Little Ming wins directly. + +If $k$ is odd, then Little Red can remove the entire string, resulting in a direct win for Little Red. + +If $k$ is even, then Little Red can remove $k - 1$ vowels, leaving one vowel in the string. In this case, Little Ming cannot remove any substring, leading to a direct win for Little Red. + +In conclusion, if the string contains vowels, then Little Red wins; otherwise, Little Ming wins. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def doesAliceWin(self, s: str) -> bool: + vowels = set("aeiou") + return any(c in vowels for c in s) ``` #### Java ```java - +class Solution { + public boolean doesAliceWin(String s) { + for (int i = 0; i < s.length(); ++i) { + if ("aeiou".indexOf(s.charAt(i)) != -1) { + return true; + } + } + return false; + } +} ``` #### C++ ```cpp - +class Solution { +public: + bool doesAliceWin(string s) { + string vowels = "aeiou"; + for (char c : s) { + if (vowels.find(c) != string::npos) { + return true; + } + } + return false; + } +}; ``` #### Go ```go +func doesAliceWin(s string) bool { + vowels := "aeiou" + for _, c := range s { + if strings.ContainsRune(vowels, c) { + return true + } + } + return false +} +``` +#### TypeScript + +```ts +function doesAliceWin(s: string): boolean { + const vowels = 'aeiou'; + for (const c of s) { + if (vowels.includes(c)) { + return true; + } + } + return false; +} ``` diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.cpp b/solution/3200-3299/3227.Vowels Game in a String/Solution.cpp new file mode 100644 index 0000000000000..d6f1ed3c761e8 --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + bool doesAliceWin(string s) { + string vowels = "aeiou"; + for (char c : s) { + if (vowels.find(c) != string::npos) { + return true; + } + } + return false; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.go b/solution/3200-3299/3227.Vowels Game in a String/Solution.go new file mode 100644 index 0000000000000..fd0d441936b5d --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.go @@ -0,0 +1,9 @@ +func doesAliceWin(s string) bool { + vowels := "aeiou" + for _, c := range s { + if strings.ContainsRune(vowels, c) { + return true + } + } + return false +} \ No newline at end of file diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.java b/solution/3200-3299/3227.Vowels Game in a String/Solution.java new file mode 100644 index 0000000000000..96d204480b6ab --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public boolean doesAliceWin(String s) { + for (int i = 0; i < s.length(); ++i) { + if ("aeiou".indexOf(s.charAt(i)) != -1) { + return true; + } + } + return false; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.py b/solution/3200-3299/3227.Vowels Game in a String/Solution.py new file mode 100644 index 0000000000000..0f2ca25232cef --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def doesAliceWin(self, s: str) -> bool: + vowels = set("aeiou") + return any(c in vowels for c in s) diff --git a/solution/3200-3299/3227.Vowels Game in a String/Solution.ts b/solution/3200-3299/3227.Vowels Game in a String/Solution.ts new file mode 100644 index 0000000000000..78295149c93af --- /dev/null +++ b/solution/3200-3299/3227.Vowels Game in a String/Solution.ts @@ -0,0 +1,9 @@ +function doesAliceWin(s: string): boolean { + const vowels = 'aeiou'; + for (const c of s) { + if (vowels.includes(c)) { + return true; + } + } + return false; +}