Calculate Number Frequency

简介: 腾讯面试题:<br> 给你 10 分钟时间,根据上排给出十个数,在其下排填出对应的十个数,<br> 要求下排每个数都是先前上排那十个数在下排出现的次数。<br> 上排的十个数如下:<br><p>【0,1,2,3,4,5,6,7,8,9】</p> 举一个例子,<br> 数值: 0,1,2,3,4,5,6,7,8,9<br> 分配: 6,2,1,0,0,0,1,0,0,0<br>
腾讯面试题:
给你 10 分钟时间,根据上排给出十个数,在其下排填出对应的十个数,
要求下排每个数都是先前上排那十个数在下排出现的次数。
上排的十个数如下:

【0,1,2,3,4,5,6,7,8,9】

举一个例子,
数值: 0,1,2,3,4,5,6,7,8,9
分配: 6,2,1,0,0,0,1,0,0,0
0 在下排出现了 6 次,1 在下排出现了 2 次,
2 在下排出现了 1 次,3 在下排出现了 0 次....

以此类推..


package shuai.study.frequency;

/**
 * @author shengshu
 * 
 */
public class NumberFrequency {
	private final int len = 10;

	private int[] numberArray = null;
	private int[] frequencyArray = null;

	public NumberFrequency() {
		numberArray = new int[len];
		frequencyArray = new int[len];
	}

	// Initialize numberArray
	public void initNumberArray() {
		for (int i = 0; i < len; i++) {
			numberArray[i] = i;
		}
	}

	// Get final frequencyArray
	public int[] getFrequencyArray() {
		while (isSetFrequencyArray(numberArray)) {
			isSetFrequencyArray(numberArray);
		}

		return frequencyArray;
	}

	// Whether set frequencyArray according to judging 'frequencyArray[i] != frequency'
	public boolean isSetFrequencyArray(int[] numberArray) {
		boolean setNextFrequencyFlag = false;

		for (int i = 0; i < len; i++) {
			int frequency = getFrequency(numberArray[i]);

			if (frequencyArray[i] != frequency) {
				frequencyArray[i] = frequency;
				setNextFrequencyFlag = true;
			}
		}

		return setNextFrequencyFlag;
	}

	// Get numberArray's element(number) frequency 
	public int getFrequency(int number) {
		int frequency = 0;

		for (int i = 0; i < len; i++) {
			if (frequencyArray[i] == number) {
				frequency++;
			}
		}

		return frequency;
	}

	// Print array
	public void printArray(int[] array) {
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + "\t");
		}
	}

	// Main thread
	public static void main(String[] args) {
		NumberFrequency numberFrequency = new NumberFrequency();

		numberFrequency.initNumberArray();
		int[] frequencyArray = numberFrequency.getFrequencyArray();
		numberFrequency.printArray(frequencyArray);
	}
}


相关文章
|
1月前
|
Python
Calculating Dates
Calculating Dates
|
6月前
Adjustable Precision Shunt Regulator
一、产品描述 The TL431 is a three-terminal adjustable regulator series with a guaranteed thermal stability over applicable temperature ranges. The output voltage may be set to any value between Vref and 36 volts with two external resistors. These device have a typical dynamic output impedance of 0.27Ω,
|
Linux 定位技术 数据安全/隐私保护
【Calculate】Calculate Linux安装操作记录
【Calculate】Calculate Linux安装操作记录
144 0
A. Calculating Function
A. Calculating Function
49 0
Calculating Function
Calculating Function
94 0
Calculating Function
|
测试技术
std::accumulate的具体用法
std::accumulate的具体用法
362 0
std::accumulate的具体用法
HDU-1012,u Calculate e
HDU-1012,u Calculate e
|
Go
HDOJ 1012 u Calculate e
HDOJ 1012 u Calculate e
111 0
HDOJ 1012 u Calculate e
HDOJ 2114 Calculate S(n)(找周期)
HDOJ 2114 Calculate S(n)(找周期)
97 0
|
算法
HDOJ 1202 The calculation of GPA
HDOJ 1202 The calculation of GPA
117 0