字节笔试 后端笔试题(8.21)
总体来说,比较复杂,做一题老半天,真心觉得两个小时也就最多做三道题。
8.22更新:问了一下字节hr,挂了。
做题情况:100 100 75 1.6 下面附前三题的代码,第四题没时间看了(顺便有没有大佬帮忙看看我第三题的忘记考虑哪个约束了吗)下面附代码
1、10的数学规律,无非就是找1的个数,然后判断有没有1的位置是不是在最后或者最前面
假设1的个数是n
如果 1在第一个位置也在最后一个位置,那么 和最小=n*11-10-1
如果 1在第一个位置但不在最后一个位置,那么 和最小=n*11-1
如果 1不在第一个位置但是在最后一个位置,那么 和=n*11-10
如果 1不在第一个位置也不在最后一位置,那么 和=n*11
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = Integer.valueOf(sc.nextLine()); for (int i = 0; i <t ; i++) { String[] strings = sc.nextLine().split(" "); int n = Integer.valueOf(strings[0]); int k = Integer.valueOf(strings[1]); String s =sc.nextLine(); int oneNum = 0; for (int j = 0; j < s.length(); j++) { if(s.charAt(j)=='1'){ oneNum++; } } if(oneNum==0){ System.out.println(0); }else if(oneNum==1){ if(k>=1){ int index = s.length()-1; for (int j = s.length()-1; j >=0; j--) { if(s.charAt(j)=='1'){ index=j; break; } } if(k>=s.length()-1-index){ System.out.println(1); }else if(k<s.length()-1-index&&k>=index){ System.out.println(10); }else { System.out.println(11); } }else { if(s.charAt(n-1)=='1'){ System.out.println(1); }else if(s.charAt(0)=='1'&&s.length()==1){ System.out.println(1); }else if(s.charAt(0)=='1'&&s.length()>1){ System.out.println(10); }else { System.out.println(11); } } }else{ if(k==0){ if(s.charAt(n-1)=='1'&&s.charAt(0)!='1'){ System.out.println(oneNum*11-10); }else if(s.charAt(0)=='1'&&s.charAt(n-1)=='1'){ System.out.println(oneNum*11-10-1); }else if(s.charAt(0)=='1'&&s.charAt(n-1)!='1'){ System.out.println(oneNum*11-1); }else { System.out.println(oneNum*11); } }else { int r = s.length()-1; for (int j = s.length()-1; j >=0; j--) { if(s.charAt(j)=='1'){ r=j; break; } } int l = s.length()-1; for (int j = 0; j <n; j++) { if(s.charAt(j)=='1'){ l=j; break; } } if(k>=s.length()-1-r){ int remain = k-s.length()+1+r; if(remain>=l){ System.out.println(oneNum*11-10-1); }else{ System.out.println(oneNum*11-10); } }else{ if(k>=l){ System.out.println(oneNum*11-1); }else { System.out.println(oneNum*11); } } } } } }2、O出口
就反向思维,从O开始用BFS找能到达的地方就行了,最后输出M*N-能到的地方的个数就是不能到达O出口的地方个数
public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] strings = sc.nextLine().split(" "); int m = Integer.valueOf(strings[0]); int n = Integer.valueOf(strings[1]); char[][] grid = new char[m][n]; for (int i = 0; i < m; i++) { String s = sc.nextLine(); for (int j = 0; j < n; j++) { grid[i][j] = s.charAt(j); } } Deque<int[]> deque = new LinkedList<>(); boolean[][] visited = new boolean[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if(grid[i][j]=='O'){ deque.addLast(new int[]{i,j}); visited[i][j]=true; break; } } } int count = 1; while (!deque.isEmpty()){ int[] start = deque.pollFirst(); int x =start[0]; int y =start[1]; if(x-1>=0&&(grid[x-1][y]=='.'||grid[x-1][y]=='D')&&!visited[x-1][y]){ deque.addLast(new int[]{x-1,y}); visited[x-1][y]=true; count++; } if(x+1<m&&(grid[x+1][y]=='.'||grid[x+1][y]=='U')&&!visited[x+1][y]){ deque.addLast(new int[]{x+1,y}); visited[x+1][y]=true; count++; } if(y-1>=0&&(grid[x][y-1]=='.'||grid[x][y-1]=='R')&&!visited[x][y-1]){ deque.addLast(new int[]{x,y-1}); visited[x][y-1]=true; count++; } if(y+1<n&&(grid[x][y+1]=='.'||grid[x][y+1]=='L')&&!visited[x][y+1]){ deque.addLast(new int[]{x,y+1}); visited[x][y+1]=true; count++; } } System.out.println(m*n-count); }3.{}匹配,只过了70%。。。还请各位大佬帮忙指点一下漏判了哪些条件
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.valueOf(sc.nextLine()); String t = sc.nextLine(); List<String> list = new ArrayList<>(); List<Integer> l = new ArrayList<>(); List<Integer> r = new ArrayList<>(); for (int i = 0; i < t.length(); i++) { if(t.charAt(i)=='{'){ l.add(i); } if(t.charAt(i)=='}'){ r.add(i); } } if(l.size()!=0){ String sub = t.substring(0,l.get(0)); if(sub!=null&&sub.length()!=0&&sub!="")list.add(sub); for (int i = 0; i < r.size()-1; i++) { String substr = t.substring(r.get(i)+1,l.get(i+1)); if(substr!=null&&substr.length()!=0&&substr!="") list.add(substr); } String sunlast = t.substring(r.get(r.size()-1)+1); if(sunlast!=""&&sunlast!=null&&sunlast.length()!=0){ list.add(sunlast); } }else { list.add(t); } for (int i = 0; i < n; i++) { String s = sc.nextLine(); boolean flag = true; if(s.charAt(0)=='{'&&s.charAt(s.length()-1)=='}'&&list.size()==0) { System.out.println("False"); continue; } if(t.charAt(0)!='{'&&t.charAt(t.length()-1)!='}'){ String first = list.get(0); String last = list.get(list.size()-1); if(!s.substring(0,first.length()).equals(first)||!s.substring(s.length()-last.length(),s.length()).equals(last)){ System.out.println("False"); continue; }else{ String sub = ""; for (int j = 0; j < list.size(); j++) { sub = list.get(j); int index = s.indexOf(sub); if(index==-1){ System.out.println("False"); flag=false; }else{ sub = s.substring(index+sub.length()); } } if(flag==false) continue; } }else if(t.charAt(0)=='{'&&t.charAt(t.length()-1)!='}'){ String last = list.get(list.size()-1); if(!s.substring(s.length()-last.length(),s.length()).equals(last)){ System.out.println("False"); continue; }else{ String sub = ""; for (int j = 0; j < list.size(); j++) { sub = list.get(j); int index = s.indexOf(sub); if(index==-1){ System.out.println("False"); flag=false; }else{ sub = s.substring(index+sub.length()); } } if(flag==false) continue; } }else if(t.charAt(0)!='{'&&t.charAt(t.length()-1)=='}'){ String first = list.get(0); if(!s.substring(0,first.length()).equals(first)){ System.out.println("False"); continue; }else{ String sub = ""; for (int j = 0; j < list.size(); j++) { sub = list.get(j); int index = s.indexOf(sub); if(index==-1){ System.out.println("False"); flag=false; }else{ sub = s.substring(index+sub.length()); } } if(flag==false) continue; } }else{ String sub = ""; for (int j = 0; j < list.size(); j++) { sub = list.get(j); int index = s.indexOf(sub); if(index==-1){ System.out.println("False"); flag=false; }else{ sub = s.substring(index+sub.length()); } } if(flag==false) continue; } System.out.println("True"); } }4.概率题,偷了一点分,emmmm