栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

阵列中3个长度的AP的最快算法计数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

阵列中3个长度的AP的最快算法计数

如果您对列表进行第一次遍历,并且仅提取可能有3项AP的数字对(差异为0 mod 2),则可以大大缩短执行时间。然后在这些对之间进行迭代。

伪C ++-y代码:

// Contains information about each beginning pointstruct BeginNode {  int value;  size_t offset;  SortedList<EndNode> ends;  //sorted by EndNode.value};// Contains information about each class of end pointstruct EndNode {  int value;  List<size_t> offsets; // will be sorted without effort due to how we collect offsets};struct Result {  size_t begin;  size_t middle;  size_t end;};SortedList<BeginNode> nodeList;foreach (auto i : baseList) {  BeginNode begin;  node.value = i;  node.offset = i's offset; //you'll need to use old school for (i=0;etc;i++) with this  // baseList is the list between begin and end-2 (inclusive)  foreach (auto j : restList) {     // restList is the list between iterator i+2 and end (inclusive)    // we do not need to consider i+1, because not enough space for AP    if ((i-j)%2 == 0) { //if it's possible to have a 3 term AP between these two nodes      size_t listOffset = binarySearch(begin.ends);      if (listOffset is valid) {        begin.ends[listOffset].offsets.push_back(offsets);      } else {        EndNode end;        end.value = j;        end.offsets.push_back(j's offset);        begin.ends.sorted_insert(end);      }    }  }  if (begin has shit in it) {    nodeList.sorted_insert(begin);  }}// Collection done, now iterate over collectionList<Result> res;foreach (auto node : nodeList) {  foreach (auto endNode : node.ends) {    foreach (value : sublist from node.offset until endNode.offsets.last()) {      if (value == average(node.value, endNode.value)) {        // binary_search here to determine how many offsets in "endNode.offsets" "value's offset" is less than.        do this that many times:          res.push_back({node.value, value, endNode.value});      }    }  }}return res;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号