Giter Club home page Giter Club logo

Comments (11)

Edwardgudanyang avatar Edwardgudanyang commented on May 5, 2024 1

这是华为最喜欢考的题。。。。计数排序

from leetcode.

xiongcaihu avatar xiongcaihu commented on May 5, 2024
/**
 * 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.

azl397985856 avatar azl397985856 commented on May 5, 2024
/**
 * 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.

EdgeAsh avatar EdgeAsh commented on May 5, 2024

我來,之前看過這道題。但是不會做

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

我來,之前看過這道題。但是不會做

done

from leetcode.

raymondmars avatar raymondmars commented on May 5, 2024

这是编程珠玑里面的题,思路就是将输入的数据映射到数组的index,最后输出数组中有值的index,就是排好序的数据。分配一个10^7大小的bit位数组,大约为1M,将输入数据作为index,在对应数组值上填写一个bit 1,遍历数组,输出bit为1的数组的 index。排序完成。

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

这是编程珠玑里面的题,思路就是将输入的数据映射到数组的index,最后输出数组中有值的index,就是排好序的数据。分配一个10^7大小的bit位数组,大约为1M,将输入数据作为index,在对应数组值上填写一个bit 1,遍历数组,输出bit为1的数组的 index。排序完成。

我的label 有著明是来自 编程珠玑 ,只不过是英文

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

我來,之前看過這道題。但是不會做

时间很长了,没有忘记吧?

from leetcode.

EdgeAsh avatar EdgeAsh commented on May 5, 2024

没忘,我补了点基础知识。争取这个月完成

from leetcode.

azl397985856 avatar azl397985856 commented on May 5, 2024

没忘,我补了点基础知识。争取这个月完成

ok

from leetcode.

Changanyue avatar Changanyue commented on May 5, 2024

认领

from leetcode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.