# leetcode 2057. Smallest Index With Equal Value（python）

2022-01-31 02:05:46

「 This is my participation 11 The fourth of the yuegengwen challenge 4 God , Check out the activity details ：2021 One last more challenge

### describe

Given a 0-indexed integer array nums, return the smallest index i of nums such that i mod 10 == nums[i], or -1 if such index does not exist.

x mod y denotes the remainder when x is divided by y.

Example 1:

``````Input: nums = [0,1,2]
Output: 0
Explanation:
i=0: 0 mod 10 = 0 == nums.
i=1: 1 mod 10 = 1 == nums.
i=2: 2 mod 10 = 2 == nums.
All indices have i mod 10 == nums[i], so we return the smallest index 0.
Copy code ``````

Example 2:

``````Input: nums = [4,3,2,1]
Output: 2
Explanation:
i=0: 0 mod 10 = 0 != nums.
i=1: 1 mod 10 = 1 != nums.
i=2: 2 mod 10 = 2 == nums.
i=3: 3 mod 10 = 3 != nums.
2 is the only index which has i mod 10 == nums[i].
Copy code ``````

Example 3:

``````Input: nums = [1,2,3,4,5,6,7,8,9,0]
Output: -1
Explanation: No index satisfies i mod 10 == nums[i].
Copy code ``````

Example 4:

``````Input: nums = [2,1,3,5,2]
Output: 1
Explanation: 1 is the only index with i mod 10 == nums[i].
Copy code ``````

Note:

``````1 <= nums.length <= 100
0 <= nums[i] <= 9
Copy code ``````

### analysis

According to the meaning , Is to give out one from 0 List of integers to start indexing nums , The title requires us to return the smallest index i , send i mod 10 == nums[i] establish , If there is no index that matches the meaning of the question , Then go straight back -1 .

x mod y Express x Divide y The remainder of .

It seems very difficult , It's actually a simple question , It's traversal range(len(nums)) Every index i , If i%10 == nums[i] Go straight back to i , Otherwise, the traversal ends and returns directly to -1 .

``````class Solution(object):
def smallestEqual(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(len(nums)):
if i%10 == nums[i]:
return i
return -1

Copy code ``````

### Running results

``````Runtime: 60 ms, faster than 100.00% of Python online submissions for Smallest Index With Equal Value.
Memory Usage: 13.5 MB, less than 100.00% of Python online submissions for Smallest Index With Equal Value.
Copy code ``````

### analysis

You can also use built-in functions next , Just one line of code .

``````class Solution(object):
def smallestEqual(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return next((i for i, x in enumerate(nums) if i%10 == x), -1)
Copy code ``````

### Running results

``````Runtime: 52 ms, faster than 100.00% of Python online submissions for Smallest Index With Equal Value.
Memory Usage: 13.4 MB, less than 100.00% of Python online submissions for Smallest Index With Equal Value.
Copy code ``````