码迷,mamicode.com
首页 > 其他好文 > 详细

Calculate Number Frequency

时间:2014-07-29 14:41:57      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:blog   java   for   2014   ar   时间   new   log   

腾讯面试题:
给你 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);
	}
}


Calculate Number Frequency,布布扣,bubuko.com

Calculate Number Frequency

标签:blog   java   for   2014   ar   时间   new   log   

原文地址:http://blog.csdn.net/three_man/article/details/38235941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!