java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

华为上机面试题

华为java上机题目
题目:

手动输入一个字符串,仅限小写字母,统计并输出每个字符在字符串中出现的次数,并输出。提示可以用map

例子:输入:aaabbbccc

输出:a 3

b 3

c 3

分析: 看到后面的提示,简直就是不用动脑,直接简单粗暴的顺势而上

直接上代码:

package com.javaer.huawei;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class TestCharAcount {

	public static void main(String args[]) {
		String strIn = "aaabbbccc";
		TestCharAcount tc = new TestCharAcount();
		Map mTemp = tc.charAcount(strIn);

		Set ks = mTemp.keySet();// 生成索引set
		for (Iterator it = ks.iterator(); it.hasNext();) {// 遍历索引取值
			char c = it.next();
			System.out.println(c + " " + mTemp.get(c));
		}

	}

	public Map charAcount(String strIn) {
		String tempStr = strIn;

		// The map is sorted according to the natural ordering of its keys
		// treemap是以键的自然顺序来存储值的
		Map m = new TreeMap();
		char[] strC = tempStr.toCharArray();
		for (int i = 0; i < strC.length; i++) {
			Integer count = m.get(strC[i]);
			if (null == count)
				count = 0;

			count++;
			m.put(strC[i], count);
		}

		return m;
	}
}


This entry was posted in JAVA and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/huawei-mianshi-1/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

One Response to 华为上机面试题

Leave a Reply