9.1深信服开发岗笔试

C++岗,但是只会Java,C++选择填空看不懂。做的不好。。看运气了。。
算法最后一题比较难,没时间做了

1.数组前两个数比较,求最大的。然后依次往后比较,求连续k轮胜出的元素。
public class Q1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] arr = str.split(" ");
        long[] nums = new long[arr.length - 1];
        for(int i = 0;i < nums.length;i++){
            nums[i] = Long.parseLong(arr[i]);
        }
        int k = Integer.parseInt(arr[arr.length - 1]);
        long maxVal = nums[0];
        Map<Long,Integer> map = new HashMap<>();
        for(int i = 1;i < nums.length;i++){


             maxVal = Math.max(maxVal,nums[i]);
            map.put(maxVal,map.getOrDefault(maxVal,0) + 1);
        }
        if(k >= nums.length){
            long m = getMax(nums);
            System.out.println(m);
        }else if(k < nums.length){
            Set<Long> set = map.keySet();
            for(long se:set){
                int val = map.get(se);
                if(val >= k){
                    System.out.print(se);
                }
            }
        }
    }
    public static long getMax(long[] nums){
        long max = nums[0];
        for(long num : nums){
            if(max < num){
                max = num;
            }
        }
        return max;
    }

}
2.求数组中最长的连续子序列。
import java.util.*;
public class Q5 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        long[] nums = new long[n];
        for(int i = 0;i < nums.length;i++){
            nums[i] = scan.nextLong();
        }
        Arrays.sort(nums);
        Set<Long> set = new HashSet<>();
        for(long num:nums){
            set.add(num);
        }

        long count = 0;
        for(long num : set){
            if(!set.contains(num - 1)){
                long current = num;
                while(set.contains(current + 1)){
                    current ++;
                }
                count = Math.max(count,current - num + 1);
            }
        }
        System.out.println(count);
    }
}



#深信服笔试题#
全部评论
贴下第三题
点赞 回复
分享
发布于 2022-09-01 21:18 浙江
老哥,第一题双向链表模拟可以出来吗?
点赞 回复
分享
发布于 2022-09-01 21:19 浙江
联易融
校招火热招聘中
官网直投
我没参加,这是群里其他老哥拍的照片,我写了下,让其他大佬看了下,说是没问题
点赞 回复
分享
发布于 2022-09-01 21:28 浙江
第二问暴力60%,其他两个ac了,不知道能不能过,选择题还好,填空题有两个不确定😂
点赞 回复
分享
发布于 2022-09-01 21:58 湖北
第一题用链表可以吗
点赞 回复
分享
发布于 2022-09-01 22:21 北京
第二题直接sort,应该不行吧,我试了只能过30好像
点赞 回复
分享
发布于 2022-09-04 14:39 河南

相关推荐

1 15 评论
分享
牛客网
牛客企业服务