Skip to content

feat: add solutions to lc problems: No.3226,3227 #3298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,69 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3226.Nu

<!-- solution:start -->

### 方法一
### 方法一:位运算

如果 $n$ 和 $k$ 的按位与结果不等于 $k$,说明 $k$ 存在某一位为 $1$,而 $n$ 对应的位为 $0$,此时无法通过改变 $n$ 的某一位使得 $n$ 等于 $k$,返回 $-1$;否则,我们统计 $n \oplus k$ 的二进制表示中 $1$ 的个数即可。

时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,69 @@ It is not possible to make <code>n</code> equal to <code>k</code>.</p>

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int minChanges(int n, int k) {
return (n & k) != k ? -1 : __builtin_popcount(n ^ k);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func minChanges(n int, k int) int {
if n&k != k {
return -1
}
return bits.OnesCount(uint(n ^ k))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
public int minChanges(int n, int k) {
return (n & k) != k ? -1 : Integer.bitCount(n ^ k);
}
}
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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;
}
65 changes: 61 additions & 4 deletions solution/3200-3299/3227.Vowels Game in a String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,89 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3227.Vo

<!-- solution:start -->

### 方法一
### 方法一:脑筋急转弯

我们不妨记字符串中元音字母的个数为 $k$。

如果 $k = 0$,即字符串中没有元音字母,那么小红无法移除任何子字符串,小明直接获胜。

如果 $k$ 为奇数,那么小红可以移除整个字符串,小红直接获胜。

如果 $k$ 为偶数,那么小红可以移除 $k - 1$ 个元音字母,此时剩下一个元音字母,小明无法移除任何子字符串,小红直接获胜。

综上所述,如果字符串中包含元音字母,那么小红获胜,否则小明获胜。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
65 changes: 61 additions & 4 deletions solution/3200-3299/3227.Vowels Game in a String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,89 @@ There is no valid play for Alice in her first turn, so Alice loses the game.</p>

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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;
}
```

<!-- tabs:end -->
Expand Down
12 changes: 12 additions & 0 deletions solution/3200-3299/3227.Vowels Game in a String/Solution.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
Loading
Loading