Comments (2)
第k小的数字
function minK(num, k) {
k = k - 1;
function partial(num, start, end) {
let pivot = num[start];
while (start < end) {
while (start < end && num[end] > pivot) end--;
if (start < end) {
num[start] = num[end];
start++;
}
while (start < end && num[start] < pivot) start++;
if (start < end) {
num[end] = num[start];
end--;
}
}
num[start] = pivot;
return start;
}
function findK(start, end) {
let mid = partial(num, start, end);
if (mid === k) return num[mid];
if (mid > k) return findK(start, mid - 1);
if (mid < k) return findK(mid + 1, end);
}
return findK(0, num.length - 1);
}
let arr = [1, 234, 4, 23, 88, 34, 90, 45, 63, 2, 3, 4, 555, 6, 7, 8, 10, 9];
console.log(arr);
console.log(minK(arr, 18));
console.log(arr.sort((a, b) => a - b));
from basic-programming-knowledge.
合并K个有序链表
- 解决两条有序链表的合并
- 解决规模划分,即两两划分
- 递归处理子问题,注意当数组单位为1时,直接返回该链表,奇数长度数组又一个元素作为单位为1的处理
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
if (!lists || !lists.length) return null
// 合并两排序链表
function mergeTwo(a, b) {
if (!a) return b;
if (!b) return a;
let head = new ListNode(0);
let tail = head;
while (a && b) {
if (a.val < b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = (a ? a : b);
return head.next;
}
// 分治算法 两两合并
function division(lists, l, r) {
if (!lists) return null;
if (l === r) return lists[l]
let all = [];
while(l < r) {
all.push(mergeTwo(lists[l++], lists[r--]));
}
if (l === r) all.push(lists[l])
return division(all, 0, all.length - 1)
}
return division(lists, 0, lists.length - 1)
};
from basic-programming-knowledge.
Related Issues (20)
- 2021-11-25 图论
- 【VIM】编辑器相关整理
- 【Rust】资源汇总
- 【React】资源汇总
- 【tools】最有用的那些软件、站点-工具链
- 【英语】学习资源
- 【vim】文艺复兴·VIM使用指南·Day 1
- 【vim】文艺复兴·VIM使用指南·Day 2
- 【vim】文艺复兴·VIM使用指南·Day 3
- 【翻译】monio关于monad的解释
- 【函数式编程】函子 HOT 1
- 【异步】async/await比Promise好吗?
- 【杂文】述职报告的思考
- 【函数式编程】什么是lambda演算? HOT 1
- 【函数式编程】haskell语言-haskell趣学指南 HOT 5
- 【工程化】如何创建一个现代化的前端基础库
- wsl2开发环境设置排坑
- 前端库
- 【产品】构建一个产品需要哪些优秀的工具?
- 【docker】mac配置docker开发环境/用vscode远程编码/逐行解说
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from basic-programming-knowledge.