去哪儿第一题
去哪儿第一题
输入:
1 2 3 4 5 5 5 5 5
输出:长度超过一半的数字
直接nextLine 空格split内存超,下面代码时间超,最终只有88%。
奇怪的是Integer.parseInt(sc.next()) 88%
int tmp = sc.nextInt();只有 75%
!sc.hasNext("\r\n"); 解析不到回车结束,hashNext贴上去就能过。
不会写代码了,已提交。
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int times = 1; while(sc.hasNext()){ int tmp = Integer.parseInt(sc.next()); if(tmp == num){ times++; }else{ times--; } if(times == 0){ num = tmp; times = 1; } } System.out.println(num); } }