2019字节跳动第三次笔试的其中3道编程题
最长不重复子串 (通过)√
//输入 //一个字符串 function getMax(){ var str = readline().split("")[0]; var max = 1; var cur = 1; var s = 0; for(var i=1; i<str.length; ++i){ var temp = str.substr(s, i-s); //每次从s位置开始计算新的最长不重复子串 var index = temp.lastIndexOf(str[i]); if(index != -1){ cur = i - s; if(cur > max) max = cur; cur = 1; s = s + index + 1; } } //for //for循环结束后,最后一个不重复子串可能是最长的 if((i-s) > max) max = i - s; console.log(max); } getMax();
部门个数
(通过80%,请教各位兄弟!看看漏了啥)
#include<iostream> using namespace std; int main(){ int m; cin >> m; int **a = new int*[m]; int **flag = new int*[m]; for(int i=0; i<m; ++i){ a[i] = new int[m]; flag[i] = new int[m]; for(int j=0; j<m; ++j){ cin >> a[i][j]; flag[i][j] = 0; } } int count = 0; //左上角 if(a[0][0] == 1){ ++ count; flag[0][0] = 1; if(a[0][1] == 1) flag[0][1] = 1; if(a[1][0] == 1) flag[1][0] = 1; } //左下角 if(a[m-1][0] == 1 && !flag[m-1][0] && !flag[m-2][0] && !flag[m-1][1]){ flag[m-1][0] = 1; ++ count; if(a[m-1][1] == 1) flag[m-1][1] = 1; if(a[m-2][0] == 1) flag[m-2][0] = 1; } //右上角 if(a[0][m-1] == 1 && !flag[0][m-1] && !flag[0][m-2] && !flag[1][m-1]){ flag[0][m-1] = 1; ++ count; if(a[0][m-2] == 1) flag[0][m-2] = 1; if(a[1][m-1] == 1) flag[1][m-1] = 1; } //右下角 if(a[m-1][m-1] == 1 && !flag[m-1][m-1] && !flag[m-1][m-2] && !flag[m-2][m-1]){ flag[m-1][m-1] = 1; ++ count; if(a[m-1][m-2] == 1) flag[m-1][m-2] = 1; if(a[m-2][m-1] == 1) flag[m-2][m-1] = 1; } for(int i=1; i<=(m-2); ++i){ for(int j=1; j<=(m-2); ++j){ //只要有一个设置为1说明已被组成部门,则 剩余几个也会被归于同一部门 if(flag[i+1][j] || flag[i-1][j] || flag[i][j-1] || flag[i][j+1]){ if(a[i][j]) flag[i][j] = 1; if(a[i+1][j]) flag[i+1][j] = 1; if(a[i-1][j]) flag[i-1][j] = 1; if(a[i][j-1]) flag[i][j-1] = 1; if(a[i][j+1]) flag[i][j+1] = 1; continue; } if(a[i][j] == 1){ count += 1; flag[i][j] = 1; if(a[i+1][j] == 1) flag[i+1][j] = 1; if(a[i-1][j] == 1) flag[i-1][j] = 1; if(a[i][j+1] == 1) flag[i][j+1] = 1; if(a[i][j-1] == 1) flag[i][j-1] = 1; } } } cout << count << endl; return 0; }
寻找抖音红人 (通过)
//输入 //n:总用户数 //m:m对关注关系 //2*m个数字:代表m对关注关系 //若某个用户被n个人直接或间接关注,则可视为抖音红人 //要点:集合和数组的转换,以及concat使返回一个新数组,不会改变原数组! function getHot(){ // var n = readline().split("\n")[0]; // var m = readline().split("\n")[0]; // var arr = readline().split(" "); var n = 4; var m = 4; var arr = ["1", "2", "2", "1", "3", "2", "4", "2"]; var o = {}; //key: 博主 //value:粉丝 for(var i=1; i<arr.length; i+=2){ if(! (arr[i] in o)) o[arr[i]] = []; o[arr[i]].push(arr[i-1]); //将其粉丝的粉丝也纳入 if(arr[i-1] in o){ o[arr[i]] = o[arr[i]].concat(o[arr[i-1]]); //concat不改变原数组! //两个人互粉,则相当于自己间接关注了自己 if(o[arr[i-1]].indexOf(arr[i]) != -1){ o[arr[i-1]] = o[arr[i-1]].concat([arr[i-1]]); } } } console.log(o); var count = 0; for(key in o){ var myset = new Set(o[key]); if(myset.size == n) count++; } console.log(count); }#字节跳动##笔试题目#