如何使用位查找工作计划中的可用时间?
参考已经给出的另一个问题的答案:
https://stackoverflow.com/a/1609686/1183608
我是使用这个答案,但是我对计算acceptable_times数组的意义感到困惑。我想找出一天中的哪些时间(时间表)可用于特定的时间段长度,但上述答案中执行此操作的功能似乎不能满足这一点,除非我错过了某些内容。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
grioja,您链接到的答案确实为您提供了一天中可用于您所需的时段长度的时间。繁忙/可用时间用位表示,例如:
1100001110
(这将是 30 个可用分钟,后面是 60 个繁忙分钟,后面是 45 个可用分钟,后面是 15 个繁忙时间。)然后您将得到一个二进制数,表示长度您要查找的可用时间段,例如:
111
(这意味着您要查找 45 分钟的可用时间段。)然后您尝试将这 2 个数字相互移动:
1100001110
0000000111
1100001110
0000001110
1100001110
0000011100
...等等,对于每个可能的相对移位长度。他在该答案中显示的 AND 运算确定“所需时隙长度”数字中的 1 是否与“可用次数”数字中的全 1 匹配。如果是的话,你已经找到了一个可以接受的时间。
顺便说一下,你可以用其他方式做同样的事情。例如,您可以用字符串中的字符表示繁忙/可用时间,并使用正则表达式查找所需长度的可用插槽。
请注意,如果可用/繁忙时隙列表很长,则这种通用方法(基本上只是“线性搜索”)将无法扩展。在这种情况下,还有其他方法会更快。
grioja, the answer you linked to does give you the times of day which are available for your desired slot length. The busy/available times are represented by bits, for example:
1100001110
(This would be 30 available minutes, followed by 60 busy minutes, followed by 45 available, followed by 15 busy.) Then you get a binary number which represents the length of the available time slot you want to find, say:
111
(This would mean you want to find a slot of 45 available minutes.) Then you try shifting the 2 numbers against each other:
1100001110
0000000111
1100001110
0000001110
1100001110
0000011100
...and so on, for every possible relative shift length. The AND operation he shows in that answer determines whether the 1's in the "desired slot length" number are matching all 1's in the "available times" number. If they are, you have found an acceptable time.
By the way, you could do the same thing in other ways. For example, you could represent busy/available times by characters in a string, and use a regular expression to find an available slot of the desired length.
Note that this general approach (which is basically just "linear search") will not scale if the list of available/busy time slots is very long. There are other approaches which will be much faster in that case.