Skip to content
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

feat: add solutions to lc problems: No.3461,3462 #4102

Merged
merged 1 commit into from
Feb 23, 2025
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 @@ -88,32 +88,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Ch

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以模拟题目描述的操作,直到字符串 $s$ 中只剩下两个数字为止,判断这两个数字是否相同。

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

<!-- tabs:start -->

#### Python3

```python

class Solution:
def hasSameDigits(self, s: str) -> bool:
t = list(map(int, s))
n = len(t)
for k in range(n - 1, 1, -1):
for i in range(k):
t[i] = (t[i] + t[i + 1]) % 10
return t[0] == t[1]
```

#### Java

```java

class Solution {
public boolean hasSameDigits(String s) {
char[] t = s.toCharArray();
int n = t.length;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
}
}
return t[0] == t[1];
}
}
```

#### C++

```cpp

class Solution {
public:
bool hasSameDigits(string s) {
int n = s.size();
string t = s;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (t[i] - '0' + t[i + 1] - '0') % 10 + '0';
}
}
return t[0] == t[1];
}
};
```

#### Go

```go
func hasSameDigits(s string) bool {
t := []byte(s)
n := len(t)
for k := n - 1; k > 1; k-- {
for i := 0; i < k; i++ {
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
}
}
return t[0] == t[1]
}
```

#### TypeScript

```ts
function hasSameDigits(s: string): boolean {
const t = s.split('').map(Number);
const n = t.length;
for (let k = n - 1; k > 1; --k) {
for (let i = 0; i < k; ++i) {
t[i] = (t[i] + t[i + 1]) % 10;
}
}
return t[0] === t[1];
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,90 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3461.Ch

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can simulate the operations described in the problem until the string $s$ contains exactly two digits, and then check if these two digits are the same.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def hasSameDigits(self, s: str) -> bool:
t = list(map(int, s))
n = len(t)
for k in range(n - 1, 1, -1):
for i in range(k):
t[i] = (t[i] + t[i + 1]) % 10
return t[0] == t[1]
```

#### Java

```java

class Solution {
public boolean hasSameDigits(String s) {
char[] t = s.toCharArray();
int n = t.length;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
}
}
return t[0] == t[1];
}
}
```

#### C++

```cpp

class Solution {
public:
bool hasSameDigits(string s) {
int n = s.size();
string t = s;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (t[i] - '0' + t[i + 1] - '0') % 10 + '0';
}
}
return t[0] == t[1];
}
};
```

#### Go

```go
func hasSameDigits(s string) bool {
t := []byte(s)
n := len(t)
for k := n - 1; k > 1; k-- {
for i := 0; i < k; i++ {
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
}
}
return t[0] == t[1]
}
```

#### TypeScript

```ts
function hasSameDigits(s: string): boolean {
const t = s.split('').map(Number);
const n = t.length;
for (let k = n - 1; k > 1; --k) {
for (let i = 0; i < k; ++i) {
t[i] = (t[i] + t[i + 1]) % 10;
}
}
return t[0] === t[1];
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
bool hasSameDigits(string s) {
int n = s.size();
string t = s;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (t[i] - '0' + t[i + 1] - '0') % 10 + '0';
}
}
return t[0] == t[1];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func hasSameDigits(s string) bool {
t := []byte(s)
n := len(t)
for k := n - 1; k > 1; k-- {
for i := 0; i < k; i++ {
t[i] = (t[i]-'0'+t[i+1]-'0')%10 + '0'
}
}
return t[0] == t[1]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public boolean hasSameDigits(String s) {
char[] t = s.toCharArray();
int n = t.length;
for (int k = n - 1; k > 1; --k) {
for (int i = 0; i < k; ++i) {
t[i] = (char) ((t[i] - '0' + t[i + 1] - '0') % 10 + '0');
}
}
return t[0] == t[1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def hasSameDigits(self, s: str) -> bool:
t = list(map(int, s))
n = len(t)
for k in range(n - 1, 1, -1):
for i in range(k):
t[i] = (t[i] + t[i + 1]) % 10
return t[0] == t[1]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function hasSameDigits(s: string): boolean {
const t = s.split('').map(Number);
const n = t.length;
for (let k = n - 1; k > 1; --k) {
for (let i = 0; i < k; ++i) {
t[i] = (t[i] + t[i + 1]) % 10;
}
}
return t[0] === t[1];
}
Loading