前几天投了发搜狗,做了他家的笔试,随手记录一下。
一共3道编程题,没有选择,没有填空。
第一题题意大概是这样的:
给了两个人做题的答案(只有ABCD的字符串),告诉第二个人做对了n个,问第一个人最少做对多少个,最多做对多少个。
做这一题的时候有点想睡觉,花的时间比较久,导致后面自己会的题目也没时间写了,结果也只做对了60%。我想的是求最小值时,拿两者不相等的题数n1和第二个人对的题数n2去比较,当n1<n2时,结果是n2-n1,当n1>=n2时,结果为0。求最大值时,拿两者相等的题数n3和第二个人对的题数n2去比较,当n3<n2时,结果是n3+(n1-n2),当n3=n2时,结果是n3【现在突然发现这时候可能为全部】,当n3>n2时,结果是n2.
求做对这题的大佬能指点我一下,万分感谢。
第二题题意大概是:
给一个n*n的只含1和0的矩阵m1和一个n*n的字母矩阵m2,把m1中为0的位置对应的m2输出,再将m1顺时针旋转90度,做相同的操作,再将m1顺时针旋转90度,做相同的操作,再将m1顺时针旋转90度,做相同操作(一共是4次操作)。
这题很简单,暴力就可以了。
留个代码:
public String rotatePassword (String[] s1, String[] s2) {
// write code here
String result = "";
int len = s1.length;
String[] time1 = getTimes(s1, len);
String[] time2 = getTimes(time1, len);
String[] time3 = getTimes(time2, len);
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if(s1[i].charAt(j) == '0')
tmp.append(s2[i].charAt(j));
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if(time1[i].charAt(j) == '0')
tmp.append(s2[i].charAt(j));
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if(time2[i].charAt(j) == '0')
tmp.append(s2[i].charAt(j));
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if(time3[i].charAt(j) == '0')
tmp.append(s2[i].charAt(j));
}
}
result = new String(tmp);
return result;
}
/**
* 顺时针旋转90度
* @param s
* @return
*/
public String[] getTimes(String[] s, int len){
String[] result = new String[len];
char[] tmp = new char[len];
for (int i = 0; i < len; i++) {//行
int co = 0;
for (int j = len - 1; j >= 0 ; j--) {//列
tmp[co++] = s[j].charAt(i);
}
result[i] = new String(tmp);
}
return result;
}
第三题题意大概是:
有n个点和m条有向边,其中0为起点,-1为终点,能到-1的点都记作有效点(不含0和-1),问有效点有多少个和有效点的和。100%数据范围是1e5。
这题前前后后读了3遍题目才看懂,第一题时间花的有点长,导致最后剩15分钟没时间写了,大致想了下思路,用并查集做,把点都往-1合并,最后父节点为-1的就是有效点,不知道对不对。