It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.

Apparently, many engineers take measures on the site called LetCode.

It is a site that trains the algorithmic power that can withstand the coding test that is being done in the early story, and it is an inevitable path for those who want to build a career at an overseas tech company.

I wrote it in a big way, but I have no plans to have such an interview at the moment.

However, as an IT engineer, it would be better to have the same level of algorithm power as a person, so I would like to solve the problem irregularly and write down the method I thought at that time as a memo.

I'm solving it with Python3.

Leet Code Table of Contents Starting from Zero

Last time Leet Code Day85 starting from zero "6. ZigZag Conversion"

Twitter I'm doing it.

** Technical Blog Started! !! ** ** I think the technology will write about LetCode, Django, Nuxt, and so on. ** This is faster to update **, so please bookmark it!

33. Search in Rotated Sorted Array

The difficulty level is Medium.
The problem is, let's say you rotate an array sorted in ascending order with an unknown pivot (`[0,1,2,4,5,6,7]`

is `[4,5,6,7]. , 0,1,2] `

may be.)
(For example, `[0,1,2,4,5,6,7]`

may become `[4,5,6,7,0,1,2]`

.)
The value to be searched is given. If found in the array, it returns its index, otherwise it returns `-1`

.
You can assume that there are no duplicates in the array.
Note that the run-time complexity of the algorithm must be on the order of ʻO (log n)`.

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1

```
class Solution:
def search(self, nums: List[int], target: int) -> int:
if nums.count(target) == 0:
return -1
else:
ans = nums.index(target)
return ans
# Runtime: 36 ms, faster than 92.59% of Python3 online submissions for Search in Rotated Sorted Array.
# Memory Usage: 14 MB, less than 61.16% of Python3 online submissions for Search in Rotated Sorted Array.
```

If `count`

is` 0`

, it returns `-1`

, otherwise it returns ʻindex`.

・ ・ ・ ~~ I can't write! !! !! ~~

So that's it for this time. Thank you for your hard work.

Recommended Posts