current position：Home>leetcode 2057. Smallest Index With Equal Value（python）
leetcode 2057. Smallest Index With Equal Value（python）
20220131 02:05:46 【Wang Daya】
「 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 0indexed 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[0].
i=1: 1 mod 10 = 1 == nums[1].
i=2: 2 mod 10 = 2 == nums[2].
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[0].
i=1: 1 mod 10 = 1 != nums[1].
i=2: 2 mod 10 = 2 == nums[2].
i=3: 3 mod 10 = 3 != nums[3].
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 .
answer
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 builtin functions next , Just one line of code .
answer
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
Original link ：leetcode.com/problems/sm…
Your support is my greatest motivation
copyright notice
author[Wang Daya],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310205443990.html
The sidebar is recommended
 Python notes (20): built in highorder functions
 Python notes (17): closure
 Python notes (18): decorator
 Python notes (16): generators and iterators
 Python notes (XV): List derivation
 Python tells you what timing attacks are
 Python  file and exception
 [Python from introduction to mastery] (IV) what are the builtin data types of Python? Figure out
 Python code to scan code to pay attention to official account login
 [algorithm learning] 1221 Split balanced string (Java / C / C + + / Python / go / trust)
guess what you like

Python notes (22): errors and exceptions

Python has been hidden for ten years, and once image recognition is heard all over the world

Python notes (21): random number module

Python notes (19): anonymous functions

Use Python and OpenCV to calculate and draw twodimensional histogram

Python, Hough circle transformation in opencv

A library for reading and writing markdown in Python: mdutils

Datetime of Python time operation (Part I)

The most useful decorator in the python standard library

Python iterators and generators
Random recommended
 [Python from introduction to mastery] (V) Python's builtin data types  sequences and strings. They have no girlfriend, not a nanny, and can only be used as dry goods
 Does Python have a, = operator?
 Go through the string common sense in Python
 Fanwai 4 Handling of mouse events and solutions to common problems in Python opencv
 Summary of common functions for processing strings in Python
 When writing Python scripts, be sure to add this
 Python web crawler  Fundamentals (1)
 Pandas handles duplicate values
 Python notes (23): regular module
 Python crawlers are slow? Concurrent programming to understand it
 Parameter passing of Python function
 Stroke tuple in Python
 Talk about ordinary functions and higherorder functions in Python
 [Python data acquisition] page image crawling and saving
 [Python data collection] selenium automated test framework
 Talk about function passing and other supplements in Python
 Python programming simulation poker game
 leetcode 160. Intersection of Two Linked Lists （python）
 Python crawler actual combat, requests module, python to grab the beautiful wallpaper of a station
 Fanwai 5 Detailed description of slider in Python opencv and solutions to common problems
 My friend's stock suffered a terrible loss. When I was angry, I crawled the latest data of securities with Python
 Python interface automation testing framework  if you want to do well, you must first sharpen its tools
 Python multi thread crawling weather website pictures and saving
 How to convert pandas data to excel file
 Python series tutorials 122
 Python Complete Guide  printing data using pyspark
 Python Complete Guide  tuple conversion array
 Stroke the list in python (top)
 Analysis of Python requests module
 Comments and variables in Python
 New statement match, the new version of Python is finally going to introduce switch case?
 Fanwai 6 Different operations for image details in Python opencv
 Python crawler native code learning (I)
 Python quantitative data warehouse building series 2: Python operation database
 Python code reading (Part 50): taking elements from list intervals
 Pyechart + pandas made word cloud pictures of 19 report documents
 [Python crawler] multithreaded daemon & join() blocking
 Python crawls cat pictures in batches to realize thousand image imaging
 Van * Python  simple crawling of a planet
 Input and output of Python practice