diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/README.md b/solution/2000-2099/2057.Smallest Index With Equal Value/README.md index 2387346f8734c..97511b03a1d77 100644 --- a/solution/2000-2099/2057.Smallest Index With Equal Value/README.md +++ b/solution/2000-2099/2057.Smallest Index With Equal Value/README.md @@ -76,7 +76,13 @@ i=3: 3 mod 10 = 3 != nums[3]. -### 方法一 +### 方法一:遍历 + +我们直接遍历数组,对于每个下标 $i$,我们判断是否满足 $i \bmod 10 = \textit{nums}[i]$,如果满足则返回当前下标 $i$。 + +如果遍历完数组都没有找到满足条件的下标,则返回 $-1$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 @@ -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 ``` @@ -112,9 +118,11 @@ class Solution { class Solution { public: int smallestEqual(vector& 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; } }; @@ -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 } } @@ -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 { + 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 { + for (i in 0..nums.size) { + if (i % 10 == nums[i]) { + return i + } + } + -1 + } +} +``` + diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/README_EN.md b/solution/2000-2099/2057.Smallest Index With Equal Value/README_EN.md index c4179432ef8da..fe73c89872a6d 100644 --- a/solution/2000-2099/2057.Smallest Index With Equal Value/README_EN.md +++ b/solution/2000-2099/2057.Smallest Index With Equal Value/README_EN.md @@ -28,7 +28,7 @@ tags:
 Input: nums = [0,1,2]
 Output: 0
-Explanation: 
+Explanation:
 i=0: 0 mod 10 = 0 == nums[0].
 i=1: 1 mod 10 = 1 == nums[1].
 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.
 
 Input: nums = [4,3,2,1]
 Output: 2
-Explanation: 
+Explanation:
 i=0: 0 mod 10 = 0 != nums[0].
 i=1: 1 mod 10 = 1 != nums[1].
 i=2: 2 mod 10 = 2 == nums[2].
@@ -70,7 +70,13 @@ i=3: 3 mod 10 = 3 != nums[3].
 
 
 
-### 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)$.
 
 
 
@@ -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
 ```
@@ -106,9 +112,11 @@ class Solution {
 class Solution {
 public:
     int smallestEqual(vector& 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;
     }
 };
@@ -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
 		}
 	}
@@ -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 {
+        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 {
+        for (i in 0..nums.size) {
+            if (i % 10 == nums[i]) {
+                return i
+            }
+        }
+        -1
+    }
+}
+```
+
 
 
 
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cj b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cj
new file mode 100644
index 0000000000000..e69c846ae086d
--- /dev/null
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cj	
@@ -0,0 +1,10 @@
+class Solution {
+    func smallestEqual(nums: Array): Int64 {
+        for (i in 0..nums.size) {
+            if (i % 10 == nums[i]) {
+                return i
+            }
+        }
+        -1
+    }
+}
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cpp b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cpp
index beb1a32a49953..333d06e058e84 100644
--- a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cpp	
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.cpp	
@@ -1,9 +1,11 @@
 class Solution {
 public:
     int smallestEqual(vector& 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;
     }
-};
\ No newline at end of file
+};
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.go b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.go
index 27c65c9875820..bd2991f607fd2 100644
--- a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.go	
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.go	
@@ -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
-}
\ No newline at end of file
+}
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.py b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.py
index db36a1c10185d..09b16d3a78633 100644
--- a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.py	
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.py	
@@ -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
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.rs b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.rs
new file mode 100644
index 0000000000000..4e0990c821dfc
--- /dev/null
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.rs	
@@ -0,0 +1,10 @@
+impl Solution {
+    pub fn smallest_equal(nums: Vec) -> i32 {
+        for (i, &x) in nums.iter().enumerate() {
+            if i % 10 == x as usize {
+                return i as i32;
+            }
+        }
+        -1
+    }
+}
diff --git a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.ts b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.ts
index 4e260646d540a..49d6f6a6a47e2 100644
--- a/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.ts	
+++ b/solution/2000-2099/2057.Smallest Index With Equal Value/Solution.ts	
@@ -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;
 }