必威体育Betway必威体育官网
当前位置:首页 > IT技术

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

时间:2019-10-16 16:44:32来源:IT技术作者:seo实验室小编阅读:79次「手机版」
 

华为笔试

我的个人微信公众号: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<Character, integer> map = new LinkedHashMap<Character, Integer>();
		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<Character, Integer> 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<Node> {		
		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<Character, Integer> charAndNum = statisticsCharNum(input.toCharArray());
		String encodedBinariStr = encode2binary(input, charAndNum);
		System.out.println(encodedBinariStr);
	}
	
	public static Map<Character, Integer> statisticsCharNum(char[] charArray) {		
		Map<Character, Integer> map = new HashMap<Character, Integer>();		
		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<Character, Integer> statisticsChar2Num, List<Node> leafs) {	
		
		Character[] keys_char = statisticsChar2Num.keySet().toArray(new Character[0]); 		
		PriorityQueue<Node> priorityQueue = new PriorityQueue<Node>();
		
		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<Character, Integer> statisticsChar2Num) {		
		if (inputStr == null || inputStr.equals("")) {			
			return "";		
			} 		
		char[] char_Array = inputStr.toCharArray();		
		List<Node> leaf_Nodes = new ArrayList<Node>();		
		buildTree(statisticsChar2Num, leaf_Nodes);		
		Map<Character, String> 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<Character, String> buildEncoding2Binary(List<Node> leafNodes) {		
		Map<Character, String> encodewords = new HashMap<Character, String>();		
		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. 题目很长,读了好久都没明白题目意思。在此就不列出啦!

相关阅读

华为云计算笔记(随笔)

fusionSphere 1.fusion Computer SV (1)VRM (2)CNA      (1)和(2)合起来叫FusionSphere 2.Fusion Manger  租户管理 3.Fusion

华为Mate 30发布,苏宁推出以旧换新400补贴等10大福利

9月19日晚,华为在德国慕尼黑举办Mate 30系列发布会,发布年度旗舰手机华为Mate30系列。据悉,华为新品手机已于9月20日0点在苏宁开启预

华为稳了:余承东在微博上发文“稳了,我们十月十六日伦敦

A5创业网(公众号:iadmin5)9月13日讯,今日苹果正式发布了新一代的iPhone手机,分别是iPhone XS、iPhone XS MAX以及iPhone XR,三者在外

高新兴--java工程师笔试题

                           高新兴--java工程师笔试题 试卷中涵盖前端技术、java基础知识、开源框架和数据库技

巴西5G不排除华为:巴西需要华为所提供的电信技术

A5创业网(公众号:iadmin5)6月9日讯,近日工信部下发了5张5G商用牌照,预示着5G离我们的生活也越来越近了,作为5G技术的核心企业华为仍然

分享到:

栏目导航

推荐阅读

热门阅读