一.编程题
字符串中找出连续最长的数字串_牛客题霸_牛客网 (nowcoder.com)
例子:abcd12345ed125ss123456789
public class Study {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String ret = "";//所有的数字字符串
String cur = "";//最终结果
int i=0;
for(;i < str.length();i++) {
char ch = str.charAt(i);
if(ch >= '0' && ch <= '9') {
cur = cur + ch+"";
}else{
if(ret.length() < cur.length()) {
ret = cur;
}
cur = "";
}
}
//处理123abc123456这种情况
if(i== str.length() && ret.length()< cur.length()) {
ret = cur;
}
System.out.println(ret);
}
}
数组中出现次数超过一半的数字_牛客题霸_牛客网 (nowcoder.com)
easy.使用HashMap
public class Study {
public static void main(int []array) {
int half=array.length/2;
HashMap<Integer,Integer>hashMap=new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (!hashMap.containsKey(array[i])){
hashMap.put(array[i],1);
}else {
hashMap.put(array[i],hashMap.get(array[i])+1);
}
}
for (int i = 0; i < array.length; i++) {
if (hashMap.get(array[i])>=half){
System.out.println(array[i]);
}
}
}
}
二.选择题错题
题目1:
String name是private的,不允许在类外进行访问,只能在类内进行访问
题目2:
答案:编译错误
子类对象可能需要动用到从父类继承下来的东西,所以那些东西必须要先完成。父类的构造函数必须在子类的构造函数之前结束。
题目3: