Leetcode 1370. 上升下降字符串
这题是一个简单题,题目没有需要太多的思考,只需要按照题目的思路来即可。
我想到的思路是用哈希表,把每个字母出现的次数都记录下来,同时把每个字母都记录下来(只记录一次)为keys。之后只需要将keys按ascil大小排序,然后先从前向后遍历keys,如果该keys对应的哈希表的值大于一,就将它添加到结果里去,同时对应哈希表的值要减一。之后在从后往前遍历,直到结果的长度与输入的长度一样为止。
var sortString = function(s) {
let s_arr=s.split('');
let map=new Map();
let keys=[];
for(let i=0;i<s_arr.length;i++){//构建哈希表
if(map.has(s_arr[i])){
map.set(s_arr[i],map.get(s_arr[i])+1);
}
else{
keys.push(s_arr[i]);
map.set(s_arr[i],1);
}
}
if(keys.length<2){//如果是重复的字母,直接返回
return s;
}
keys.sort((a,b)=>a.charCodeAt()-b.charCodeAt());//排序
let end=[];
let num=0;//指针
let up=true;
while(end.length<s_arr.length){
if(map.get(keys[num])>0){
map.set(keys[num],map.get(keys[num])-1);//哈希表对应减一
end.push(keys[num]);//添加到结果里去
}
if(up){//向上
num++;
}
else{//向下
num--;
}
if(num>keys.length-1){
num=keys.length-1;
up=false;
}
if(num<0){
num=0;
up=true;
}
}
let end_s='';
for(let i=0;i<end.length;i++){//结果拼接成字符串
end_s+=end[i];
}
return end_s;
};
二,js对象继承
es5没有类的语法,所以继承只能使用原型链的方式。
function base_obj() {//对象构造器
this.item1=1;
this.item2=5;
// body...
}
base_obj.prototype.add_item_num=function(){//为对象添加方法
this.item1++;
console.log(this.item1);
}
function child_obj(){
base_obj.call(this);//使用该方法完成继承,只会继承构造器的内容,不会继承原型链上的
this.new_item1=-1;
}
child_obj.prototype=Object.create(base_obj.prototype);//该方法可以实现继承父类的方法
//child_obj.prototype.construct=child_obj;
ES6添加了class语法,使得js编写类更为方便,继承的语法也变得更简单
class Point{//point类
constructor(x,y,color){
this.x=x;
this.y=y;
this.color=color;
}
change_color(color){
this.color=color;
console.log(this.color);
}
}
class Rancle extends Point{//继承point
constructor(x,y,color,back_color){
super(x,y,color);//必须第一步调用
this.back_color=back_color;
}
change_back(color){
this.back_color=color;
}
}
let point =new Point(1,2,'red');
console.log(point);
point.change_color('rednew');
console.log(point);
let ran=new Rancle(3,4,'blue','bblue');
ran.change_color('new_back');
ran.change_color('new_color');
console.log(ran);
es6的类继承还可以继承原生的类,使得可以定义自己的数据结构
下面代码是自定义一个数组,添加一个history,用来记录每次操作后的数据。
class New_arr extends Array{
constructor(){
super();
this.history=[];
}
push_(arr){//定义push方法
this.push(arr);
this.history.push(this.concat());//记录数据
}
pop_(){//定义push方法
this.pop();
this.history.push(this.concat());//记录数据
}
}
let arr=new New_arr();
arr.push_(1);
arr.push_(2);
arr.push_(1);
arr.push_(2);
console.log(arr.length);
console.log(arr);
console.log(arr.history);
arr.pop_();
console.log(arr);
console.log(arr.history);
三,websocket
websocket是为了解决数据不能从服务器主动发送的问题,由于http协议是在tcp协议上的,所以理论上也支持全双工通信。websocket其实就是利用http协议来完成握手,之后的通信都是tcp的。
服务端使用websocket-node,实例代码
#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}
var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.sendUTF(message.utf8Data);
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
客户端
#!/usr/bin/env node
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log("Received: '" + message.utf8Data + "'");
}
});
function sendNumber() {
if (connection.connected) {
var number = Math.round(Math.random() * 0xFFFFFF);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
});
client.connect('ws://localhost:8080/', 'echo-protocol');