#702 Search in a Sorted Array of Unknown Size

题目:

This is an interactive problem.

You have a sorted array of unique elements and an unknown size. You do not have an access to the array but you can use the ArrayReader interface to access it. You can call ArrayReader.get(i) that:

  • returns the value at the ith index (0-indexed) of the secret array (i.e., secret[i]), or
  • returns 231 - 1 if the i is out of the boundary of the array.

You are also given an integer target.

Return the index k of the hidden array where secret[k] == target or return -1 otherwise.

You must write an algorithm with O(log n) runtime complexity.

解题思路:

因为不知道array的length,想了很久也不知道怎么求出右边界。没办法只能看一下solution。

solution的思路是,右边界从1开始,左边界从0开始。然后对比右边界的值和目标值的大小,如果前者小,那么要扩界。因为要采用二分法,所以右边界的index开始翻倍。一旦右边界的值大于目标值,则确立range。用回传统老办法找target。

# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
#    def get(self, index: int) -> int:class Solution:def search(self, reader: 'ArrayReader', target: int) -> int:# make sure the secret array has valid index 1if reader.get(1) is not (2**31 - 1):left, right = 0, 1else:if reader.get(0) == target:return 0else:return -1# make sure the target is within a certain rangewhile reader.get(right) < target:left = rightright <<= 1# find the index of the target if it exists otherwise return -1while left<=right:mid = left+((right-left)>>1)if reader.get(mid) < target:left = mid + 1elif reader.get(mid) > target:right = mid - 1else:return midreturn -1

runtime:

看了其他更快的solution解法思路是一样的,所以就不参考了。

ps有个小小的疑惑,为什么sample solution第一步没有排除length可能为1的情况。

后来想明白了,如果length为1那么值直接是2^31-1。直接以这个值为界。

Apr 1st再做了一次。复习巩固一下skill。(翻了一下记录,第一次做是Mar 20th)

思路有些不同。不再确认idx为1时有无值,发现不重要。

# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
#class ArrayReader:
#    def get(self, index: int) -> int:class Solution:def search(self, reader: 'ArrayReader', target: int) -> int:l, r = 0, 1while True:if reader.get(r) == target:return rif reader.get(r) > target:while l <= r:m = (l+r)//2if reader.get(m) > target:r = m - 1elif reader.get(m) < target:l = m + 1else:return mreturn -1else:l = r + 1r = 2*l

左右标依然逐轮放大,直到右标值大于target为止。若等,则输出右标。这个因为是看选定范围内有无target,所以用的是 template I 的方法。

runtime:

算是有一点点进步:)

Leetcode日练笔记8 #702 Search in a Sorted Array of Unknown Size (Medium)相关推荐

  1. 【Lintcode】1522. Search in a Sorted Array of Unknown Size

    题目地址: https://www.lintcode.com/problem/search-in-a-sorted-array-of-unknown-size/description 给定一个长度非常 ...

  2. LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  3. Leetcode日练笔记19 #487 Max Consecutive Ones II (Medium)

    #487 Max Consecutive Ones II (Medium) Given a binary array nums, return the maximum number of consec ...

  4. Leetcode日练笔记31 #19 #206 # 203 Rmv Nth Node From End Reverse Linked List Rmv Linked List Elements

    Given the head of a linked list, remove the nth node from the end of the list and return its head. E ...

  5. LeetCode 33. Search in Rotated Sorted Array

    问题链接 LeetCode 33. Search in Rotated Sorted Array 题目解析 给定一个 "升序" 的 无重复 数组,从中寻找目标值."升序& ...

  6. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  7. LeetCode Search in Rotated Sorted Array II

     Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...

  8. LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  9. leetcode 81 Search in Rotated Sorted Array II ----- java

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

最新文章

  1. HOG 特征计算实现
  2. 【自由随想录(一)】
  3. 物联网、云计算、大数据、人工智能之间有怎样的联系和区别?
  4. python爬虫拉取豆瓣Top250数据
  5. ncbi查找目的基因序列_NCBI大搜索之目的基因寻踪
  6. linux 软件部署工具下载,linux配置 yum管理应用软件 、 快速部署Web/FTP
  7. php xml 留言板,php xml留言板 xml存储数据的简单例子
  8. 破解校园数字安全难点,联想推出智慧教育安全体系
  9. 红米Note8手机图纸-电路原理图+主板元件位号图
  10. 各种梯度算法总结 + Total Variation
  11. 连续时间正弦信号和离散时间正弦信号
  12. 1--request模块
  13. Python基础07
  14. LeGO-LOAM资料整理
  15. 云科技时代力作:《读懂新基建,数字技术带来全民机遇》上市
  16. LPC1768 SSP0驱动示例——SD卡驱动
  17. 八斗大数据20期冲击百万年薪完结分享
  18. Git 中 git restore 的使用
  19. HTML网页一键变黑白色调
  20. D75 LeetCode 812.最大三角形面积(简单)

热门文章

  1. android go 联发科 p10,联发科P10和高通骁龙435哪个性能强?联发科P10对比高通骁龙435评测...
  2. 【程序员的财富自由之路】开篇序
  3. python画大耳朵图图_python之禅
  4. 不可以涩涩!AI续写软件初体验;迁移学习路线图;谷歌新闻非官方搜索API;CS295『因果推理』2021课程资料;前沿论文 | ShowMeAI资讯日报
  5. flash 怎么擦掉fpga_基于FPGA的flash板卡程序擦除与固化
  6. 想精通 Python 数据挖掘?清华博士带你入门!
  7. python大鱼吃小鱼_写简单游戏,学编程语言-python篇:大鱼吃小鱼
  8. Bean重名报错 解决方案
  9. SmartGit软甲提交代码时报: Commit: Not all refs have been pushed.的解决方法
  10. 飞凌嵌入式AM62x核心板,赋能新一代HMI