1. 查找 最大 不含重複字元 子字元串AC, 思路:視窗周遊
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function (line) { // javascript每行資料的回調接口
var str = line.trim();
var tmp = '', maxSubStr = tmp, start = ;
for(var i = ; i < str.length; i++){
var j = tmp.search(str[i]);
if(j === -){
tmp += str[i];
}else{
if(tmp.length > maxSubStr.length){
maxSubStr = tmp;
}
start += j +
tmp = str.slice(start, i + );
}
// console.log(tmp);
}
if(tmp.length > maxSubStr.length){
maxSubStr = tmp;
}
console.log(maxSubStr.length);
rl.close();
});
2.分幾個部門未AC,筆試完後寫的:
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var M = ;
var curLine = ;
var input = [];
rl.on('line', function (line) { // javascript每行資料的回調接口
if(curLine === ){
M = parseInt(line.trim());
}else{
input.push(line.trim().split(' '));
if(curLine === M){
console.log(func(input));;
rl.close();
}
}
curLine++;
});
function func(input){
var cnt = ;
for(var i = ; i < input.length; i++){
for(var j = ; j < input[].length; j++){
if(input[i][j] == ){
visit(input, i, j);
cnt++;
}
}
}
return cnt;
}
function visit(input, i, j){
input[i][j] = ;
if(i- >= && input[i-][j] == ){
visit(input, i-, j);
}
if(i+ < input.length && input[i+][j] == ){
visit(input, i+, j);
}
if(j- >= && input[i][j-] == ){
visit(input, i, j-);
}
if(j+ < input[].length && input[i][j+] == ){
visit(input, i, j+);
}
}
3.還原IP AC:暴力分段
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function (line) { // javascript每行資料的回調接口
var str = line.trim();
var ret = [], a, b, c, d;
if(str.length > ){
rl.close();
}
for(var i = ; i <= ; i++){
if(str.length - i < ){
break;
}
a = str.slice(, i);
for(var j = ; j <= ; j++){
if(str.length - i - j < ){
break;
}
b = str.slice(i, i + j);
for(var k = ; k <= ; k++){
if(str.length - i - j - k < ){
break;
}
c = str.slice(i + j, i + j + k);
d = str.slice(i + j + k);
var tmp = [a, b, c, d].join('.');
// console.log(tmp);
if(/^(1\d{2,2}|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[1-9])(\.(1\d{2,2}|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])){3,3}$/.test(tmp)){
ret.push(tmp);
}
}
}
}
console.log(ret.length);
rl.close();
});
4.是否為合理utf-8碼:比較每個位元組是否在給定區間(比大小)40%,不知道為什麼出錯了,樣例有點問題:
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var curLine = ;
var bytes = ;
var input = [];
rl.on('line', function (line) { // javascript每行資料的回調接口
if(curLine === ){
bytes = parseInt(line.trim());
if(bytes > ){
console.log();
rl.close();
}
}else{
input = line.trim().split(' ');
for(var i = ; i < bytes; i++){
input[i] = parseInt(input[i]);
}
if(bytes == ){
if(input[] > || input[] < ){
console.log();
rl.close();
}else{
console.log();
rl.close();
}
}else if(bytes > ){
var max = parseInt((new Array(bytes + )).join('1') + '0' + (new Array( - bytes)).join('1'), );
var min = parseInt((new Array(bytes + )).join('1') + '0' + (new Array( - bytes)).join('0'), );
// console.log(min, max);
if(input[] > max || input[] < min){
console.log();
rl.close();
}else{
var max = ;
var min = ;
for(var i = ; i < bytes; i++){
if(input[i] > max || input[i] < min){
console.log();
rl.close();
}
}
console.log();
rl.close();
}
}
}
curLine++;
});
5.抖音網紅AC:反向建立有向圖,逐個dfs,dfs傳回路徑等于人數,則計數加一
var Graph = (function () {
function Graph(v) {
this.vertices = v;
this.edges = ;
this.adj = [];
this.marked = [];
for (var i = ; i < this.vertices; i++) {
this.adj[i] = [];
this.marked[i] = false;
}
}
Graph.prototype.addEdge = function (v, w) {
this.adj[v].push(w);
this.edges += ;
}
// search
function _dfs(v, path) {
this.marked[v] = true;
if (this.adj[v] != undefined) {
path.push(v);
}
this.adj[v].forEach(element => {
if (!this.marked[element]) {
_dfs.call(this, element, path);
}
});
}
Graph.prototype.dfs = function (v) {
for (var i = ; i < this.vertices; i++) {
this.marked[i] = false;
}
var path = [];
_dfs.call(this, v, path);
return path;
}
return Graph;
})();
// 讀取資料
var readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
var N, M;
var curLine = ;
var g;
rl.on('line', function (line) {
if (curLine === ) {
N = parseInt(line.trim());
g = new Graph(N + );
} else if (curLine === ) {
M = parseInt(line.trim());
}else if(curLine === ){
var arr = line.trim().split(' ');
for (var i = ; i < M * ; i+=) {
arr[i] = parseInt(arr[i]);
arr[i + ] = parseInt(arr[i + ]);
g.addEdge(arr[i + ], arr[i]);
}
var cnt = ;
for (var i = ; i <= N; i++) {
var tmp = g.dfs(i);
if(tmp.length === N){
cnt++;
}
}
console.log(cnt);
rl.close();
}
curLine++;
});