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 problem: No.2057 #3837

Merged
merged 1 commit into from
Dec 4, 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
58 changes: 49 additions & 9 deletions solution/2000-2099/2057.Smallest Index With Equal Value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ i=3: 3 mod 10 = 3 != nums[3].

<!-- solution:start -->

### 方法一
### 方法一:遍历

我们直接遍历数组,对于每个下标 $i$,我们判断是否满足 $i \bmod 10 = \textit{nums}[i]$,如果满足则返回当前下标 $i$。

如果遍历完数组都没有找到满足条件的下标,则返回 $-1$。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -85,8 +91,8 @@ i=3: 3 mod 10 = 3 != nums[3].
```python
class Solution:
def smallestEqual(self, nums: List[int]) -> int:
for i, v in enumerate(nums):
if i % 10 == v:
for i, x in enumerate(nums):
if i % 10 == x:
return i
return -1
```
Expand All @@ -112,9 +118,11 @@ class Solution {
class Solution {
public:
int smallestEqual(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i)
if (i % 10 == nums[i])
for (int i = 0; i < nums.size(); ++i) {
if (i % 10 == nums[i]) {
return i;
}
}
return -1;
}
};
Expand All @@ -124,8 +132,8 @@ public:

```go
func smallestEqual(nums []int) int {
for i, v := range nums {
if i%10 == v {
for i, x := range nums {
if i%10 == x {
return i
}
}
Expand All @@ -137,13 +145,45 @@ func smallestEqual(nums []int) int {

```ts
function smallestEqual(nums: number[]): number {
for (let i = 0; i < nums.length; i++) {
if (i % 10 == nums[i]) return i;
for (let i = 0; i < nums.length; ++i) {
if (i % 10 === nums[i]) {
return i;
}
}
return -1;
}
```

#### Rust

```rust
impl Solution {
pub fn smallest_equal(nums: Vec<i32>) -> i32 {
for (i, &x) in nums.iter().enumerate() {
if i % 10 == x as usize {
return i as i32;
}
}
-1
}
}
```

#### Cangjie

```cj
class Solution {
func smallestEqual(nums: Array<Int64>): Int64 {
for (i in 0..nums.size) {
if (i % 10 == nums[i]) {
return i
}
}
-1
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [0,1,2]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
<strong>Explanation:</strong>
i=0: 0 mod 10 = 0 == nums[0].
i=1: 1 mod 10 = 1 == nums[1].
i=2: 2 mod 10 = 2 == nums[2].
Expand All @@ -40,7 +40,7 @@ All indices have i mod 10 == nums[i], so we return the smallest index 0.
<pre>
<strong>Input:</strong> nums = [4,3,2,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
<strong>Explanation:</strong>
i=0: 0 mod 10 = 0 != nums[0].
i=1: 1 mod 10 = 1 != nums[1].
i=2: 2 mod 10 = 2 == nums[2].
Expand Down Expand Up @@ -70,7 +70,13 @@ i=3: 3 mod 10 = 3 != nums[3].

<!-- solution:start -->

### Solution 1
### Solution 1: Traversal

We directly traverse the array. For each index $i$, we check if it satisfies $i \bmod 10 = \textit{nums}[i]$. If it does, we return the current index $i$.

If we traverse the entire array and do not find a satisfying index, we return $-1$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -79,8 +85,8 @@ i=3: 3 mod 10 = 3 != nums[3].
```python
class Solution:
def smallestEqual(self, nums: List[int]) -> int:
for i, v in enumerate(nums):
if i % 10 == v:
for i, x in enumerate(nums):
if i % 10 == x:
return i
return -1
```
Expand All @@ -106,9 +112,11 @@ class Solution {
class Solution {
public:
int smallestEqual(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i)
if (i % 10 == nums[i])
for (int i = 0; i < nums.size(); ++i) {
if (i % 10 == nums[i]) {
return i;
}
}
return -1;
}
};
Expand All @@ -118,8 +126,8 @@ public:

```go
func smallestEqual(nums []int) int {
for i, v := range nums {
if i%10 == v {
for i, x := range nums {
if i%10 == x {
return i
}
}
Expand All @@ -131,13 +139,45 @@ func smallestEqual(nums []int) int {

```ts
function smallestEqual(nums: number[]): number {
for (let i = 0; i < nums.length; i++) {
if (i % 10 == nums[i]) return i;
for (let i = 0; i < nums.length; ++i) {
if (i % 10 === nums[i]) {
return i;
}
}
return -1;
}
```

#### Rust

```rust
impl Solution {
pub fn smallest_equal(nums: Vec<i32>) -> i32 {
for (i, &x) in nums.iter().enumerate() {
if i % 10 == x as usize {
return i as i32;
}
}
-1
}
}
```

#### Cangjie

```cj
class Solution {
func smallestEqual(nums: Array<Int64>): Int64 {
for (i in 0..nums.size) {
if (i % 10 == nums[i]) {
return i
}
}
-1
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
func smallestEqual(nums: Array<Int64>): Int64 {
for (i in 0..nums.size) {
if (i % 10 == nums[i]) {
return i
}
}
-1
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Solution {
public:
int smallestEqual(vector<int>& nums) {
for (int i = 0; i < nums.size(); ++i)
if (i % 10 == nums[i])
for (int i = 0; i < nums.size(); ++i) {
if (i % 10 == nums[i]) {
return i;
}
}
return -1;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
func smallestEqual(nums []int) int {
for i, v := range nums {
if i%10 == v {
for i, x := range nums {
if i%10 == x {
return i
}
}
return -1
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Solution:
def smallestEqual(self, nums: List[int]) -> int:
for i, v in enumerate(nums):
if i % 10 == v:
for i, x in enumerate(nums):
if i % 10 == x:
return i
return -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
impl Solution {
pub fn smallest_equal(nums: Vec<i32>) -> i32 {
for (i, &x) in nums.iter().enumerate() {
if i % 10 == x as usize {
return i as i32;
}
}
-1
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
function smallestEqual(nums: number[]): number {
for (let i = 0; i < nums.length; i++) {
if (i % 10 == nums[i]) return i;
for (let i = 0; i < nums.length; ++i) {
if (i % 10 === nums[i]) {
return i;
}
}
return -1;
}
Loading