题解 | HJ20#密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
//        1.长度超过8位
//        2.包括大小写字母.数字.其它符号,以上四种至少三种
//        3.不能有长度大于2的包含公共元素的子串重复 (注:其他符号不含空格或换行)
//        数据范围:输入的字符串长度满足 1≤n≤100
        Scanner scanner = new Scanner(System.in);
        //长度超过8位
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            if (str.length() >= 8 && str.length() <= 100) {
                //四种种情况至少出现3
//            if (bigChara>0&&)
                if (!subStringMoreThan2(str) && fourOrThree(str)) {
                    System.out.println("OK");
                } else {
                    System.out.println("NG");
                }
            }else {
                System.out.println("NG");
            }
        }
    }

//    有长度大于2的包含公共元素的子串重复
    public static boolean subStringMoreThan2(String str) {
        char[] chars = str.toCharArray();
        boolean flag = false;
        for (int i = 0; i < chars.length; i++) {
            for (int j = i + 1; j < chars.length; j++) {
                if (j + 2 < chars.length) {
                    if (chars[i] == chars[j] && chars[i + 1] == chars[j + 1]) {
                        if (chars[i + 2] == chars[j + 2]) {
                            flag = true;
                        }
                    }
                }
            }
        }
        return flag;
    }
    public static boolean fourOrThree(String str) {
        //包括大小写字母.数字.其它符号,以上四种至少三种
        int bigChara = 0;
        int smallChara = 0;
        int numChara = 0;
        int otherChara = 0;
        boolean flag = false;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (65 <= chars[i] && chars[i] <= 90) {
                bigChara++;
            } else if (97 <= chars[i] && chars[i] <= 122) {
                smallChara++;
            } else if (48 <= chars[i] && chars[i] <= 57) {
                numChara++;
            } else if (123 <= chars[i] || chars[i] <= 47 || (58 <= chars[i] &&
                       chars[i] <= 64) || (91 <= chars[i] && chars[i] <= 96)) {
                otherChara++;
            }
        }
        if (bigChara > 0) {
            if (smallChara > 0) {
                if (numChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (numChara > 0) {
                if (smallChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (otherChara > 0) {
                if (smallChara > 0 || numChara > 0) {
                    flag = true;
                }
            }
        } else if (smallChara > 0) {
            if (bigChara > 0) {
                if (numChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (numChara > 0) {
                if (bigChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (otherChara > 0) {
                if (bigChara > 0 || numChara > 0) {
                    flag = true;
                }
            }

        } else if (numChara > 0) {
            if (bigChara > 0) {
                if (smallChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (smallChara > 0) {
                if (bigChara > 0 || otherChara > 0) {
                    flag = true;
                }
            } else if (otherChara > 0) {
                if (bigChara > 0 || smallChara > 0) {
                    flag = true;
                }
            }

        } else if (otherChara > 0) {
            if (bigChara > 0) {
                if (numChara > 0 || numChara > 0) {
                    flag = true;
                }
            } else if (numChara > 0) {
                if (bigChara > 0 || smallChara > 0) {
                    flag = true;
                }
            } else if (smallChara > 0) {
                if (bigChara > 0 || numChara > 0) {
                    flag = true;
                }
            }

        }
        return flag;
    }
}

情况较多 写的比较复杂 后续有待优化

全部评论

相关推荐

躺尸修仙中:因为很多92的也去卷中小厂,反正投递简历不要钱,面试不要钱,时间冲突就推,不冲突就面试积累经验
点赞 评论 收藏
分享
投票
我要狠拿offer:如果不是必须去成都绝对选九院呀,九院在四川top1研究所了吧
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务