2017年阿里校招Android附加第三题

        不记得原题,题目的大概意思是输入一段字符串,字符串里面包含字母大、小写和数字,需要应试者找出字符串中的数字,并把出现次数最多的数字求和输出。

也许我表达不够准确,举个例子吧:输入——ss2kk3kh4n11h11gg2k11字符串;输出:33。

本人程序源代码如下,请多多指点!


package test3;
/*
 * 阿里笔试最后一题编程题
 */
import java.util.*;

public class Main1 {

	public static void main(String[] args) {

		Scanner in = new Scanner(System.in);

		while (in.hasNext()) {
			String str = in.nextLine();
			fuc(str);
		}
	}

	public static void fuc(String str) {
		List<Integer> list = new ArrayList<>();
		int len = str.length();
		int s = 0;
		int z = 0;
		int f = 0;
		for (int i = 0; i < len; i++) {
			boolean b = str.charAt(i) < 58 && str.charAt(i) > 47;
			if (b)
				s++;
			else
				z++;
			if (s == 0) {
				z = 0;
				continue;

			} else if (s > 0 && z == 1 || s > 0 && i == len - 1) {
				String s1 = null;
				if (i == len - 1) {
					f++;
					s1 = str.substring(i - f + 1, i + 1);
				} else
					s1 = str.substring(i - f, i);
				int sum = 0;
				for (int j = 0; j < f; j++) {
					sum += (s1.charAt(j) - 48) * Math.pow(10, f - j - 1);

				}
				list.add(sum);
				s = 0;
				z = 0;
				f = 0;
			}

			else if (s > 0 && z == 0) {
				f++;
				continue;
			}
		}
		List<Integer> lis = new ArrayList<>();
		Map<Integer, Integer> m = new HashMap<>();
		for (int i = 0; i < list.size(); i++) {
			if (!lis.contains(list.get(i))) {
				lis.add(list.get(i));
				m.put(list.get(i), 1);
			} else {
				m.put(list.get(i), m.get(list.get(i)) + 1);
			}
		}

		Set<Integer> set = m.keySet();
		Iterator<Integer> it = set.iterator();
		int max = 0;
		int k = 0;
		while (it.hasNext()) {
			int it0 = it.next();
			int it1 = m.get(it0);
			if (max < it1) {
				max = it1;
				k = it0;
			}
		}

		System.out.println(max * k);
	}
}







  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值