华为笔试题0424

华为笔试0424

第一题

二分查找

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class H01 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<Integer> input = new ArrayList<>();
        String line = in.nextLine();
        String[] tokens = line.split(" "); // 使用正则表达式分割输入的字符串
        for (String token : tokens) {
            try {
                int num = Integer.parseInt(token);
                input.add(num);
            } catch (NumberFormatException e) {
                return;
            }
        }
        int target = in.nextInt();
        int[] nums = input.stream().mapToInt(Integer::valueOf).toArray();
        String res = findTree(nums, target);
        System.out.println(res);
    }

    public static String findTree(int[] nums, int target){
        Arrays.sort(nums);
        int left = 0, right = nums.length - 1;
        StringBuilder res = new StringBuilder("S");
        while (left < right){
            int mid = (left + right) / 2;
            if(nums[mid] == target){
                res.append("Y");
                return res.toString();
            } else if (nums[mid] > target) {
                right = mid - 1;
                res.append("L");
            }
            else {
                left = mid + 1;
                res.append("R");
            }
        }
        if(left == right && nums[left] == target){
            res.append("Y");
        }
        else {
            res.append("N");
        }
        return res.toString();
    }

}

第二题

排序

import java.util.*;
public class H02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<Player> players = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            String shots = sc.next();
            int goals = 0;
            int maxConsecutive = 0;
            int currentConsecutive = 0;
            int firstMiss = 0;

            for (int j = 0; j < m; j++) {
                if (shots.charAt(j) == '1') {
                    goals++;
                    currentConsecutive++;
                    maxConsecutive = Math.max(maxConsecutive, currentConsecutive);
                } else {
                    currentConsecutive = 0;
                    firstMiss = j;
                }
            }

            players.add(new Player(i, goals, maxConsecutive, firstMiss));
        }

        players.sort((a, b) -> {
            if (a.goals != b.goals) {
                return b.goals - a.goals;
            }
            if (a.maxConsecutive != b.maxConsecutive) {
                return b.maxConsecutive - a.maxConsecutive;
            }
            if(a.firstMiss != b.firstMiss){
                return b.firstMiss - a.firstMiss;
            }
            return a.id - b.id;
        });
        int len = players.size();
        for(int i = 0; i < len - 1; i++){
            System.out.print(players.get(i).id + " ");
        }
        System.out.println(players.get(len - 1).id);
    }
}

class Player {
    int id;
    int goals;
    int maxConsecutive;
    int firstMiss;

    Player(int id, int goals, int maxConsecutive, int firstMiss) {
        this.id = id;
        this.goals = goals;
        this.maxConsecutive = maxConsecutive;
        this.firstMiss = firstMiss;
    }
}
第三题
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值