LeetCode Algorithm 面试题 10.05. 稀疏数组搜索
生活随笔
收集整理的這篇文章主要介紹了
LeetCode Algorithm 面试题 10.05. 稀疏数组搜索
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
面試題 10.05. 稀疏數組搜索
Ideas
沒啥好說的,二分搜索就完了,注意過濾空字符串。
Code
Python
from typing import Listclass Solution:def findString(self, words: List[str], s: str) -> int:left, right = 0, len(words) - 1while left < right + 1:# 過濾空字符串while left < right + 1 and not words[left]:left += 1while left < right + 1 and not words[right]:right -= 1if left > right:return -1middle = ((right - left) >> 1) + leftwhile middle < right and not words[middle]:middle += 1if words[middle] == s:return middleelif words[middle] > s:right = middle - 1elif words[middle] < s:left = middle + 1return -1總結
以上是生活随笔為你收集整理的LeetCode Algorithm 面试题 10.05. 稀疏数组搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019年第十届蓝桥杯 - 省赛 - C
- 下一篇: PyTorch 加载超大 Libsvm