这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这是一个非常好的用来讲分支预测(Branch Prediction)的例子,分享给大家看看 一.问题引入 先看这个代码: #include <algorithm> #include <ctime> #include <iostream> #include <stdint…
What is Branch Prediction? Consider a railroad junction: Image by Mecanismo, via Wikimedia Commons. Used under the CC-By-SA 3.0 license. Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication. Yo…
Maintain a min-heap with size = k, to collect the result. //Find K minimum values from an unsorted array //Implement Min-Heap public int[] findKMax(int[] arr, int k){ if(arr == null || arr.length == 0) return null; int[] result = new int[k]; for(int…
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路:使用二分法,将list的中间节点作为根节点,然后分别处理list左半边及右半边,以此递归. struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right…
(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appearing in interviews. There are four basic solutions. Solution 1 -- Sort First A Simple Solution is to sort the given array using a O(n log n) sorting alg…
在阅读ECMAScript的文档的时候,有注意到它说,数组的push方法其实不仅限于在数组中使用,专门留作通用方法.难道是说,在一些类数组的地方也可以使用?而哪些是和数组非常相像的呢,大家或许一下子就可以想到就是Object对象.因为Array就是继承自Object的,可以用 [] instanceof Object,会发现返回的是true.当然大家都知道,这也不是什么新鲜事.那我们可以大胆尝试一下,如果我们将数组的push方法应用在对象上,会一个怎么样的表现呢?…
相关阅读:https://blog.csdn.net/u013185654/article/details/78498393 相关阅读:https://www.cnblogs.com/huangyin1213/p/5573676.html 说明:本片主要对比介绍与数组操作相关的方法 (一)字符串方法(String对象方法) indexOf() //检索字符串 lastIndexOf() //从后向前搜索字符串 match() //找到一个或多个正则表达式的匹配 replace() //替换与正则…
我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2,时这个成立,但=1时,就不成立了,如: In [7]: yOut[7]: array([0, 0, 0, 0, 0]) In [14]: y.TOut[14]: array([0, 0, 0, 0, 0]) In [15]: y.transpose()Out[15]: array([0, 0, 0, 0,…
Array: 1. slice() const newAry = ary.slice() 2. concat const newAry = [].concat(ary) 3. spread opreator: const newAry = [...ary] 4. Array.from: const newAry = Array.from(ary) Object: Shadow copy: 1. object.assign: const newObj = Object.assign({}, obj…
//数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key.&qu…