字节笔试 后端笔试题(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




        
#字节笔试##笔试##字节跳动##字节23秋招笔试太难了吧##原来字节劝退的只是我,罢了罢了#
全部评论
同学可以看看微众银行秋招,武汉深圳都有岗位https://www.nowcoder.com/discuss/1022146
1 回复 分享
发布于 2022-08-22 20:10 广东
第三题倒没那么麻烦  写个正则就过了==
4 回复 分享
发布于 2022-08-21 21:46 上海
字节是先面试还是先笔试?
2 回复 分享
发布于 2022-08-21 21:56 江苏
在 vx 公众号 度小满 招聘 可以投递 也可以在官网投递 内推码 NTAM8vC  随时查看进度(可私信可留言)
1 回复 分享
发布于 2022-08-23 00:00 北京
请问一下第一题第二种情况:1010。如果换最后两位 最小值不是11吗,不等于你这里的n*11-1.
点赞 回复 分享
发布于 2022-08-21 22:45 英国
笔试可以用go嘛,可不可以用ide
点赞 回复 分享
发布于 2022-08-22 11:10 江苏
第三题是力扣44题,做过的话还挺好写的。不过第二题最后才想到,只过了60
点赞 回复 分享
发布于 2022-08-22 11:17 北京
第四题在offer 60的基础上改就能做出来
点赞 回复 分享
发布于 2022-08-22 14:26 北京
理想汽车2023提前批校招目前已开启,有打算找工作的师弟师妹们,可以通过以下链接内推投递,全程进度跟随,无笔试。 https://www.nowcoder.com/discuss/1002231
点赞 回复 分享
发布于 2022-08-22 20:03 北京
恒生电子股份内推 offer已就位,就等你来投,填写内推码,快人一步! 网申链接:campus.hundsun.com 内推码:ESVGRV
点赞 回复 分享
发布于 2022-08-22 20:30 陕西
字节难还是前两天的网易难啊
点赞 回复 分享
发布于 2022-08-22 21:03 江苏
对那么多题还挂啊。。。昨天那个题真的超难
点赞 回复 分享
发布于 2022-08-22 21:22 福建
大佬要不试试阿里巴巴可以选择三个岗位呢 https://www.nowcoder.com/discuss/1022639
点赞 回复 分享
发布于 2022-08-23 08:58 上海

相关推荐

10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
20 57 评论
分享
牛客网
牛客企业服务