Skip to content

feat: add solutions to lc problems: No.1295,1299 #3629

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
Oct 10, 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 @@ -41,7 +41,7 @@ tags:

<pre>
<strong>输入:</strong>nums = [555,901,482,1771]
<strong>输出:</strong>1
<strong>输出:</strong>1
<strong>解释: </strong>
只有 1771 是位数为偶数的数字。
</pre>
Expand All @@ -61,11 +61,13 @@ tags:

<!-- solution:start -->

### 方法一:枚举
### 方法一:模拟

枚举数组 `nums` 中的每个元素,将其转换为字符串,判断字符串长度是否为偶数,是则答案加一
我们遍历数组 $\textit{nums}$ 中的每个元素,对于当前遍历到的元素 $x$,我们直接将其转换为字符串,然后判断其长度是否为偶数即可。若是则将答案加一

时间复杂度 $O(n \times \log_{10} m)$,空间复杂度 $O(\log_{10} m)$,其中 $n$ 和 $m$ 分别为数组 `nums` 的长度以及数组 `nums` 中的最大元素。
遍历结束后,我们返回答案即可。

时间复杂度 $O(n \times \log M)$,空间复杂度 $O(\log M)$。其中 $n$ 是数组 $\textit{nums}$ 的长度,而 $M$ 是数组 $\textit{nums}$ 中的元素的最大值。

<!-- tabs:start -->

Expand All @@ -74,7 +76,7 @@ tags:
```python
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(v)) % 2 == 0 for v in nums)
return sum(len(str(x)) % 2 == 0 for x in nums)
```

#### Java
Expand All @@ -83,8 +85,8 @@ class Solution:
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int v : nums) {
if (String.valueOf(v).length() % 2 == 0) {
for (int x : nums) {
if (String.valueOf(x).length() % 2 == 0) {
++ans;
}
}
Expand All @@ -100,8 +102,8 @@ class Solution {
public:
int findNumbers(vector<int>& nums) {
int ans = 0;
for (int& v : nums) {
ans += to_string(v).size() % 2 == 0;
for (int& x : nums) {
ans += to_string(x).size() % 2 == 0;
}
return ans;
}
Expand All @@ -112,15 +114,23 @@ public:

```go
func findNumbers(nums []int) (ans int) {
for _, v := range nums {
if len(strconv.Itoa(v))%2 == 0 {
for _, x := range nums {
if len(strconv.Itoa(x))%2 == 0 {
ans++
}
}
return
}
```

#### TypeScript

```ts
function findNumbers(nums: number[]): number {
return nums.filter(x => x.toString().length % 2 === 0).length;
}
```

#### JavaScript

```js
Expand All @@ -129,11 +139,7 @@ func findNumbers(nums []int) (ans int) {
* @return {number}
*/
var findNumbers = function (nums) {
let ans = 0;
for (const v of nums) {
ans += String(v).length % 2 == 0;
}
return ans;
return nums.filter(x => x.toString().length % 2 === 0).length;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [12,345,2,6,7896]
<strong>Output:</strong> 2
<strong>Explanation:
<strong>Explanation:
</strong>12 contains 2 digits (even number of digits).&nbsp;
345 contains 3 digits (odd number of digits).&nbsp;
2 contains 1 digit (odd number of digits).&nbsp;
Expand All @@ -40,7 +40,7 @@ Therefore only 12 and 7896 contain an even number of digits.

<pre>
<strong>Input:</strong> nums = [555,901,482,1771]
<strong>Output:</strong> 1
<strong>Output:</strong> 1
<strong>Explanation: </strong>
Only 1771 contains an even number of digits.
</pre>
Expand All @@ -59,7 +59,13 @@ Only 1771 contains an even number of digits.

<!-- solution:start -->

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

We traverse each element $x$ in the array $\textit{nums}$. For the current element $x$, we directly convert it to a string and then check if its length is even. If it is, we increment the answer by one.

After the traversal is complete, we return the answer.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(\log M)$. Here, $n$ is the length of the array $\textit{nums}$, and $M$ is the maximum value of the elements in the array $\textit{nums}$.

<!-- tabs:start -->

Expand All @@ -68,7 +74,7 @@ Only 1771 contains an even number of digits.
```python
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(v)) % 2 == 0 for v in nums)
return sum(len(str(x)) % 2 == 0 for x in nums)
```

#### Java
Expand All @@ -77,8 +83,8 @@ class Solution:
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int v : nums) {
if (String.valueOf(v).length() % 2 == 0) {
for (int x : nums) {
if (String.valueOf(x).length() % 2 == 0) {
++ans;
}
}
Expand All @@ -94,8 +100,8 @@ class Solution {
public:
int findNumbers(vector<int>& nums) {
int ans = 0;
for (int& v : nums) {
ans += to_string(v).size() % 2 == 0;
for (int& x : nums) {
ans += to_string(x).size() % 2 == 0;
}
return ans;
}
Expand All @@ -106,15 +112,23 @@ public:

```go
func findNumbers(nums []int) (ans int) {
for _, v := range nums {
if len(strconv.Itoa(v))%2 == 0 {
for _, x := range nums {
if len(strconv.Itoa(x))%2 == 0 {
ans++
}
}
return
}
```

#### TypeScript

```ts
function findNumbers(nums: number[]): number {
return nums.filter(x => x.toString().length % 2 === 0).length;
}
```

#### JavaScript

```js
Expand All @@ -123,11 +137,7 @@ func findNumbers(nums []int) (ans int) {
* @return {number}
*/
var findNumbers = function (nums) {
let ans = 0;
for (const v of nums) {
ans += String(v).length % 2 == 0;
}
return ans;
return nums.filter(x => x.toString().length % 2 === 0).length;
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class Solution {
public:
int findNumbers(vector<int>& nums) {
int ans = 0;
for (int& v : nums) {
ans += to_string(v).size() % 2 == 0;
for (int& x : nums) {
ans += to_string(x).size() % 2 == 0;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
func findNumbers(nums []int) (ans int) {
for _, v := range nums {
if len(strconv.Itoa(v))%2 == 0 {
for _, x := range nums {
if len(strconv.Itoa(x))%2 == 0 {
ans++
}
}
return
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Solution {
public int findNumbers(int[] nums) {
int ans = 0;
for (int v : nums) {
if (String.valueOf(v).length() % 2 == 0) {
for (int x : nums) {
if (String.valueOf(x).length() % 2 == 0) {
++ans;
}
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
* @return {number}
*/
var findNumbers = function (nums) {
let ans = 0;
for (const v of nums) {
ans += String(v).length % 2 == 0;
}
return ans;
return nums.filter(x => x.toString().length % 2 === 0).length;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Solution:
def findNumbers(self, nums: List[int]) -> int:
return sum(len(str(v)) % 2 == 0 for v in nums)
return sum(len(str(x)) % 2 == 0 for x in nums)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function findNumbers(nums: number[]): number {
return nums.filter(x => x.toString().length % 2 === 0).length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ tags:

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

根据题目描述,如果一个长串满足条件,那么这个长串的子串(长度至少为 `minSize`)也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 `minSize` 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。
根据题目描述,如果一个长串满足条件,那么这个长串的长度为 $\textit{minSize}$ 的子串也一定满足条件。因此,我们只需要枚举 $s$ 中所有长度为 $\textit{minSize}$ 的子串,然后利用哈希表记录所有子串的出现次数,找出最大的次数作为答案即可。

时间复杂度 $O(n \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 `minSize` 的大小。本题中 $m$ 不超过 $26$。
时间复杂度 $O(n \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度以及 $\textit{minSize}$ 的大小。本题中 $m$ 不超过 $26$。

<!-- tabs:start -->

Expand Down Expand Up @@ -162,6 +162,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
}
```

#### TypeScript

```ts
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
const cnt = new Map<string, number>();
let ans = 0;
for (let i = 0; i < s.length - minSize + 1; ++i) {
const t = s.slice(i, i + minSize);
const ss = new Set(t.split(''));
if (ss.size <= maxLetters) {
cnt.set(t, (cnt.get(t) || 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ It satisfies the conditions, 2 unique letters and size 3 (between minSize and ma

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + Enumeration

According to the problem description, if a long string meets the condition, then its substring of length $\textit{minSize}$ must also meet the condition. Therefore, we only need to enumerate all substrings of length $\textit{minSize}$ in $s$, then use a hash table to record the occurrence frequency of all substrings, and find the maximum frequency as the answer.

The time complexity is $O(n \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the lengths of the string $s$ and $\textit{minSize}$, respectively. In this problem, $m$ does not exceed $26$.

<!-- tabs:start -->

Expand Down Expand Up @@ -146,6 +150,24 @@ func maxFreq(s string, maxLetters int, minSize int, maxSize int) (ans int) {
}
```

#### TypeScript

```ts
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
const cnt = new Map<string, number>();
let ans = 0;
for (let i = 0; i < s.length - minSize + 1; ++i) {
const t = s.slice(i, i + minSize);
const ss = new Set(t.split(''));
if (ss.size <= maxLetters) {
cnt.set(t, (cnt.get(t) || 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
const cnt = new Map<string, number>();
let ans = 0;
for (let i = 0; i < s.length - minSize + 1; ++i) {
const t = s.slice(i, i + minSize);
const ss = new Set(t.split(''));
if (ss.size <= maxLetters) {
cnt.set(t, (cnt.get(t) || 0) + 1);
ans = Math.max(ans, cnt.get(t)!);
}
}
return ans;
}
Loading
Loading