You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2000-2099/2057.Smallest Index With Equal Value/README_EN.md
+51-11
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ tags:
28
28
<pre>
29
29
<strong>Input:</strong> nums = [0,1,2]
30
30
<strong>Output:</strong> 0
31
-
<strong>Explanation:</strong>
31
+
<strong>Explanation:</strong>
32
32
i=0: 0 mod 10 = 0 == nums[0].
33
33
i=1: 1 mod 10 = 1 == nums[1].
34
34
i=2: 2 mod 10 = 2 == nums[2].
@@ -40,7 +40,7 @@ All indices have i mod 10 == nums[i], so we return the smallest index 0.
40
40
<pre>
41
41
<strong>Input:</strong> nums = [4,3,2,1]
42
42
<strong>Output:</strong> 2
43
-
<strong>Explanation:</strong>
43
+
<strong>Explanation:</strong>
44
44
i=0: 0 mod 10 = 0 != nums[0].
45
45
i=1: 1 mod 10 = 1 != nums[1].
46
46
i=2: 2 mod 10 = 2 == nums[2].
@@ -70,7 +70,13 @@ i=3: 3 mod 10 = 3 != nums[3].
70
70
71
71
<!-- solution:start -->
72
72
73
-
### Solution 1
73
+
### Solution 1: Traversal
74
+
75
+
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$.
76
+
77
+
If we traverse the entire array and do not find a satisfying index, we return $-1$.
78
+
79
+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
74
80
75
81
<!-- tabs:start -->
76
82
@@ -79,8 +85,8 @@ i=3: 3 mod 10 = 3 != nums[3].
79
85
```python
80
86
classSolution:
81
87
defsmallestEqual(self, nums: List[int]) -> int:
82
-
for i, vinenumerate(nums):
83
-
if i %10==v:
88
+
for i, xinenumerate(nums):
89
+
if i %10==x:
84
90
return i
85
91
return-1
86
92
```
@@ -106,9 +112,11 @@ class Solution {
106
112
classSolution {
107
113
public:
108
114
int smallestEqual(vector<int>& nums) {
109
-
for (int i = 0; i < nums.size(); ++i)
110
-
if (i % 10 == nums[i])
115
+
for (int i = 0; i < nums.size(); ++i) {
116
+
if (i % 10 == nums[i]) {
111
117
return i;
118
+
}
119
+
}
112
120
return -1;
113
121
}
114
122
};
@@ -118,8 +126,8 @@ public:
118
126
119
127
```go
120
128
func smallestEqual(nums []int) int {
121
-
for i, v := range nums {
122
-
if i%10 == v {
129
+
for i, x := range nums {
130
+
if i%10 == x {
123
131
return i
124
132
}
125
133
}
@@ -131,13 +139,45 @@ func smallestEqual(nums []int) int {
0 commit comments