[Node.js]自学记录(二)REPL常用命令
REPL(Read Eval Print Loop)
类似于python,是一个交互式运行环境
REPL环境的基础命令:
.break 放弃当前输入函数
.clear 清屏
.exit 退出REPL
.help 帮助
.save 保存
.load 载入保存的表达式
REPL(Read Eval Print Loop)
类似于python,是一个交互式运行环境
REPL环境的基础命令:
.break 放弃当前输入函数
.clear 清屏
.exit 退出REPL
.help 帮助
.save 保存
.load 载入保存的表达式
水题
class Solution { public: int removeElement(vector<int> &nums, int val) { int curr = 0, pos = 0, len = nums.size(); for (curr = 0; curr < len; curr++) { if (nums[curr] == val) { continue; } nums[pos] = nums[curr]; pos++; } return pos; } };
什么是Node.js?
Node.js是一个基于Chrome V8引擎的JavaScript运行环境,Node.js使用了一个事件驱动、非阻塞式 I/O的模型,使其轻量又高效(来自http://nodejs.cn)。
安装Node.js
在Ubuntu系统下,需要使用NPM安装最新版Node.js。
sudo apt-get install npm sudo apt-get install nodejs sudo npm install -g n n latest
Node.js的模块
在Node.js中是以模块为单位划分功能的,每一个模块都是一个js文件,每个模块都拥有独立的全局变量或函数,只有export对象才能传递到外部。例如:
exports.printStr=function(){ return "Hello World"; };
把这段代码保存为printStr.js,那么我们要通过以下代码才能访问模块中的函数:
var printstr = require('./printStr.js'); console.log(printstr.printStr());
对于一些核心模块我们可以直接引用。比如:
var http = require('http');
这样我们就可以通过http变量来访问http模块中的方法。
下面举一个简单的例子
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, { 'Content-Type': 'text/html' });//写HTTP响应头 res.write('<head><meta charset="utf-8"/></head>');//开始书写HTML代码 res.write('Hello World'); res.end();//结束HTML响应流 }).listen(1337, "localhost");//在localhost的1337端口上开始监听 console.log('Server running on localhost');
保存这段代码为simplehttp.js,在终端中输入node simplehttp.js,终端将显示“Server runnint on localhost”
此时在浏览器中输入127.0.0.1:1337,便可以看到Hello World网页。
可视化工具RoboMongo https://robomongo.org/
在Collection中插入
db.CollectionName.insert(Your data);
或者
db.CollectionName.save(Your data);
insert和save的区别在于当_id已存在时,insert会报错,而save会覆盖数据。
例如:
db.blog.insert({"name":"rasogan","password":"123","email":"admin@admin.com"});
查询
db.CollectionName.find(Require); db.CollectionName.findOne(Require); db.CollectionName.find(Require).limit(Num); db.CollectionName.find(Require).skip(Num); db.CollectionName.find().count();
第一个,查询,可选条件
第二个,查询第一条,可选条件
第三个,查询,限制数量,可选条件
第四个,查询,跳过数量,可选条件
第五个,查询数据数量
例如
db.blog.find({"name":"rasogan"});
比较条件
大于:$gt
小于:$lt
大于等于:$gte
小于等于:$lte
不等于:$ne
或者:$or
包含:$in
不包含:$nin
排序
db.CollectionName.sort({"Segment":type});
Segment:字段名
type:1:升序 -1:降序
按条件修改
db.CollectionName.update({"Require Segment":"Value"},{$set:{"Segment want to edit":"Value after edit"}});
移除
db.CollectionName.remove(Require);
一开始没注意到要在nums中修改= =WA了好多次
自己还是蠢啊= =
public class Solution { public int removeDuplicates(int[] nums) { Set<Integer> set=new HashSet<>(); List<Integer> list=new ArrayList<>(); for (Integer integer : nums) { if (!set.contains(integer)) list.add(integer); set.add(integer); } for(int i=0;i<list.size();i++) { nums[i]=list.get(i); } return set.size(); } }
注意记录前一个节点
//JAVA public class Solution { public ListNode swapPairs(ListNode head) { ListNode currNode = head; if (head == null) return head; if (head.next == null) return head; ListNode newHead = head.next; ListNode prev = null; boolean flag = true; while (flag) { if (currNode.next == null) break; if (currNode.next.next == null) flag = false; ListNode tempNode = currNode.next; currNode.next = tempNode.next; tempNode.next = currNode; if (prev != null) prev.next = tempNode; prev = currNode; currNode = currNode.next; } return newHead; } }
双向链表,每次有get之后就把get的元素放到链表头,put的时候满了就删掉链表尾的元素。
public class LRUCache { private int Length; private Node Head; private Node Tail; private int Capacity; public LRUCache(int capacity) { Length = 0; Head = new Node(); Tail = new Node(); Head.Next = Tail; Tail.Prev = Head; Capacity = capacity; } public int Get(int key) { if (Length == 0) return -1; Node FindNode = Head; while (FindNode != null) { if (FindNode.Key == key) { FindNode.Prev.Next = FindNode.Next; FindNode.Next.Prev = FindNode.Prev; FindNode.Prev = Head; FindNode.Next = Head.Next; Head.Next.Prev = FindNode; Head.Next = FindNode; return FindNode.Value; } FindNode = FindNode.Next; } return -1; } public void Put(int key, int value) { if (Get(key) != -1) { Node FindNode = Head; while (FindNode != null) { if (FindNode.Key == key) { FindNode.Value = value; return; } FindNode = FindNode.Next; } } Node NewNode = new Node(key, value); if (Length < Capacity) { //Node NewNode = new Node(key, value); NewNode.Prev = Head; NewNode.Next = Head.Next; Head.Next.Prev = NewNode; Head.Next = NewNode; Length++; return; } if (Capacity == 1) { //Node NewNode = new Node(key, value); NewNode.Prev = Head; Head.Next = NewNode; Tail.Prev = NewNode; NewNode.Next = Tail; Length++; return; } Node OldNode = Tail.Prev; Tail.Prev = Tail.Prev.Prev; Tail.Prev.Next = Tail; OldNode = null; NewNode.Next = Head.Next; Head.Next.Prev = NewNode; Head.Next = NewNode; NewNode.Prev = Head; Length++; return; } } public class Node { public int Key; public int Value; public Node Next, Prev; public Node(int key, int value) { Key = key; Value = value; Next = null; Prev = null; } public Node() { Key = 0; Value = 0; Next = null; Prev = null; } }
注意浮点精度问题
主要就是两两算斜率
public class Solution { public int MaxPoints(Point[] points) { if (points.Length == 0) return 0; if (points.Length == 1) return 1; int max = 0; foreach (var P in points) { int Duplicate = 1; Dictionary<decimal, int> CountOfOnePoint = new Dictionary<decimal, int>(); foreach (var P2 in points) { if (P == P2) continue; if (P2.x == P.x && P2.y == P.y) { Duplicate++; continue; } decimal Gradient = (P.x == P2.x) ? decimal.MaxValue : (decimal)(P2.y - P.y) / (P2.x - P.x); if (CountOfOnePoint.ContainsKey(Gradient)) CountOfOnePoint[Gradient]++; else CountOfOnePoint.Add(Gradient, 1); } foreach (var V in CountOfOnePoint.Values) { if (V + Duplicate > max) max = V + Duplicate; } if (CountOfOnePoint.Count == 0) max = (max > Duplicate) ? max : Duplicate; } return max; } }