Git Product home page Git Product logo

blog's Introduction

unclay

unclay

blog's People

Contributors

unclay avatar

Watchers

 avatar

blog's Issues

Ubuntu16.04安装最新版nodejs

最近公司服务器因为某些原因,导致服务器环境需要全部重新部署,安装nodejs又忘了,记录下,方便下次使用。

安装最新版nodejs

更新ubuntu软件源
sudo apt-get update
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
安装nodejs
sudo apt-get install nodejs
sudo apt install nodejs-legacy
sudo apt install npm
更新npm的包镜像源,方便快速下载
sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list
全局安装n管理器(用于管理nodejs版本)
sudo npm install n -g
安装最新的nodejs(stable版本)
sudo n stable
sudo node -v

QA

Q: apt-get update找不到文件

// error code
W: The repository 'http://ppa.launchpad.net/chris-lea/node.js/ubuntu xenial Release' does not have a Release file.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
E: Failed to fetch http://ppa.launchpad.net/chris-lea/node.js/ubuntu/dists/xenial/main/binary-amd64/Packages  404  Not Found
E: Some index files failed to download. They have been ignored, or old ones used instead.

// A: 修改node相关文件:/etc/apt/sources.list.d/chris-lea-ubuntu-node_js-xenial.list
deb http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main
deb-src http://ppa.launchpad.net/chris-lea/node.js/ubuntu trusty main

Q: node -v异常

node -v
-bash: /usr/local/bin/node: Permission denied

sudo node -v
Segmentation fault (core dumped)

// A: 重新安装nodejs
手动移除 /usr/local/lib/node_modules整个目录
手动移除 /usr/local/bin/node
手动移除 /usr/local/bin/n
手动移除 /usr/local/bin/npm

ubuntu软件源

修改/etc/apt/sources.list为以下软件源(改前请备份)
163和里面在部分网络下会一直下载失败,可以改回官方源下载

源列表

http://wiki.ubuntu.org.cn/模板:16.04source

Ubuntu官方
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-proposed main restricted universe multiverse

mongoose常见问题

  • aggregate的match无法查询ObjectId类型数据

解决方法: 使用mongoose.Types.ObjectId,而不是用mongoose.Schema.Types.ObjectId

前端也有算法 - 位图法

一不小心就看到位图法,引起了我的兴趣。
简单说下位图法(bitmap),用每一个bit来存放某个状态,常用于正整数类型中。(具体内容请自行百度)

先来个抛砖引玉吧,曾经有一个题目,据说是腾讯的前端面试题,内容大概如下:

示例一(已删数字):

题目:

有一组数字,从1到n,假设n=10,乱序且不重复。例如[1,5,8,3,4,9,2,6,7,10],随机删除3个数字,请找出3个被删除的数字。

算法:
  1. 以最大数字n=10来初始化位图数组
  2. 循环剩余数字,把状态位设置进位图数组里面(复杂度N)
  3. 循环状态位数组,把状态位不为1的提取到输出结果数组(复杂度N)
  4. 总复杂度2N,N∈正整数
// 随机删除后的数组
var arr = [1, 3, 4, 9, 2, 7, 10];
// 位图数组,多了索引0,所以设置11个
var bitmap = Array(11);
// 输出数组
var outputArr = [];
// 设置位图状态
for (var i = 0; i < arr.length; i++) {
  bitmap[arr[i]] = 1;
}
// 提取已移除的数字
for (var i = 1; i < bitmap.length; i++) {
  if (!bitmap[i]) {
    outputArr.push(i);
  }
}
// 输出[5, 6, 8]
console.log(outputArr); 
原理:

初始化的位图数组如下:

[undefind * 11]

设置状态位后的位图数组(undefind当0)如下:

[0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1 ]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

不难看出,缺失的5,6,8,位图法就是通过这种原理来设置的
另外,位图法还支持排序,从上得知数字也被重新排好序了

示例二(去重):

题目:

已知两个数组[8,4,6,2,1,9]、[3,4,5,8,9,7,0],请合并去除重复数字,并找出重复过的数字。

算法:
  1. 找出两个数组中最大的数字,用于构建位图数组, max=10(此步骤不做处理)
  2. 把已知的第一个数组写进位图数组里(复杂度N)
  3. 把已知的第二个数组与位图数组做比对,状态位为1的则是已存在的数字,即重复数字,比对完后也写进位图数组里(复杂度N)
  4. 循环位图数组,提取所有数字,即可得出合并后的数组(复杂度N)
  5. 总复杂度3N,N∈正整数
var arr1 = [8, 4, 6, 2, 1, 9];
var arr2 = [3, 4, 5, 8, 9, 7, 0];
var bitmap = Array(10);
// 重复的数字
var repeatArr = [];
// 合并后的数组
var concatArr = [];
// 设置位图状态
for (var i = 0; i < arr1.length; i++) {
  bitmap[arr1[i]] = 1;
}
// 找到重复数字
for (var i = 1; i < arr2.length; i++) {
  if (bitmap[arr2[i]]) {
    repeatArr.push(arr2[i]);
  }
  // 同时设置位图状态
  bitmap[arr2[i]] = 1;
}
// 合并后的数组
for (var i = 0; i < bitmap.length; i++) {
  if (bitmap[i]) {
    concatArr.push(i);
  }
}
// 输出[4, 8, 9]
console.log(repeatArr); 
// 输出[0, 1, 2, 4, 5, 6, 7, 8, 9]
console.log(concatArr); 

数量级别:

位图法能不能计算数以亿计的量级,答案是“能”
一个bit状态位表示一条数据,
我们来简单计算一下:

1B = 8bit
1KB = 1024B
1MB = 1024KB
    ≈ 1024 * 1024 * 8 bit 
    ≈ 840万 bit

也就是说,800多万的数据才占用了1MB内存而已,剩下的就是cpu的计算了

位图法总结

  1. 只能用于正整数
  2. 支持排序
  3. 支持去重
  4. 支持亿量级别计算
    至于使用场景,需要自己摸索,至少比二叉树的实际使用场景多

文件格式(UMD、注释头)

注释头

/*
* @Description: test
* @Author: Clay
* @Date: 2019-04-01
* @LastEditors: Clay
* @LastEditTime: 2019-04-01 17:56
*/

UMD模板

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD
    define(['jquery'], factory);
  } else if (typeof exports === 'object') {
    // Node, CommonJS之类的
    module.exports = factory(require('jquery'));
  } else {
    // 浏览器全局变量(root 即 window)
    root.returnExports = factory(root.jQuery);
  }
}(this, function ($) {
  // 方法
  function myFunc(){};

  // 暴露公共方法
  return myFunc;
}));

Ubuntu14.04安装最新版nodejs

最近公司服务器因为某些原因,导致服务器环境需要全部重新部署,安装nodejs又忘了,记录下,方便下次使用。

安装最新版nodejs

更新ubuntu软件源
sudo apt-get update
sudo apt-get install -y python-software-properties software-properties-common
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
安装nodejs
sudo apt-get install nodejs
更新npm的包镜像源,方便快速下载
sudo npm config set registry https://registry.npm.taobao.org
sudo npm config list
全局安装n管理器(用于管理nodejs版本)
sudo npm install n -g
安装最新的nodejs(stable版本)
sudo n stable
sudo node -v

QA

Q: 更新软件源出现-找不到对应的KEY
// error code
W: There is no public key available for the following key IDs:
8B48AD6246925553
W: Failed to fetch http://mirrors.aliyuncs.com/ubuntu/dists/precise/Release.gpg  Could not connect to mirrors.aliyuncs.com:80 (10.143.34.200), connection timed out

// A:resolve: add key
gpg --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
gpg -a --export 8B48AD6246925553 | sudo apt-key add -

Q: aliyun、163等其他软件源一直出现下载失败

// error code
W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise/Release.gpg  Could not connect to mirrors.163.com:80 (123.58.173.106). - connect (111: Connection refused)
W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-security/Release.gpg  Unable to connect to mirrors.163.com:http:
W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-updates/Release.gpg  Unable to connect to mirrors.163.com:http:
W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-proposed/Release.gpg  Unable to connect to mirrors.163.com:http:
W: Failed to fetch http://mirrors.163.com/ubuntu/dists/precise-backports/Release.gpg  Unable to connect to mirrors.163.com:http:
W: Some index files failed to download. They have been ignored, or old ones used instead.

// A: 部分网络问题,下载失败,尝试换成官方源,虽然比较慢,但是能下载
// resolve:修改apt文件  /etc/apt/sources.list(注意备份)
# Ubuntu官方
deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse

Q: sudo make编译nodejs失败

make[1]: *** [/home/software/node-v7.8.0/out/Release/obj.target/v8_libplatform/deps/v8/src/libplatform/default-platform.o] Error 1
make[1]: Leaving directory `/home/software/node-v7.8.0/out'
make: *** [node] Error 2

// A: gcc版本太低,需要把gcc升级到4.8以上

ubuntu软件源

修改/etc/apt/sources.list为以下软件源(改前请备份)
163和里面在部分网络下会一直下载失败,可以改回官方源下载

源列表

http://wiki.ubuntu.org.cn/模板:14.04source

Ubuntu官方
deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://cn.archive.ubuntu.com/ubuntu/ trusty-proposed main restricted universe multiverse
163软件源
deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse
aliyun软件源
deb http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://mirrors.aliyuncs.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.aliyuncs.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.aliyuncs.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.aliyuncs.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.aliyuncs.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.aliyuncs.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.aliyuncs.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.aliyuncs.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.aliyuncs.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.aliyuncs.com/ubuntu/ precise-backports main restricted universe multiverse

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.