当前位置: 首页 > 面试经验 >

趋势科技笔试两道算法题复盘(Java)

优质
小牛编辑
96浏览
2023-03-28

趋势科技笔试两道算法题复盘(Java)

第一道:字符串替换
public class 趋势科技1 {
    /*
    字符串替换
     */
    public static void main(String[] args) {
        String my_template = "%FILENAME%VIRUSNAME%balabalabala%VIRUSNAME%FILENAME%";
        String[] keys = new String[]{"SENDER","RCPTS","SUBJECT","FILENAME","VIRUSNAME","ACTION"};
        String[] values  = new String[]{"abc@test.com","test@company.com","Test","Myfile","ICAN","Delete"};
        StringBuilder sb = new StringBuilder();
        int len = my_template.length();
        for(int i=0;i<len;i++){
            if(my_template.charAt(i)=='%'){
                boolean flag = false;
                for(int j=0;j<keys.length;j++){
                    int size = keys[j].length();
                    if(size+i+1<len
                    &&my_template.substring(i+1,size+i+1).equals(keys[j])
                    &&my_template.charAt(i+size+1)=='%'){
                        sb.append(values[j]);
                        i=i+size+1;
                        flag=true;
                        break;
                    }
                }
                if(!flag)sb.append(my_template.charAt(i));
            }else{
                sb.append(my_template.charAt(i));
            }
        }
        System.out.println(String.valueOf(sb));
    }
}
第二道:岛屿问题中加入最大曼哈顿距离
import java.util.ArrayList;

/*
    岛屿问题中再加入最长曼哈顿距离
 */
public class 趋势科技2 {
    static int max = Integer.MIN_VALUE;
    public static void main(String[] args) {
        int count = 0;
        int[][] nums = new int[][]{
                {0,0,0,0,0,1,1,1,1,1},
                {0,1,0,0,0,1,0,0,0,1},
                {0,0,0,0,0,1,0,0,0,1},
                {0,0,0,0,0,1,1,1,1,1},
                {0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,1,1,1,0,0,0},
                {0,0,0,1,1,1,1,1,0,0},
                {1,1,1,1,1,1,1,1,1,0}
        };
        boolean[][] flag = new boolean[nums.length][nums[0].length];
        for(int i=0;i< nums.length;i++){
            for(int j=0;j<nums[0].length;j++){
                if(nums[i][j]==1&&!flag[i][j]){
                    dfs(nums,i,j,new ArrayList<>(),flag);
                    count++;
                }
            }
        }
        System.out.println(count);
        System.out.println(max);
    }
    public static void dfs(int[][] nums, int i, int j, ArrayList<int[]> list,boolean[][] flag){
        //边界和判断条件
        if(i<0||j<0||i>=nums.length||j>=nums[0].length||nums[i][j]==0||flag[i][j])return;
        //这里因为要用到网格的1,所以不能设为0,添加了一个flag来做是否走过的判断
        flag[i][j] = true;
        //使用一个list<int[]> 存一个图形中所有的点,以此来算最大曼哈顿距离
        for(int[] pre : list){
            int temp = Math.abs(pre[0]-i)+Math.abs(pre[1]-j);
            max = Math.max(temp,max);
        }
        //添加这次的点到list中
        list.add(new int[]{i,j});
        //遍历四周
        dfs(nums,i+1,j,list,flag);
        dfs(nums,i,j+1,list,flag);
        dfs(nums,i-1,j,list,flag);
        dfs(nums,i,j-1,list,flag);
    }
}


#趋势科技招聘##趋势科技笔试##趋势科技#
 类似资料: