9.4 哔哩哔哩笔试

1. 第一题:

public class Main1 {

    public static int GetMaxConsecutiveOnes(int[] arr, int k) {
        int left = 0;
        int right = 0;
        int max = 0;
        int zero = 0;
        while(right != arr.length)
        {
            if(arr[right++] == 0)
            {
                zero++;
            }
            while(zero > k) // 0的数量大于k
            {
                if(arr[left++] == 0)
                {
                    zero--;
                }
            }
            int count = right - left;
            if(count > max)
            {
                max = count;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int arr1[] = { 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0 };
        int k1 = 2;
        int res1 = GetMaxConsecutiveOnes(arr1, k1);
        System.out.println(res1);

        int arr2[] = { 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1 };
        int k2 = 3;
        int res2 = GetMaxConsecutiveOnes(arr2, k2);
        System.out.println(res2);
    }
}

2. 第二题:

import java.util.*;

public class Main2 {

    public static int[] SpiralMatrix (int[][] matrix) {
        List<Integer> ans = new ArrayList<>();
        if(matrix.length == 0)
        {
            int temp[] = new int[0];
            return temp;
        }
        int r1 = 0;
        int r2 = matrix.length - 1;
        int c1 = 0;
        int c2 = matrix[0].length - 1;

        while(r1 <= r2 && c1 <= c2)
        {
            for(int c = c1; c <= c2; c++)
            {
                ans.add(matrix[r1][c]);
            }
            for(int r = r1 + 1; r <= r2; r++)
            {
                ans.add(matrix[r][c2]);
            }
            if(r1 < r2 && c1 < c2)
            {
                for(int c = c2 - 1; c >= c1 + 1; c--)
                {
                    ans.add(matrix[r2][c]);
                }
                for(int r = r2; r >= r1 + 1; r--)
                {
                    ans.add(matrix[r][c1]);
                }
            }
            r1++;
            c1++;
            r2--;
            c2--;
        }

        int res[] = new int[ans.size()];
        for(int i = 0; i < ans.size(); i++)
        {
            res[i] = ans.get(i);
        }
        return res;
    }
    
    public static void main(String[] args) {
        int matrix[][] = {{ 1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
        int res[] = SpiralMatrix(matrix);
        for(int i = 0; i < res.length; i++)
        {
            System.out.print(res[i] + " ");
        }
    }
}

3. 第三题:

public class Main3 {
    
    public static int GetFragment (String str) {

        if(str.equals(""))
        {
            return 0;
        }
        
        str = str.trim();

        char c = str.charAt(0);
        int n = 1; // 统计不同的碎片的数量
        
        for(int i = 1; i < str.length(); i++)
        {
            if(c != str.charAt(i))
            {
                c = str.charAt(i);
                n++;
            }
        }
        int res = (int)(str.length() * 1.0 / n);
        return res;
    }

    public static void main(String[] args) {
        String str = "aaabbaaac";
        int res = GetFragment(str);
        System.out.println(res);
    }
}

你可能感兴趣的:(秋招笔试)