【华为2018年校园招聘】算法岗笔试题

 我的个人微信公众号:Microstrong

微信公众号ID:MicrostrongAI

公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步!

知乎专栏:https://zhuanlan.zhihu.com/Microstrong

Github:https://github.com/Microstrong0305

个人博客:https://blog.csdn.net/program_developer

1. 给定一行字符串,求出这行字符串中出现频率最高的字符,字符串中含有标点符号,字符不区分大小写。如果出现频率相同时,输出先出现在字符串中的字符。

输入:

输入一行字符串,字符串中可能包含多个空格,也可能包含标点符号,但肯定包含字符。

输出:

 输出字符的大写和出现频率。

输入样例:

Abcdefg  ahigkl Mnopq rstu o v wBBBBBB!

输出样例:

B7 

已经AC代码: 

import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
		String strs = input.replaceAll(" ","").toLowerCase();
		LinkedHashMap map = new LinkedHashMap();
		Character CharStr = null;
		Integer CountmaxLength = 0;
		for (Character temp : strs.toCharArray()) {
			if (map.containsKey(temp)) { 
				map.put(temp, map.get(temp) + 1);
			} else {
				map.put(temp, 1);
			}
		}
		for (Entry entry : map.entrySet()) {
			if (entry.getValue() > CountmaxLength) {
				CharStr = entry.getKey();
				CountmaxLength = entry.getValue();
			}	
		}
		
		System.out.println(Character.toUpperCase(CharStr)+CountmaxLength.toString());
	}
	
}

2. 给你一个字符串,以这个字符串中字符出现的频率为权重,构造这个字符串的哈夫曼编码。(题目给的很长,其实意思简洁明了。)

输入样例:

abbcccdddd

输出样例:

1101111111010100000

已经AC代码:


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main2 {
	
	static class Tree {	
		
		private Node root; 
		
		public Node getRoot() {			
			return root;		
			}
		
		public void setRoot(Node root) {			
			this.root = root;		
			}	
		} 	
	
	static class Node implements Comparable {		
		private String Strchars = "";		
		private int Numfrequence = 0;		
		private Node parent;		
		private Node leftNode;		
		private Node rightNode; 
		
		@Override		
		public int compareTo(Node n) {			
			return Numfrequence - n.Numfrequence;		
			} 
		
		public boolean isLeaf() {			
			return Strchars.length() == 1;		
			}
		
		public boolean isRoot() {			
			return parent == null;		
			} 	
		
		public boolean isLeftChild() {			
			return parent != null && this == parent.leftNode;		
			} 	
		
		public int getFrequence() {			
			return Numfrequence;		
			} 
		
		public void setFrequence(int frequence) {			
			this.Numfrequence = frequence;		
			} 	
		
		public String getChars() {			
			return Strchars;		
			} 	
		
		public void setChars(String chars) {			
			this.Strchars = chars;		
			} 	
		
		public Node getParent() {			
			return parent;		
			} 	
		
		public void setParent(Node parent) {			
			this.parent = parent;		
			} 	
		
		public Node getLeftNode() {			
			return leftNode;		
			} 	
		
		public void setLeftNode(Node leftNode) {			
			this.leftNode = leftNode;		
			} 	
		
		public Node getRightNode() {			
			return rightNode;		
			} 
		
		public void setRightNode(Node rightNode) {			
			this.rightNode = rightNode;		
			}	
		}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
		Map charAndNum = statisticsCharNum(input.toCharArray());
		String encodedBinariStr = encode2Binary(input, charAndNum);
		System.out.println(encodedBinariStr);
	}
	
	public static Map statisticsCharNum(char[] charArray) {		
		Map map = new HashMap();		
		for (char temp : charArray) {			
			Character Tempchar = new Character(temp);			
			if (map.containsKey(Tempchar)) {				
				map.put(Tempchar, map.get(Tempchar) + 1);			
			} else {				
				map.put(Tempchar, 1);			
				}		
			} 		
		return map;	
		}
	
	/**
	 * 构建树
	 * @param statistics
	 * @param leafs
	 * @return
	 */
	private static Tree buildTree(Map statisticsChar2Num, List leafs) {	
		
		Character[] keys_char = statisticsChar2Num.keySet().toArray(new Character[0]); 		
		PriorityQueue priorityQueue = new PriorityQueue();
		
		for (Character character : keys_char) {			
			Node node = new Node();			
			node.Strchars = character.toString();			
			node.Numfrequence = statisticsChar2Num.get(character);			
			priorityQueue.add(node);			
			leafs.add(node);		
			} 	
		
		int sizeNum = priorityQueue.size();	
		
		for (int i = 1; i <= sizeNum - 1; i++) {			
			Node nodeOne = priorityQueue.poll();			
			Node nodeTwo = priorityQueue.poll(); 			
			Node totalNode = new Node();			
			totalNode.Strchars = nodeOne.Strchars + nodeTwo.Strchars;			
			totalNode.Numfrequence = nodeOne.Numfrequence + nodeTwo.Numfrequence; 			
			totalNode.leftNode = nodeOne;			
			totalNode.rightNode = nodeTwo; 			
			nodeOne.parent = totalNode;			
			nodeTwo.parent = totalNode; 			
			priorityQueue.add(totalNode);		
			} 	
		
		Tree tree = new Tree();		
		tree.root = priorityQueue.poll();		
		return tree;	
		}
	
	public static String encode2Binary(String inputStr, Map statisticsChar2Num) {		
		if (inputStr == null || inputStr.equals("")) {			
			return "";		
			} 		
		char[] char_Array = inputStr.toCharArray();		
		List leaf_Nodes = new ArrayList();		
		buildTree(statisticsChar2Num, leaf_Nodes);		
		Map encod2BinaryInfo = buildEncoding2Binary(leaf_Nodes); 		
		StringBuffer buffer = new StringBuffer();		
		for (char temp : char_Array) {			
			Character character = new Character(temp);			
			buffer.append(encod2BinaryInfo.get(character));		
			} 		
		return buffer.toString();	
		} 
	
	private static Map buildEncoding2Binary(List leafNodes) {		
		Map encodewords = new HashMap();		
		for (Node leafNode : leafNodes) {			
			Character character = new Character(leafNode.getChars().charAt(0));			
			String encodeword = "";			
			Node currentNode = leafNode; 			
			do {				
				if (currentNode.isLeftChild()) {					
					encodeword = "0" + encodeword;				
				} else {					
					encodeword = "1" + encodeword;				
					} 				
				currentNode = currentNode.parent;			
				} while (currentNode.parent != null); 			
			encodewords.put(character, encodeword);		
			} 		
		return encodewords;	
		}
}

Reference:

【1】https://blog.csdn.net/kimylrong/article/details/17022319

3. 题目很长,读了好久都没明白题目意思。在此就不列出啦!

你可能感兴趣的:(面试+笔试)