Skip to content

[pull] main from doocs:main #420

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 2 commits into from
Mar 17, 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
76 changes: 76 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,82 @@ function isBalanced(num: string): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn is_balanced(num: String) -> bool {
let mut f = [0; 2];
for (i, x) in num.as_bytes().iter().enumerate() {
f[i & 1] += (x - b'0') as i32;
}
f[0] == f[1]
}
}
```

#### JavaScript

```js
/**
* @param {string} num
* @return {boolean}
*/
var isBalanced = function (num) {
const f = [0, 0];
for (let i = 0; i < num.length; ++i) {
f[i & 1] += +num[i];
}
return f[0] === f[1];
};
```

#### C#

```cs
public class Solution {
public bool IsBalanced(string num) {
int[] f = new int[2];
for (int i = 0; i < num.Length; ++i) {
f[i & 1] += num[i] - '0';
}
return f[0] == f[1];
}
}
```

#### PHP

```php
class Solution {
/**
* @param String $num
* @return Boolean
*/
function isBalanced($num) {
$f = [0, 0];
foreach (str_split($num) as $i => $ch) {
$f[$i & 1] += ord($ch) - 48;
}
return $f[0] == $f[1];
}
}
```

#### Scala

```scala
object Solution {
def isBalanced(num: String): Boolean = {
val f = Array(0, 0)
for (i <- num.indices) {
f(i & 1) += num(i) - '0'
}
f(0) == f(1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
76 changes: 76 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,82 @@ function isBalanced(num: string): boolean {
}
```

#### Rust

```rust
impl Solution {
pub fn is_balanced(num: String) -> bool {
let mut f = [0; 2];
for (i, x) in num.as_bytes().iter().enumerate() {
f[i & 1] += (x - b'0') as i32;
}
f[0] == f[1]
}
}
```

#### JavaScript

```js
/**
* @param {string} num
* @return {boolean}
*/
var isBalanced = function (num) {
const f = [0, 0];
for (let i = 0; i < num.length; ++i) {
f[i & 1] += +num[i];
}
return f[0] === f[1];
};
```

#### C#

```cs
public class Solution {
public bool IsBalanced(string num) {
int[] f = new int[2];
for (int i = 0; i < num.Length; ++i) {
f[i & 1] += num[i] - '0';
}
return f[0] == f[1];
}
}
```

#### PHP

```php
class Solution {
/**
* @param String $num
* @return Boolean
*/
function isBalanced($num) {
$f = [0, 0];
foreach (str_split($num) as $i => $ch) {
$f[$i & 1] += ord($ch) - 48;
}
return $f[0] == $f[1];
}
}
```

#### Scala

```scala
object Solution {
def isBalanced(num: String): Boolean = {
val f = Array(0, 0)
for (i <- num.indices) {
f(i & 1) += num(i) - '0'
}
f(0) == f(1)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
9 changes: 9 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Solution {
public bool IsBalanced(string num) {
int[] f = new int[2];
for (int i = 0; i < num.Length; ++i) {
f[i & 1] += num[i] - '0';
}
return f[0] == f[1];
}
}
11 changes: 11 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/Solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {string} num
* @return {boolean}
*/
var isBalanced = function (num) {
const f = [0, 0];
for (let i = 0; i < num.length; ++i) {
f[i & 1] += +num[i];
}
return f[0] === f[1];
};
13 changes: 13 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
/**
* @param String $num
* @return Boolean
*/
function isBalanced($num) {
$f = [0, 0];
foreach (str_split($num) as $i => $ch) {
$f[$i & 1] += ord($ch) - 48;
}
return $f[0] == $f[1];
}
}
9 changes: 9 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
impl Solution {
pub fn is_balanced(num: String) -> bool {
let mut f = [0; 2];
for (i, x) in num.as_bytes().iter().enumerate() {
f[i & 1] += (x - b'0') as i32;
}
f[0] == f[1]
}
}
9 changes: 9 additions & 0 deletions solution/3300-3399/3340.Check Balanced String/Solution.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Solution {
def isBalanced(num: String): Boolean = {
val f = Array(0, 0)
for (i <- num.indices) {
f(i & 1) += num(i) - '0'
}
f(0) == f(1)
}
}
117 changes: 113 additions & 4 deletions solution/3400-3499/3483.Unique 3-Digit Even Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,141 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3483.Un

<!-- solution:start -->

### 方法一
### 方法一:哈希表 + 枚举

我们用一个哈希表 $\textit{s}$ 记录所有不同的三位偶数,然后枚举所有可能的三位偶数,将其加入哈希表中。

最后返回哈希表的大小即可。

时间复杂度 $O(n^3)$,空间复杂度 $O(n^3)$。其中 $n$ 为数组 $\textit{digits}$ 的长度。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def totalNumbers(self, digits: List[int]) -> int:
s = set()
for i, a in enumerate(digits):
if a & 1:
continue
for j, b in enumerate(digits):
if i == j:
continue
for k, c in enumerate(digits):
if c == 0 or k in (i, j):
continue
s.add(c * 100 + b * 10 + a)
return len(s)
```

#### Java

```java

class Solution {
public int totalNumbers(int[] digits) {
Set<Integer> s = new HashSet<>();
int n = digits.length;
for (int i = 0; i < n; ++i) {
if (digits[i] % 2 == 1) {
continue;
}
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
for (int k = 0; k < n; ++k) {
if (digits[k] == 0 || k == i || k == j) {
continue;
}
s.add(digits[k] * 100 + digits[j] * 10 + digits[i]);
}
}
}
return s.size();
}
}
```

#### C++

```cpp

class Solution {
public:
int totalNumbers(vector<int>& digits) {
unordered_set<int> s;
int n = digits.size();
for (int i = 0; i < n; ++i) {
if (digits[i] % 2 == 1) {
continue;
}
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
for (int k = 0; k < n; ++k) {
if (digits[k] == 0 || k == i || k == j) {
continue;
}
s.insert(digits[k] * 100 + digits[j] * 10 + digits[i]);
}
}
}
return s.size();
}
};
```

#### Go

```go
func totalNumbers(digits []int) int {
s := make(map[int]struct{})
for i, a := range digits {
if a%2 == 1 {
continue
}
for j, b := range digits {
if i == j {
continue
}
for k, c := range digits {
if c == 0 || k == i || k == j {
continue
}
s[c*100+b*10+a] = struct{}{}
}
}
}
return len(s)
}
```

#### TypeScript

```ts
function totalNumbers(digits: number[]): number {
const s = new Set<number>();
const n = digits.length;
for (let i = 0; i < n; ++i) {
if (digits[i] % 2 === 1) {
continue;
}
for (let j = 0; j < n; ++j) {
if (i === j) {
continue;
}
for (let k = 0; k < n; ++k) {
if (digits[k] === 0 || k === i || k === j) {
continue;
}
s.add(digits[k] * 100 + digits[j] * 10 + digits[i]);
}
}
}
return s.size;
}
```

<!-- tabs:end -->
Expand Down
Loading