9.8 小米笔试

1. 第一题:

// 第一题:判断密码是否符合要求:
// 给一系列密码:每个密码用空格分隔
//(1)同时具有大写、小写、符号、数字输出0;
//(2)长度不在8-120之间,输出1;
//(3)类型不符合输出2;

import java.util.*;

public class Main1 {

    // 方法1:
    // 数字
    public static final String REG_NUMBER = ".*\\d+.*";
    // 大写字母
    public static final String REG_UPPERCASE = ".*[A-Z]+.*";
    // 小写字母
    public static final String REG_LOWERCASE = ".*[a-z]+.*";
    // 特殊符号(~!@#$%^&*()_+|<>,.?/:;'[]{}\)
    public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";

    public static int check1(String password)
    {
        // 密码为空及长度大于8位小于120位判断
        if(password == null || password.length() < 8 || password.length() > 120)
        {
            return 1;
        }
        int count = 0;
        if(password.matches(REG_NUMBER)) count++;
        if(password.matches(REG_UPPERCASE)) count++;
        if(password.matches(REG_LOWERCASE)) count++;
        if(password.matches(REG_SYMBOL)) count++;

        if(count != 4)
        {
            return 2;
        }

        return 0;
    }

    // 方法2:
    public static int check2(String password) {
        int n = password.length();
        if (n < 8 || n > 120) return 1;
        boolean num = false, symbol = false, D = false, X = false;
        for (int i = 0; i < n; ++i) {
            if (num == true && symbol == true && D == true && X == true) return 0;
            if (password.charAt(i) >= '0' && password.charAt(i) <= '9') num = true;
            if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') X = true;
            if (password.charAt(i) >= 'A' && password.charAt(i) <= 'Z') D = true;
            else symbol = true;
        }
        return 2;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext())
        {
            String str = sc.next();
            int res1 = check1(str);
            System.out.println(res1);
            int res2 = check2(str);
            System.out.println(res2);
        }
    }
}

// 输入:
// 123 12345678 123abcABC!!!
//
// 输出:
// 1
// 2
// 0

2. 第二题:

import java.util.*;

public class Main2 {

    public static boolean exist(char[][] board, String word)
    {
        if(board == null || board.length ==0 || board[0].length == 0 || word.equals(""))
        {
            return false;
        }
        int m = board.length;
        int n = board[0].length;

        boolean visited[][] = new boolean[m][n];
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(board[i][j] == word.charAt(0) && dfs(i, j, 0, word, visited, board))
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean dfs(int i, int j, int idx, String word, boolean visited[][], char board[][])
    {
        if(idx == word.length())
        {
            return true;
        }
        if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(idx) || visited[i][j])
        {
            return false;
        }
        visited[i][j] = true;
        if(dfs(i + 1, j, idx + 1, word, visited, board) || dfs(i, j + 1, idx + 1, word, visited, board)
        || dfs(i - 1, j, idx + 1, word, visited, board) || dfs(i, j - 1, idx + 1, word, visited, board))
        {
            return true;
        }
        visited[i][j] = false;

        return false;
    }

    public static void main(String[] args) {
        char board[][] = {{'A','B','C','E'}, {'S','F','C','S'}, {'A','D','E','E'}};
        Scanner sc = new Scanner(System.in);
        String word = sc.nextLine();
        boolean res = exist(board, word);
        System.out.println(res);
    }
}

参考:

  1. Java密码强度正则表达式 – 必须包含大小写字母、数字、符号满足其中的3种
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dev_zyx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值