《高性能javascript》 --- in case of odd number of items(奇怪的条目的数量)
2016-11-30 17:09
274 查看
不知道是做着故意放的还是什么原因。总之运行后就会出现问题(奇怪的条目的数量)
问题出在标红的这3块
附上正确代码:
function merge(left, right){ var result = []; while (left.length > 0 && right.length > 0){ if (left[0] < right[0]){ result.push(left.shift()); } else { result.push(right.shift()); } } return result.concat(left).concat(right); } function mergeSort(items){ if (items.length == 1) { return items; } var work = []; for (var i=0, len=items.length; i < len; i++){ work.push([items[i]]); } work.push([]); //in case of odd number of items for (var lim=len; lim > 1; lim = (lim+1)/2){ for (var j=0,k=0; k < lim; j++, k+=2){ work[j] = merge(work[k], work[k+1]); } work[j] = []; //in case of odd number of items } return work[0]; }
问题出在标红的这3块
附上正确代码:
function merge(left, right){ var result = []; while (left.length > 0 && right.length > 0){ if (left[0] < right[0]){ result.push(left.shift()); } else { result.push(right.shift()); } } return result.concat(left).concat(right); } function mergeSort(items){ if (items.length == 1) { return items; } var work = []; for (var i=0, len=items.length; i < len; i++){ work.push([items[i]]); } // work.push([]); //in case of odd number of items for (var lim=len; lim > 1; lim = lim/2){ for (var j=0,k=0; k < lim; j++, k+=2){ work[j] = merge(work[k], work[k+1]); } // work[j] = []; //in case of odd number of items } return work[0]; }
相关文章推荐
- sharepoint2010:The number of items in this list exceeds the list view threshold, which is 20000 items.
- [LeetCode] Number of Segments in a String 字符串中的分段数量
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
- 复制单链表成为新的链表,然后return the number of items in the new list
- 转:InfoPath 中,如何限制 Contact Selector 控件中输入的用户数量? Validate the Contact Selector in InfoPath – Limit the number of names a user can
- Deleting a large number of items from a list in SharePoint
- Leetcode: Number of Connected Components in an Undirected Graph
- [已解决]In the case of corrupt Gradle processes
- leetcode 434- Number of Segments in a String
- Number of Connected Components in an Undirected Graph
- How to reduce number of records on CTXSYS.DR$WAITING,CTXSYS.DR$PENDING tables in Oracle EBS?
- 762. Prime Number of Set Bits in Binary Representation
- How can i get hold of all of the visible items in my QListView?
- 如何处理 android 方法总数超过 65536 . the number of method references in a .dex file exceed 64k
- Number of files in each directory
- The number of method references in a .dex file cannot exceed 64K. Error 解决方案
- Android开发 Error(建议收藏下来以备不时之需):The number of method references in a .dex file cannot exceed 64K.
- The number of method references in a .dex file cannot exceed 64K
- A string s is LUCKY if and only if the number of different characters in s is a fibonacci number. Gi