
[英]Why do I spend more time with binary search than with linear search?
我对进行二进制搜索的代码有疑问。 据我所知,二进制搜索比线性搜索更快。 我在 leetcode 上有一个任务,我必须在其中执行二分搜索,但我收到一个超出时间限制 (TLE) 的错误 当我进行线性搜索时,我没有收到错误 ...
[英]Why do I spend more time with binary search than with linear search?
我对进行二进制搜索的代码有疑问。 据我所知,二进制搜索比线性搜索更快。 我在 leetcode 上有一个任务,我必须在其中执行二分搜索,但我收到一个超出时间限制 (TLE) 的错误 当我进行线性搜索时,我没有收到错误 ...
[英]Reduce execution time of a code that uses binary search
问题是根据其他 2 个 arrays 创建玩家排名数组:排行榜和玩家分数。 此处问题的更多解释: https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem 。 下面的代码是一个意大利面条,但它工作正常。 但是,对于大 ...
[英]Using bisect functions with a key
在将bisect_left与key参数一起使用时,我注意到键 function 未应用于搜索值。 例如,我想在起始值大于给定数字的排序范围列表中找到第一个范围: bisect_left上的文档如下: key指定一个参数的键 function ,用于从数组中的每个元素中提取比较键。 为了支持搜索复 ...
[英]Find a specific element in an array of Booleans while using the least amount of resources C#
我有一个假设的布尔数组,其中包含 50 个元素。 这些布尔值的值对我来说是未知的,找出特定元素值的唯一方法是调用一个方法并为其提供元素的索引。 这种方法在 CPU 周期方面成本很高,我想做的是编写一个代码,使用最少的调用此方法来确定特定元素的值。 如果此数组中有一个值为假的元素,则它之前的所有元素 ...
[英]Binary search when solution is at boundaries?
使用二进制搜索算法时,每次迭代都会更新两个指针之一。 但是,在某些情况下,如 LeetCode 问题,这会错过解决方案。 例如,threeSumClosest 的以下解决方案有效class Solution: def threeSumClosest(self, nums: List[int ...
[英]Binary Search with Duplicates producing wrong result at unknown key and list
我正在尝试解决二进制搜索问题,其中 output 是排序列表中键首次出现的位置。 我设法解决了第一部分,我通过使用二进制搜索找到了键出现在排序列表中的任何位置。 但是第二部分,也就是它第一次出现的地方,对于一个未知的键和排序列表来说是错误的。 二分查找重复项的代码: 我试图通过使用线性搜索对代码进行 ...
[英]Binary search loop is not breaking when the search element input is not prsenting in the sorted array
我正在 C 中实现二进制搜索,如下所示,但是当我传递排序数组中不可用的元素时,代码进入无限循环,因为中间元素重复计算相同的值(中间元素为 4,并且然后开始索引变为 5 (elementtofind > InputArray[midelement]),结束索引为 4,因此在循环的第 9 次迭代后 ...
[英]How do we identify when the answer will be at lower bound, upper bound and at mid in a binary search?
我刚开始学习二分搜索我知道当我们找到一些确切的元素时我们会返回 mid 但在一些更高级的问题中,我们必须返回下限和上限,我该如何识别? 什么时候归还什么,我变得非常困惑。 ...
[英]Binary search with a list containing string arrays won't work
我已经制作了一个二进制搜索方法,但是当我尝试使用它时,我收到消息“无法将类型 int 隐式转换为字符串 []”。 这是我的二进制搜索方法: 这是我在 main 方法中开始编码的内容: 当我将 binarySearchInput 作为第二个参数时,我收到错误消息:“无法将类型 int 隐式转换为字符串 ...
[英]Binary search with comparer is faster than without
我有一个包含大约 200 万条记录的数据。 我试图找到最接近给定时间范围的单个数据。 数据列表是有序的,数据由以下class表示: public class DataPoint { public long OpenTimeTs; } 我已经实施了 3 种方法来 ...
[英]Searching a string by binary search
#include <stdio.h> #include <string.h> typedef struct { char name[11]; int score; } report; int main() { int n = 3; repo ...
[英]Inconsistency in size of array cells
我正在做一个学校项目,在 C 代码中实现一些排序算法,我正在对一些通用数据类型 arrays 进行二进制插入排序(所以我使用void*项目和void**数组)。 我有一个binarySearch function 返回索引,我必须将一个项目插入数组以保留其元素的顺序(根据我传递给排序函数的给定 fu ...
[英]how to do binary search on array which is the numbers on even indexes are ascending and the numbers on odd indexes are descending
如何对数组进行二进制搜索,偶数索引上的数字是升序的,奇数索引上的数字是降序的,例如数组 {-3,10,0,9,5,0,7,-1} 我想找一个数字:x=5 我想我应该单独对偶数索引和奇数索引进行二进制搜索 ...
[英]Why there is no shell sort with binary insertion?
Shell sort总的来说是改进的insertion sort 。 insertion sort的另一个改进是binary insertion sort 。 为什么没有 shell 二进制插入排序? 可以做到,编码起来应该也不是很困难。 (我同意让索引正确可能需要 2-3 天) ...
[英]Getting "none type" error while using recursive function
这是我写的程序: count_occur()调用first_occur()以获取元素的第一次出现。 然后count_occur()调用last_occur()得到最后一次出现然后减去第一次和最后一次出现的索引,得到出现的次数。 这是我收到的 output。 它应该包括元素在列表中出现的次数。 但 ...
[英]Which time-complexity is better O(logM + logN) or O(logM.logN)?
我在二维矩阵上尝试了二进制搜索,我得到了时间复杂度为 O(logN.logM) 的解决方案。 已经存在一个在 log(MN) 时间内执行它的解决方案。 我只想知道哪个更好或更短。 真是个棘手的问题。 ...
[英]How can I return the index of mid in my recursive binary search program?
我无法弄清楚如何获取 mid 的索引,因为我在每次调用中都在操作数组。 function 的参数是固定的,我不能给 function 的第一个和最后一个参数。我尝试使用迭代方法,我得到了,但我不能这样做。 我只希望逻辑在代码中获取 mid 的索引。 ...
[英]Binary search function that returns key's successor or predecessor if not found
我有这个二进制搜索 function 写在 C 返回给定键的第一次出现。 但我想添加标题中描述的功能,使用not_found参数。 如果找不到密钥并且not_found == 1 ,我该如何做,然后返回它的后继者或者如果not_found == -1 ,返回它的前任代码的注释部分在某些情况下有效,但 ...
[英]In the below code, where is the while loop breaking?
我正在尝试解决这个leetcode744 我找到了这个解决方案,但无法弄清楚 while 循环在哪一点退出? ` ` 当我尝试试运行时,我一直坚持将开始、结束和中间值设置为相同的值。 使用 IDE 进行调试显示 while 循环在设置重复的中间值、开始/结束值仅一次后退出。 ...
[英]How is binary search applicable here (since the values are not monotonic)?
我正在解决 LeetCode 问题Search in Rotated Sorted Array ,以便更好地学习 Binary Search 。 问题陈述是: 有一个整数数组nums按升序排序(具有不同的值)。 在传递给您的函数之前, nums可能会在未知的枢轴索引处旋转。 例如, [0,1,2, ...