Comments (11)
这是华为最喜欢考的题。。。。计数排序
from leetcode.
/**
* Array存储一个数字耗费一个字节,一个字节又对应8个bit,每个bit可以表示一个数,0表示没有此数,1表示有此数
* 比如:1,2,3,10,在Array中的存储为
* [7,2]
* 7的二进制为 00000111 后面三个1分别表示1,2,3
* 2的二进制位 00000010
*
* 这样array的每位可以用来表示8个数,那么一千万个数的话,就只需要1250000byte表示,
* 1250000/1024/1024 约等于 1M
*
* @param {*} inputFile
*/
function sortBigArrays(inputFile) {
var array = new Array(1).fill(0);
var number = '';
var readerStream = fs.createReadStream(inputFile, {
highWaterMark: 1
});
readerStream.on('data', function (chunk) {
if (chunk == ',') {
putNumberIntoMap(array, +number);
number = '';
} else {
number += chunk;
}
});
readerStream.on('end', function () {
putNumberIntoMap(array, +number);
var wtStream = fs.createWriteStream('output.txt');
for (var i = 0, len = array.length; i < len; i++) {
// console.log('output ', i, ' into file');
if (array[i] && array[i] != 0) {
// 输出到文件
var base = 8 * i;
var t = array[i].toString(2).split('').reverse();
for (var j = 0; j < t.length; j++) {
if (t[j] == '1') {
wtStream.write((base + j + 1) + (i == len - 1 ? '' : ','));
}
}
}
}
wtStream.end();
});
function putNumberIntoMap(array, number) {
var posIndex = number % 8;
var arrayIndex = parseInt(number / 8);
posIndex = posIndex == 0 ? (arrayIndex--, 7) : posIndex - 1;
var sum = array[arrayIndex] == null ? array[arrayIndex] = 0 : array[arrayIndex];
sum = sum.toString(2).split('');
while (sum.length != 8) {
sum.unshift('0');
}
sum[7 - posIndex] = '1';
array[arrayIndex] = parseInt(sum.join(''), 2);
}
}
from leetcode.
/** * Array存储一个数字耗费一个字节,一个字节又对应8个bit,每个bit可以表示一个数,0表示没有此数,1表示有此数 * 比如:1,2,3,10,在Array中的存储为 * [7,2] * 7的二进制为 00000111 后面三个1分别表示1,2,3 * 2的二进制位 00000010 * * 这样array的每位可以用来表示8个数,那么一千万个数的话,就只需要1250000byte表示, * 1250000/1024/1024 约等于 1M * * @param {*} inputFile */ function sortBigArrays(inputFile) { var array = new Array(1).fill(0); var number = ''; var readerStream = fs.createReadStream(inputFile, { highWaterMark: 1 }); readerStream.on('data', function (chunk) { if (chunk == ',') { putNumberIntoMap(array, +number); number = ''; } else { number += chunk; } }); readerStream.on('end', function () { putNumberIntoMap(array, +number); var wtStream = fs.createWriteStream('output.txt'); for (var i = 0, len = array.length; i < len; i++) { // console.log('output ', i, ' into file'); if (array[i] && array[i] != 0) { // 输出到文件 var base = 8 * i; var t = array[i].toString(2).split('').reverse(); for (var j = 0; j < t.length; j++) { if (t[j] == '1') { wtStream.write((base + j + 1) + (i == len - 1 ? '' : ',')); } } } } wtStream.end(); }); function putNumberIntoMap(array, number) { var posIndex = number % 8; var arrayIndex = parseInt(number / 8); posIndex = posIndex == 0 ? (arrayIndex--, 7) : posIndex - 1; var sum = array[arrayIndex] == null ? array[arrayIndex] = 0 : array[arrayIndex]; sum = sum.toString(2).split(''); while (sum.length != 8) { sum.unshift('0'); } sum[7 - posIndex] = '1'; array[arrayIndex] = parseInt(sum.join(''), 2); } }
小骑士, 有没有兴趣领这道题?
from leetcode.
我來,之前看過這道題。但是不會做
from leetcode.
我來,之前看過這道題。但是不會做
done
from leetcode.
这是编程珠玑里面的题,思路就是将输入的数据映射到数组的index,最后输出数组中有值的index,就是排好序的数据。分配一个10^7大小的bit位数组,大约为1M,将输入数据作为index,在对应数组值上填写一个bit 1,遍历数组,输出bit为1的数组的 index。排序完成。
from leetcode.
这是编程珠玑里面的题,思路就是将输入的数据映射到数组的index,最后输出数组中有值的index,就是排好序的数据。分配一个10^7大小的bit位数组,大约为1M,将输入数据作为index,在对应数组值上填写一个bit 1,遍历数组,输出bit为1的数组的 index。排序完成。
我的label 有著明是来自 编程珠玑 ,只不过是英文
from leetcode.
我來,之前看過這道題。但是不會做
时间很长了,没有忘记吧?
from leetcode.
没忘,我补了点基础知识。争取这个月完成
from leetcode.
没忘,我补了点基础知识。争取这个月完成
ok
from leetcode.
认领
from leetcode.
Related Issues (20)
- 算法学习 HOT 1
- 树专题中双色标记法后序和前序写反了 HOT 2
- leetcode/thinkings/tree.md 出错 HOT 1
- some error
- 二分查找专题,寻找最左/右插入位置算法模板错误问题 HOT 9
- possible code error in thinkings/heap.md HOT 1
- link error HOT 4
- link is not correct
- [695.最大岛屿面积,360,面试原题]【每日一题】 HOT 3
- 【专题】 反向思考 HOT 3
- 【专题】 考虑每一项对结果到的贡献
- 【专题】递推方程时间复杂度优化
- 已发布文章的代码错误 HOT 7
- Remove duplicate CPP solution and add Python solution for problem 100.same-tree
- Add OSSF Scorecard security workflow
- 题目的排版可否改一改
- 关于二分法中查找中间点索引的算式 HOT 6
- leetcode-thinkings-tree.md BFS 模版调整 HOT 3
- anki-card 中只有10道题吗?截止到2023.11 HOT 1
- 【每日一题】- 2020-xx-xx - xxx
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 leetcode.