PIQ59: Find the missing number in the increasing sequence
来源:互联网 发布:微信发长视频软件 编辑:程序博客网 时间:2024/05/21 09:31
Problem Statement
Given an increasing sequence of numbers from 1 to n with only one missing number, how can you find that missing number without traversing the sequence in linear fashion. In other words, time complexity of your algorithm must be less than O(n)?
Algorithm: Binary Search
二分查找算法: 二分查找算法是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜 素过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组 为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半。折半搜索每次把搜索区域减少一半,时间复杂度为Ο(logn) 。
Solution
Find the first misplaced element m
, then m - 1
is the missing number. The solution is a modified version of binary search. Note that when update the right
pointer, it should be updated to mid
pointer, instead of mid - 1
.
def missing_element(nums, n): """Find the missing number. Time Complexity: O(logn) """ if n == 1: return 0 left, right = 0, n - 2 while left < right: mid = (left + right) >> 1 if mid == nums[mid] - 1: left = mid + 1 else: right = mid return nums[left] - 1
0 0
- PIQ59: Find the missing number in the increasing sequence
- *[Lintcode]Find the Missing Number
- Q5.7 Find the missing number
- C/C++ Find the missing number
- lintcode Find the Missing Number II
- lintcode-196. Find the Missing Number
- Increasing the maximum number of tcp/ip connections in linux
- Find the missing numbers
- Find the missing letter
- LintCode --find-the-missing-number(寻找缺失的数)
- Find the smallest positive number missing from an unsorted array
- python求解LeetCode习题Find the First Missing Positive Number
- InnoDB: The log sequence number in ibdatafiles does not match the log sequence number in the ib_log
- O(n lg n)-time Algorithm to Find the Longest Monotonically Increasing Sub-sequence
- Find the Prime Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- 口才艺术与社交礼仪
- Java.util.date 与 java.sql.date区别和转换
- 忘记数据库密码怎么办?
- iPhone游戏开发:使用到的工具和技术
- jaxb 解析多列数据的xml 映射成list
- PIQ59: Find the missing number in the increasing sequence
- 文字换行
- 初始化方法详细
- EasyUI学习第三篇:Form表单
- hdu 3657 Game【最大权独立点集------最大流最小割Dinic】
- Android 中 ListView 的 下拉刷新 和 上拉加载 的 重点及学习(一)
- NYOJ 499.迷宫(深搜)
- c++关于线性表的基本操作
- 深入理解计算机系统 笔记(二)