2019.8.16 科大讯飞笔试java
本地过,但是第一题说case过不了,第二题直接说代码错了
80%的时间在改错,最后还是没过,心态炸了
谁能~
给我一剑?
1.数字排序
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); ArrayList<Character>res=new ArrayList<>(); String string=scanner.next(); char [] chars=string.toCharArray(); for (int i = 0; i < chars.length; i ) { if(chars[i]-'0'<=9 &&chars[i]-'0'>=0){ res.add(chars[i]); } } Collections.sort(res); if(res.size()>0) { for (int i = 0; i < res.size(); i ) { System.out.print(res.get(i)-'0'); } } else { System.out.println("-1"); } } }
2 递归二叉查找
import java.util.*; public class Main{ public static void main(String [] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer>list=new ArrayList<>(); String s=scanner.next(); if(s.length()==0){ System.out.println(-1); return; } if(s.length()>1) s=s.substring(1,s.length()-1); String []strings=s.split(","); if(strings.length>0) for (String string : strings) { list.add(Integer.parseInt(string)); } int res=bstSearch(19,list,0,list.size()-1); System.out.println(res); } private static int bstSearch(int target, ArrayList<Integer> list,int l, int r) { if(l>r){ return -1; } int mid=l (r-l)/2; if(list.get(mid)==target){ return mid 1; } else if(list.get(mid)>target){ return bstSearch(target,list,l,mid-1); } else{ return bstSearch(target,list,mid 1,r); } } }#科大讯飞##笔试题目##Java#