import java.util.HashMap;
import java.util.Map;
public class Main {
public static int solution(int n) {
if(n <= 100)return n;
int count = 100;
for (int i = 101; i <= n; i++) {
String strI = String.valueOf(i);
Map<Character, Integer> map = new HashMap<>();
for (int j = 0; j < strI.length(); j++) {
if(!map.containsKey(strI.charAt(j))) {
map.put(strI.charAt(j), 1);
} else {
map.put(strI.charAt(j), map.get(strI.charAt(j)) + 1);
}
}
if(map.keySet().stream().count() <= 2)count++;
}
System.out.println(count);
return count; // placeholder return
}
}
思路如下:
从101开始判断,例如判断111;
1.将111转换为字符串,然后进行遍历
2.字符串中出现的元素统计到map中
3.最后查看map中的键值对是否是一个或者两个,如果是,计数器加一