题解 | #密码验证合格程序#
密码验证合格程序
https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841
利用正则取匹配,a.matches(".*[A-Z].*") 不太清楚 为什么在[] 前后还需要加上 .* 不加就匹配不到了。
.matches()方法不是将整个域于模式相匹配吗?
在菜鸟教程拷贝过来的原话,如果是,我觉得不需要添加.*;
[xyz] |
字符集。匹配包含的任一字符。例如,"[abc]"匹配"plain"中的"a"。 |
虽然AC了 但是求看到的大佬解答一下
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
int num = 0;
if (a.length() < 8 ) {
System.out.println("NG");
break;
}
char[] da = a.toCharArray();
HashSet<String> set = new HashSet<String>();
for (int i = 3 ; i <= da.length ; i ++) {
if ( !set.contains(a.substring(i - 3, i) ) ) {
set.add( a.substring(i - 3, i) ) ;
} else {
num = -100;
break;
}
}
if (a.matches(".*[A-Z].*")) {
num += 1;
}
if (a.matches(".*[a-z].*")) {
num += 1;
}
if (a.matches(".*[0-9].*")) {
num += 1;
}
if (a.matches(".*[^\\da-zA-Z].*")) {
num++;
}
System.out.println(num > 2 ? "OK" : "NG");
}
}
}
#22届#